2 // System.NumberFormatter.cs
5 // Kazuki Oikawa (kazuki@panicode.com)
6 // Eyal Alaluf (eyala@mainsoft.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2008 Mainsoft Co. (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // NumberFormatter is shared with Grasshopper and hence the #if TARGET_JVM for
32 // marking the use of unsafe code that is not supported in Grasshopper.
37 using System.Globalization;
39 using System.Threading;
40 using System.Runtime.CompilerServices;
44 internal sealed partial class NumberFormatter
48 const int DefaultExpPrecision = 6;
49 const int HundredMillion = 100000000;
50 const long SeventeenDigitsThreshold = 10000000000000000;
51 const ulong ULongDivHundredMillion = UInt64.MaxValue / HundredMillion;
52 const ulong ULongModHundredMillion = 1 + UInt64.MaxValue % HundredMillion;
54 const int DoubleBitsExponentShift = 52;
55 const int DoubleBitsExponentMask = 0x7ff;
56 const long DoubleBitsMantissaMask = 0xfffffffffffff;
57 const int DecimalBitsScaleMask = 0x1f0000;
59 const int SingleDefPrecision = 7;
60 const int DoubleDefPrecision = 15;
61 const int Int8DefPrecision = 3;
62 const int UInt8DefPrecision = 3;
63 const int Int16DefPrecision = 5;
64 const int UInt16DefPrecision = 5;
65 const int Int32DefPrecision = 10;
66 const int UInt32DefPrecision = 10;
67 const int Int64DefPrecision = 19;
68 const int UInt64DefPrecision = 20;
69 const int DecimalDefPrecision = 100;
70 const int TenPowersListLength = 19;
72 const double MinRoundtripVal = -1.79769313486231E+308;
73 const double MaxRoundtripVal = 1.79769313486231E+308;
76 // The below arrays are taken from mono/metatdata/number-formatter.h
78 private static readonly unsafe ulong* MantissaBitsTable;
79 private static readonly unsafe int* TensExponentTable;
80 private static readonly unsafe char* DigitLowerTable;
81 private static readonly unsafe char* DigitUpperTable;
82 private static readonly unsafe long* TenPowersList;
84 // DecHexDigits s a translation table from a decimal number to its
85 // digits hexadecimal representation (e.g. DecHexDigits [34] = 0x34).
86 private static readonly unsafe int* DecHexDigits;
88 [MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
89 private unsafe static extern void GetFormatterTables (out ulong* MantissaBitsTable, out int* TensExponentTable,
90 out char* DigitLowerTable, out char* DigitUpperTable,
91 out long* TenPowersList, out int* DecHexDigits);
93 unsafe static NumberFormatter()
95 GetFormatterTables (out MantissaBitsTable, out TensExponentTable,
96 out DigitLowerTable, out DigitUpperTable, out TenPowersList, out DecHexDigits);
101 static long GetTenPowerOf(int i)
103 return TenPowersList [i];
105 #endregion Static Fields
109 private NumberFormatInfo _nfi;
111 //part of the private stringbuffer
112 private char[] _cbuf;
115 private bool _infinity;
116 private bool _isCustomFormat;
117 private bool _specifierIsUpper;
118 private bool _positive;
119 private char _specifier;
120 private int _precision;
121 private int _defPrecision;
123 private int _digitsLen;
124 private int _offset; // Represent the first digit offset.
125 private int _decPointPos;
127 // The following fields are a hexadeimal representation of the digits.
128 // For instance _val = 0x234 represents the digits '2', '3', '4'.
129 private uint _val1; // Digits 0 - 7.
130 private uint _val2; // Digits 8 - 15.
131 private uint _val3; // Digits 16 - 23.
132 private uint _val4; // Digits 23 - 31. Only needed for decimals.
136 #region Constructor Helpers
138 // Translate an unsigned int to hexadecimal digits.
139 // i.e. 123456789 is represented by _val1 = 0x23456789 and _val2 = 0x1
140 private void InitDecHexDigits (uint value)
142 if (value >= HundredMillion) {
143 int div1 = (int)(value / HundredMillion);
144 value -= HundredMillion * (uint)div1;
145 _val2 = FastToDecHex (div1);
147 _val1 = ToDecHex ((int)value);
150 // Translate an unsigned long to hexadecimal digits.
151 private void InitDecHexDigits (ulong value)
153 if (value >= HundredMillion) {
154 long div1 = (long)(value / HundredMillion);
155 value -= HundredMillion * (ulong)div1;
156 if (div1 >= HundredMillion) {
157 int div2 = (int)(div1 / HundredMillion);
158 div1 = div1 - div2 * (long)HundredMillion;
159 _val3 = ToDecHex (div2);
162 _val2 = ToDecHex ((int)(div1));
165 _val1 = ToDecHex ((int)value);
168 // Translate a decimal integer to hexadecimal digits.
169 // The decimal integer is 96 digits and its value is hi * 2^64 + lo.
170 // is the lower 64 bits.
171 private void InitDecHexDigits (uint hi, ulong lo)
174 InitDecHexDigits (lo); // Only the lower 64 bits matter.
178 // Compute (hi, lo) = (hi , lo) / HundredMillion.
179 uint divhi = hi / HundredMillion;
180 ulong remhi = hi - divhi * HundredMillion;
181 ulong divlo = lo / HundredMillion;
182 ulong remlo = lo - divlo * HundredMillion + remhi * ULongModHundredMillion;
184 lo = divlo + remhi * ULongDivHundredMillion;
185 divlo = remlo / HundredMillion;
186 remlo -= divlo * HundredMillion;
188 _val1 = ToDecHex ((int)remlo);
190 // Divide hi * 2 ^ 64 + lo by HundredMillion using the fact that
191 // hi < HundredMillion.
192 divlo = lo / HundredMillion;
193 remlo = lo - divlo * HundredMillion;
196 lo += hi * ULongDivHundredMillion;
197 remlo += hi * ULongModHundredMillion;
198 divlo = remlo / HundredMillion;
200 remlo -= divlo * HundredMillion;
202 _val2 = ToDecHex ((int)remlo);
204 // Now we are left with 64 bits store in lo.
205 if (lo >= HundredMillion) {
206 divlo = lo / HundredMillion;
207 lo -= divlo * HundredMillion;
208 _val4 = ToDecHex ((int)divlo);
210 _val3 = ToDecHex ((int)lo);
213 // Helper to translate an int in the range 0 .. 9999 to its
214 // Hexadecimal digits representation.
218 private static uint FastToDecHex (int val)
221 return (uint)DecHexDigits [val];
223 // Uses 2^19 (524288) to compute val / 100 for val < 10000.
224 int v = (val * 5243) >> 19;
225 return (uint)((DecHexDigits [v] << 8) | DecHexDigits [val - v * 100]);
228 // Helper to translate an int in the range 0 .. 99999999 to its
229 // Hexadecimal digits representation.
230 private static uint ToDecHex (int val)
236 res = FastToDecHex (v) << 16;
238 return res | FastToDecHex (val);
241 // Helper to count number of hexadecimal digits in a number.
242 private static int FastDecHexLen (int val)
249 else if (val < 0x1000)
255 private static int DecHexLen (uint val)
258 return FastDecHexLen ((int)val);
259 return 4 + FastDecHexLen ((int)(val >> 16));
262 // Count number of hexadecimal digits stored in _val1 .. _val4.
263 private int DecHexLen ()
266 return DecHexLen (_val4) + 24;
268 return DecHexLen (_val3) + 16;
270 return DecHexLen (_val2) + 8;
272 return DecHexLen (_val1);
277 // Helper to count the 10th scale (number of digits) in a number
278 private static int ScaleOrder (long hi)
280 for (int i = TenPowersListLength - 1; i >= 0; i--)
281 if (hi >= GetTenPowerOf (i))
286 // Compute the initial precision for rounding a floating number
287 // according to the used format.
288 int InitialFloatingPrecision ()
290 if (_specifier == 'R')
291 return _defPrecision + 2;
292 if (_precision < _defPrecision)
293 return _defPrecision;
294 if (_specifier == 'G')
295 return Math.Min (_defPrecision + 2, _precision);
296 if (_specifier == 'E')
297 return Math.Min (_defPrecision + 2, _precision + 1);
298 return _defPrecision;
301 // Parse the given format and extract the precision in it.
302 // Returns -1 for empty formats and -2 to indicate that the format
303 // is a custom format.
304 private static int ParsePrecision (string format)
307 for (int i = 1; i < format.Length; i++) {
308 int val = format [i] - '0';
309 precision = precision * 10 + val;
310 if (val < 0 || val > 9 || precision > 99)
316 #endregion Constructor Helpers
320 // Parse the given format and initialize the following fields:
321 // _isCustomFormat, _specifierIsUpper, _specifier & _precision.
322 public NumberFormatter (Thread current)
324 _cbuf = EmptyArray<char>.Value;
327 CurrentCulture = current.CurrentCulture;
330 private void Init (string format)
332 _val1 = _val2 = _val3 = _val4 = 0;
334 _NaN = _infinity = false;
335 _isCustomFormat = false;
336 _specifierIsUpper = true;
339 if (format == null || format.Length == 0) {
344 char specifier = format [0];
345 if (specifier >= 'a' && specifier <= 'z') {
346 specifier = (char)(specifier - 'a' + 'A');
347 _specifierIsUpper = false;
349 else if (specifier < 'A' || specifier > 'Z') {
350 _isCustomFormat = true;
354 _specifier = specifier;
355 if (format.Length > 1) {
356 _precision = ParsePrecision (format);
357 if (_precision == -2) { // Is it a custom format?
358 _isCustomFormat = true;
365 private void InitHex (ulong value)
367 switch (_defPrecision) {
368 case Int8DefPrecision: value = (byte) value; break;
369 case Int16DefPrecision: value = (ushort) value; break;
370 case Int32DefPrecision: value = (uint) value; break;
373 _val2 = (uint)(value >> 32);
374 _decPointPos = _digitsLen = DecHexLen ();
379 private void Init (string format, int value, int defPrecision)
382 _defPrecision = defPrecision;
383 _positive = value >= 0;
385 if (value == 0 || _specifier == 'X') {
386 InitHex ((ulong)value);
392 InitDecHexDigits ((uint)value);
393 _decPointPos = _digitsLen = DecHexLen ();
396 private void Init (string format, uint value, int defPrecision)
399 _defPrecision = defPrecision;
402 if (value == 0 || _specifier == 'X') {
407 InitDecHexDigits (value);
408 _decPointPos = _digitsLen = DecHexLen ();
411 private void Init (string format, long value)
414 _defPrecision = Int64DefPrecision;
415 _positive = value >= 0;
417 if (value == 0 || _specifier == 'X') {
418 InitHex ((ulong)value);
424 InitDecHexDigits ((ulong)value);
425 _decPointPos = _digitsLen = DecHexLen ();
428 private void Init (string format, ulong value)
431 _defPrecision = UInt64DefPrecision;
434 if (value == 0 || _specifier == 'X') {
435 InitHex ((ulong)value);
439 InitDecHexDigits (value);
440 _decPointPos = _digitsLen = DecHexLen ();
443 #if UNSAFE_TABLES // No unsafe code under TARGET_JVM
446 private void Init (string format, double value, int defPrecision)
450 _defPrecision = defPrecision;
451 long bits = BitConverter.DoubleToInt64Bits (value);
452 _positive = bits >= 0;
453 bits &= Int64.MaxValue;
461 int e = (int)(bits >> DoubleBitsExponentShift);
462 long m = bits & DoubleBitsMantissaMask;
463 if (e == DoubleBitsExponentMask) {
471 // We need 'm' to be large enough so we won't lose precision.
473 int scale = ScaleOrder (m);
474 if (scale < DoubleDefPrecision) {
475 expAdjust = scale - DoubleDefPrecision;
476 m *= GetTenPowerOf (-expAdjust);
480 m = (m + DoubleBitsMantissaMask + 1) * 10;
484 // multiply the mantissa by 10 ^ N
486 ulong hi = (ulong)m >> 32;
487 ulong lo2 = MantissaBitsTable [e];
488 ulong hi2 = lo2 >> 32;
490 ulong mm = hi * lo2 + lo * hi2 + ((lo * lo2) >> 32);
491 long res = (long)(hi * hi2 + (mm >> 32));
492 while (res < SeventeenDigitsThreshold) {
493 mm = (mm & UInt32.MaxValue) * 10;
494 res = res * 10 + (long)(mm >> 32);
497 if ((mm & 0x80000000) != 0)
500 int order = DoubleDefPrecision + 2;
501 _decPointPos = TensExponentTable [e] + expAdjust + order;
503 // Rescale 'res' to the initial precision (15-17 for doubles).
504 int initialPrecision = InitialFloatingPrecision ();
505 if (order > initialPrecision) {
506 long val = GetTenPowerOf (order - initialPrecision);
507 res = (res + (val >> 1)) / val;
508 order = initialPrecision;
510 if (res >= GetTenPowerOf (order)) {
515 InitDecHexDigits ((ulong)res);
516 _offset = CountTrailingZeros ();
517 _digitsLen = order - _offset;
520 private void Init (string format, decimal value)
523 _defPrecision = DecimalDefPrecision;
525 int[] bits = decimal.GetBits (value);
526 int scale = (bits [3] & DecimalBitsScaleMask) >> 16;
527 _positive = bits [3] >= 0;
528 if (bits [0] == 0 && bits [1] == 0 && bits [2] == 0) {
529 _decPointPos = -scale;
535 InitDecHexDigits ((uint)bits [2], ((ulong)bits [1] << 32) | (uint)bits [0]);
536 _digitsLen = DecHexLen ();
537 _decPointPos = _digitsLen - scale;
538 if (_precision != -1 || _specifier != 'G') {
539 _offset = CountTrailingZeros ();
540 _digitsLen -= _offset;
544 #endregion Constructors
546 #region Inner String Buffer
548 //_cbuf moved to before other fields to improve layout
551 private void ResetCharBuf (int size)
554 if (_cbuf.Length < size)
555 _cbuf = new char [size];
558 private void Resize (int len)
560 Array.Resize (ref _cbuf, len);
563 private void Append (char c)
565 if (_ind == _cbuf.Length)
570 private void Append (char c, int cnt)
572 if (_ind + cnt > _cbuf.Length)
573 Resize (_ind + cnt + 10);
578 private void Append (string s)
581 if (_ind + slen > _cbuf.Length)
582 Resize (_ind + slen + 10);
583 for (int i = 0; i < slen; i++)
584 _cbuf [_ind++] = s [i];
587 #endregion Inner String Buffer
589 #region Helper properties
591 private NumberFormatInfo GetNumberFormatInstance (IFormatProvider fp)
593 if (_nfi != null && fp == null)
595 return NumberFormatInfo.GetInstance (fp);
598 public CultureInfo CurrentCulture {
600 if (value != null && value.IsReadOnly)
601 _nfi = value.NumberFormat;
607 private int IntegerDigits {
608 get { return _decPointPos > 0 ? _decPointPos : 1; }
611 private int DecimalDigits {
612 get { return _digitsLen > _decPointPos ? _digitsLen - _decPointPos : 0; }
615 private bool IsFloatingSource {
616 get { return _defPrecision == DoubleDefPrecision || _defPrecision == SingleDefPrecision; }
619 private bool IsZero {
620 get { return _digitsLen == 0; }
623 private bool IsZeroInteger {
624 get { return _digitsLen == 0 || _decPointPos <= 0; }
627 #endregion Helper properties
631 private void RoundPos (int pos)
633 RoundBits (_digitsLen - pos);
636 private bool RoundDecimal (int decimals)
638 return RoundBits (_digitsLen - _decPointPos - decimals);
641 private bool RoundBits (int shift)
646 if (shift > _digitsLen) {
649 _val1 = _val2 = _val3 = _val4 = 0;
654 _digitsLen += _offset;
663 shift = (shift - 1) << 2;
664 uint v = _val1 >> shift;
665 uint rem16 = v & 0xf;
666 _val1 = (v ^ rem16) << shift;
669 _val1 |= 0x99999999 >> (28 - shift);
671 int newlen = DecHexLen ();
672 res = newlen != _digitsLen;
673 _decPointPos = _decPointPos + newlen - _digitsLen;
676 RemoveTrailingZeros ();
680 private void RemoveTrailingZeros ()
682 _offset = CountTrailingZeros ();
683 _digitsLen -= _offset;
684 if (_digitsLen == 0) {
691 private void AddOneToDecHex ()
693 if (_val1 == 0x99999999) {
695 if (_val2 == 0x99999999) {
697 if (_val3 == 0x99999999) {
699 _val4 = AddOneToDecHex (_val4);
702 _val3 = AddOneToDecHex (_val3);
705 _val2 = AddOneToDecHex (_val2);
708 _val1 = AddOneToDecHex (_val1);
711 // Assume val != 0x99999999
712 private static uint AddOneToDecHex (uint val)
714 if ((val & 0xffff) == 0x9999)
715 if ((val & 0xffffff) == 0x999999)
716 if ((val & 0xfffffff) == 0x9999999)
717 return val + 0x06666667;
719 return val + 0x00666667;
720 else if ((val & 0xfffff) == 0x99999)
721 return val + 0x00066667;
723 return val + 0x00006667;
724 else if ((val & 0xff) == 0x99)
725 if ((val & 0xfff) == 0x999)
726 return val + 0x00000667;
728 return val + 0x00000067;
729 else if ((val & 0xf) == 0x9)
730 return val + 0x00000007;
735 private int CountTrailingZeros ()
738 return CountTrailingZeros (_val1);
740 return CountTrailingZeros (_val2) + 8;
742 return CountTrailingZeros (_val3) + 16;
744 return CountTrailingZeros (_val4) + 24;
748 private static int CountTrailingZeros (uint val)
750 if ((val & 0xffff) == 0)
751 if ((val & 0xffffff) == 0)
752 if ((val & 0xfffffff) == 0)
756 else if ((val & 0xfffff) == 0)
760 else if ((val & 0xff) == 0)
761 if ((val & 0xfff) == 0)
765 else if ((val & 0xf) == 0)
773 #region public number formatting methods
776 static NumberFormatter threadNumberFormatter;
778 static NumberFormatter userFormatProvider;
780 private static NumberFormatter GetInstance (IFormatProvider fp)
783 if (userFormatProvider == null) {
784 Interlocked.CompareExchange (ref userFormatProvider, new NumberFormatter (null), null);
787 return userFormatProvider;
790 NumberFormatter res = threadNumberFormatter;
791 threadNumberFormatter = null;
793 return new NumberFormatter (Thread.CurrentThread);
794 res.CurrentCulture = Thread.CurrentThread.CurrentCulture;
798 private void Release()
800 if (this != userFormatProvider)
801 threadNumberFormatter = this;
804 public static string NumberToString (string format, sbyte value, IFormatProvider fp)
806 NumberFormatter inst = GetInstance (fp);
807 inst.Init (format, value, Int8DefPrecision);
808 string res = inst.IntegerToString (format, fp);
813 public static string NumberToString (string format, byte value, IFormatProvider fp)
815 NumberFormatter inst = GetInstance (fp);
816 inst.Init (format, value, UInt8DefPrecision);
817 string res = inst.IntegerToString (format, fp);
822 public static string NumberToString (string format, ushort value, IFormatProvider fp)
824 NumberFormatter inst = GetInstance (fp);
825 inst.Init (format, value, Int16DefPrecision);
826 string res = inst.IntegerToString (format, fp);
831 public static string NumberToString (string format, short value, IFormatProvider fp)
833 NumberFormatter inst = GetInstance (fp);
834 inst.Init (format, value, UInt16DefPrecision);
835 string res = inst.IntegerToString (format, fp);
840 public static string NumberToString (string format, uint value, IFormatProvider fp)
842 NumberFormatter inst = GetInstance (fp);
843 inst.Init (format, value, Int32DefPrecision);
844 string res = inst.IntegerToString (format, fp);
849 public static string NumberToString (string format, int value, IFormatProvider fp)
851 NumberFormatter inst = GetInstance (fp);
852 inst.Init (format, value, UInt32DefPrecision);
853 string res = inst.IntegerToString (format, fp);
858 public static string NumberToString (string format, ulong value, IFormatProvider fp)
860 NumberFormatter inst = GetInstance (fp);
861 inst.Init (format, value);
862 string res = inst.IntegerToString (format, fp);
867 public static string NumberToString (string format, long value, IFormatProvider fp)
869 NumberFormatter inst = GetInstance (fp);
870 inst.Init (format, value);
871 string res = inst.IntegerToString (format, fp);
876 public static string NumberToString (string format, float value, IFormatProvider fp)
878 NumberFormatter inst = GetInstance (fp);
879 inst.Init (format, value, SingleDefPrecision);
880 NumberFormatInfo nfi = inst.GetNumberFormatInstance (fp);
884 else if (inst._infinity)
886 res = nfi.PositiveInfinitySymbol;
888 res = nfi.NegativeInfinitySymbol;
889 else if (inst._specifier == 'R')
890 res = inst.FormatRoundtrip (value, nfi);
892 res = inst.NumberToString (format, nfi);
897 public static string NumberToString (string format, double value, IFormatProvider fp)
899 NumberFormatter inst = GetInstance (fp);
900 inst.Init (format, value, DoubleDefPrecision);
901 NumberFormatInfo nfi = inst.GetNumberFormatInstance (fp);
905 else if (inst._infinity)
907 res = nfi.PositiveInfinitySymbol;
909 res = nfi.NegativeInfinitySymbol;
910 else if (inst._specifier == 'R')
911 res = inst.FormatRoundtrip (value, nfi);
913 res = inst.NumberToString (format, nfi);
918 public static string NumberToString (string format, decimal value, IFormatProvider fp)
920 NumberFormatter inst = GetInstance (fp);
921 inst.Init (format, value);
922 string res = inst.NumberToString (format, inst.GetNumberFormatInstance (fp));
927 public static string NumberToString (uint value, IFormatProvider fp)
929 if (value >= HundredMillion)
930 return NumberToString (null, value, fp);
932 NumberFormatter inst = GetInstance (fp);
933 string res = inst.FastIntegerToString ((int)value, fp);
938 public static string NumberToString (int value, IFormatProvider fp)
940 if (value >= HundredMillion || value <= -HundredMillion)
941 return NumberToString (null, value, fp);
943 NumberFormatter inst = GetInstance (fp);
944 string res = inst.FastIntegerToString (value, fp);
949 public static string NumberToString (ulong value, IFormatProvider fp)
951 if (value >= HundredMillion)
952 return NumberToString (null, value, fp);
954 NumberFormatter inst = GetInstance (fp);
955 string res = inst.FastIntegerToString ((int)value, fp);
960 public static string NumberToString (long value, IFormatProvider fp)
962 if (value >= HundredMillion || value <= -HundredMillion)
963 return NumberToString (null, value, fp);
965 NumberFormatter inst = GetInstance (fp);
966 string res = inst.FastIntegerToString ((int)value, fp);
971 public static string NumberToString (float value, IFormatProvider fp)
973 NumberFormatter inst = GetInstance (fp);
974 inst.Init (null, value, SingleDefPrecision);
975 NumberFormatInfo nfi = inst.GetNumberFormatInstance (fp);
979 else if (inst._infinity)
981 res = nfi.PositiveInfinitySymbol;
983 res = nfi.NegativeInfinitySymbol;
985 res = inst.FormatGeneral (-1, nfi);
990 public static string NumberToString (double value, IFormatProvider fp)
992 NumberFormatter inst = GetInstance (fp);
993 NumberFormatInfo nfi = inst.GetNumberFormatInstance (fp);
994 inst.Init (null, value, DoubleDefPrecision);
998 else if (inst._infinity)
1000 res = nfi.PositiveInfinitySymbol;
1002 res = nfi.NegativeInfinitySymbol;
1004 res = inst.FormatGeneral (-1, nfi);
1009 private string FastIntegerToString (int value, IFormatProvider fp)
1012 string sign = GetNumberFormatInstance(fp).NegativeSign;
1013 ResetCharBuf (8 + sign.Length);
1020 if (value >= 10000) {
1021 int v = value / 10000;
1022 FastAppendDigits (v, false);
1023 FastAppendDigits (value - v * 10000, true);
1026 FastAppendDigits (value, false);
1028 return new string (_cbuf, 0, _ind);
1031 private string IntegerToString (string format, IFormatProvider fp)
1033 NumberFormatInfo nfi = GetNumberFormatInstance (fp);
1034 switch (_specifier) {
1036 return FormatCurrency (_precision, nfi);
1038 return FormatDecimal (_precision, nfi);
1040 return FormatExponential (_precision, nfi);
1042 return FormatFixedPoint (_precision, nfi);
1044 if (_precision <= 0)
1045 return FormatDecimal (-1, nfi);
1046 return FormatGeneral (_precision, nfi);
1048 return FormatNumber (_precision, nfi);
1050 return FormatPercent (_precision, nfi);
1052 return FormatHexadecimal (_precision);
1054 if (_isCustomFormat)
1055 return FormatCustom (format, nfi);
1056 throw new FormatException ("The specified format '" + format + "' is invalid");
1060 private string NumberToString (string format, NumberFormatInfo nfi)
1062 switch (_specifier) {
1064 return FormatCurrency (_precision, nfi);
1066 return FormatExponential (_precision, nfi);
1068 return FormatFixedPoint (_precision, nfi);
1070 return FormatGeneral (_precision, nfi);
1072 return FormatNumber (_precision, nfi);
1074 return FormatPercent (_precision, nfi);
1077 if (_isCustomFormat)
1078 return FormatCustom (format, nfi);
1079 throw new FormatException ("The specified format '" + format + "' is invalid");
1083 public string FormatCurrency (int precision, NumberFormatInfo nfi)
1085 precision = (precision >= 0 ? precision : nfi.CurrencyDecimalDigits);
1086 RoundDecimal (precision);
1087 ResetCharBuf (IntegerDigits * 2 + precision * 2 + 16);
1090 switch (nfi.CurrencyPositivePattern) {
1092 Append (nfi.CurrencySymbol);
1095 Append (nfi.CurrencySymbol);
1101 switch (nfi.CurrencyNegativePattern) {
1104 Append (nfi.CurrencySymbol);
1107 Append (nfi.NegativeSign);
1108 Append (nfi.CurrencySymbol);
1111 Append (nfi.CurrencySymbol);
1112 Append (nfi.NegativeSign);
1115 Append (nfi.CurrencySymbol);
1121 Append (nfi.NegativeSign);
1124 Append (nfi.NegativeSign);
1127 Append (nfi.NegativeSign);
1128 Append (nfi.CurrencySymbol);
1132 Append (nfi.CurrencySymbol);
1136 Append (nfi.CurrencySymbol);
1138 Append (nfi.NegativeSign);
1142 Append (nfi.CurrencySymbol);
1151 AppendIntegerStringWithGroupSeparator (nfi.RawCurrencyGroupSizes, nfi.CurrencyGroupSeparator);
1153 if (precision > 0) {
1154 Append (nfi.CurrencyDecimalSeparator);
1155 AppendDecimalString (precision);
1159 switch (nfi.CurrencyPositivePattern) {
1161 Append (nfi.CurrencySymbol);
1165 Append (nfi.CurrencySymbol);
1170 switch (nfi.CurrencyNegativePattern) {
1175 Append (nfi.NegativeSign);
1178 Append (nfi.CurrencySymbol);
1182 Append (nfi.CurrencySymbol);
1185 Append (nfi.NegativeSign);
1186 Append (nfi.CurrencySymbol);
1189 Append (nfi.CurrencySymbol);
1190 Append (nfi.NegativeSign);
1194 Append (nfi.CurrencySymbol);
1198 Append (nfi.CurrencySymbol);
1199 Append (nfi.NegativeSign);
1202 Append (nfi.NegativeSign);
1205 Append (nfi.NegativeSign);
1207 Append (nfi.CurrencySymbol);
1214 Append (nfi.CurrencySymbol);
1220 return new string (_cbuf, 0, _ind);
1223 private string FormatDecimal (int precision, NumberFormatInfo nfi)
1225 if (precision < _digitsLen)
1226 precision = _digitsLen;
1230 ResetCharBuf (precision + 1);
1232 Append (nfi.NegativeSign);
1233 AppendDigits (0, precision);
1235 return new string (_cbuf, 0, _ind);
1238 #if UNSAFE_TABLES // No unsafe code under TARGET_JVM
1241 private string FormatHexadecimal (int precision)
1243 int size = Math.Max (precision, _decPointPos);
1245 char* digits = _specifierIsUpper ? DigitUpperTable : DigitLowerTable;
1247 char[] digits = _specifierIsUpper ? DigitUpperTable : DigitLowerTable;
1249 ResetCharBuf (size);
1251 ulong val = _val1 | ((ulong)_val2 << 32);
1253 _cbuf [--size] = digits [val & 0xf];
1256 return new string (_cbuf, 0, _ind);
1259 public string FormatFixedPoint (int precision, NumberFormatInfo nfi)
1261 if (precision == -1)
1262 precision = nfi.NumberDecimalDigits;
1264 RoundDecimal (precision);
1266 ResetCharBuf (IntegerDigits + precision + 2);
1269 Append (nfi.NegativeSign);
1271 AppendIntegerString (IntegerDigits);
1273 if (precision > 0) {
1274 Append (nfi.NumberDecimalSeparator);
1275 AppendDecimalString (precision);
1278 return new string (_cbuf, 0, _ind);
1281 private string FormatRoundtrip (double origval, NumberFormatInfo nfi)
1283 NumberFormatter nfc = GetClone ();
1284 if (origval >= MinRoundtripVal && origval <= MaxRoundtripVal) {
1285 string shortRep = FormatGeneral (_defPrecision, nfi);
1286 if (origval == Double.Parse (shortRep, nfi))
1289 return nfc.FormatGeneral (_defPrecision + 2, nfi);
1292 private string FormatRoundtrip (float origval, NumberFormatInfo nfi)
1294 NumberFormatter nfc = GetClone ();
1295 string shortRep = FormatGeneral (_defPrecision, nfi);
1296 // Check roundtrip only for "normal" double values.
1297 if (origval == Single.Parse (shortRep, nfi))
1299 return nfc.FormatGeneral (_defPrecision + 2, nfi);
1302 private string FormatGeneral (int precision, NumberFormatInfo nfi)
1305 if (precision == -1) {
1306 enableExp = IsFloatingSource;
1307 precision = _defPrecision;
1312 precision = _defPrecision;
1313 RoundPos (precision);
1316 int intDigits = _decPointPos;
1317 int digits = _digitsLen;
1318 int decDigits = digits - intDigits;
1320 if ((intDigits > precision || intDigits <= -4) && enableExp)
1321 return FormatExponential (digits - 1, nfi, 2);
1327 ResetCharBuf (decDigits + intDigits + 3);
1330 Append (nfi.NegativeSign);
1335 AppendDigits (digits - intDigits, digits);
1337 if (decDigits > 0) {
1338 Append (nfi.NumberDecimalSeparator);
1339 AppendDigits (0, decDigits);
1342 return new string (_cbuf, 0, _ind);
1345 public string FormatNumber (int precision, NumberFormatInfo nfi)
1347 precision = (precision >= 0 ? precision : nfi.NumberDecimalDigits);
1348 ResetCharBuf (IntegerDigits * 3 + precision);
1349 RoundDecimal (precision);
1352 switch (nfi.NumberNegativePattern) {
1357 Append (nfi.NegativeSign);
1360 Append (nfi.NegativeSign);
1366 AppendIntegerStringWithGroupSeparator (nfi.RawNumberGroupSizes, nfi.NumberGroupSeparator);
1368 if (precision > 0) {
1369 Append (nfi.NumberDecimalSeparator);
1370 AppendDecimalString (precision);
1374 switch (nfi.NumberNegativePattern) {
1379 Append (nfi.NegativeSign);
1383 Append (nfi.NegativeSign);
1388 return new string (_cbuf, 0, _ind);
1391 public string FormatPercent (int precision, NumberFormatInfo nfi)
1393 precision = (precision >= 0 ? precision : nfi.PercentDecimalDigits);
1395 RoundDecimal (precision);
1396 ResetCharBuf (IntegerDigits * 2 + precision + 16);
1399 if (nfi.PercentPositivePattern == 2)
1400 Append (nfi.PercentSymbol);
1403 switch (nfi.PercentNegativePattern) {
1405 Append (nfi.NegativeSign);
1408 Append (nfi.NegativeSign);
1411 Append (nfi.NegativeSign);
1412 Append (nfi.PercentSymbol);
1417 AppendIntegerStringWithGroupSeparator (nfi.RawPercentGroupSizes, nfi.PercentGroupSeparator);
1419 if (precision > 0) {
1420 Append (nfi.PercentDecimalSeparator);
1421 AppendDecimalString (precision);
1425 switch (nfi.PercentPositivePattern) {
1428 Append (nfi.PercentSymbol);
1431 Append (nfi.PercentSymbol);
1436 switch (nfi.PercentNegativePattern) {
1439 Append (nfi.PercentSymbol);
1442 Append (nfi.PercentSymbol);
1447 return new string (_cbuf, 0, _ind);
1450 public string FormatExponential (int precision, NumberFormatInfo nfi)
1452 if (precision == -1)
1453 precision = DefaultExpPrecision;
1455 RoundPos (precision + 1);
1456 return FormatExponential (precision, nfi, 3);
1459 private string FormatExponential (int precision, NumberFormatInfo nfi, int expDigits)
1461 int decDigits = _decPointPos;
1462 int digits = _digitsLen;
1463 int exponent = decDigits - 1;
1464 decDigits = _decPointPos = 1;
1466 ResetCharBuf (precision + 8);
1469 Append (nfi.NegativeSign);
1471 AppendOneDigit (digits - 1);
1473 if (precision > 0) {
1474 Append (nfi.NumberDecimalSeparator);
1475 AppendDigits (digits - precision - 1, digits - _decPointPos);
1478 AppendExponent (nfi, exponent, expDigits);
1480 return new string (_cbuf, 0, _ind);
1483 public string FormatCustom (string format, NumberFormatInfo nfi)
1488 CustomInfo.GetActiveSection (format, ref p, IsZero, ref offset, ref length);
1490 return _positive ? string.Empty : nfi.NegativeSign;
1493 CustomInfo info = CustomInfo.Parse (format, offset, length, nfi);
1495 Console.WriteLine ("Format : {0}",format);
1496 Console.WriteLine ("DecimalDigits : {0}",info.DecimalDigits);
1497 Console.WriteLine ("DecimalPointPos : {0}",info.DecimalPointPos);
1498 Console.WriteLine ("DecimalTailSharpDigits : {0}",info.DecimalTailSharpDigits);
1499 Console.WriteLine ("IntegerDigits : {0}",info.IntegerDigits);
1500 Console.WriteLine ("IntegerHeadSharpDigits : {0}",info.IntegerHeadSharpDigits);
1501 Console.WriteLine ("IntegerHeadPos : {0}",info.IntegerHeadPos);
1502 Console.WriteLine ("UseExponent : {0}",info.UseExponent);
1503 Console.WriteLine ("ExponentDigits : {0}",info.ExponentDigits);
1504 Console.WriteLine ("ExponentTailSharpDigits : {0}",info.ExponentTailSharpDigits);
1505 Console.WriteLine ("ExponentNegativeSignOnly : {0}",info.ExponentNegativeSignOnly);
1506 Console.WriteLine ("DividePlaces : {0}",info.DividePlaces);
1507 Console.WriteLine ("Percents : {0}",info.Percents);
1508 Console.WriteLine ("Permilles : {0}",info.Permilles);
1510 StringBuilder sb_int = new StringBuilder (info.IntegerDigits * 2);
1511 StringBuilder sb_dec = new StringBuilder (info.DecimalDigits * 2);
1512 StringBuilder sb_exp = (info.UseExponent ? new StringBuilder (info.ExponentDigits * 2) : null);
1515 if (info.Percents > 0)
1516 Multiply10(2 * info.Percents);
1517 if (info.Permilles > 0)
1518 Multiply10(3 * info.Permilles);
1519 if (info.DividePlaces > 0)
1520 Divide10(info.DividePlaces);
1522 bool expPositive = true;
1523 if (info.UseExponent && (info.DecimalDigits > 0 || info.IntegerDigits > 0)) {
1525 RoundPos (info.DecimalDigits + info.IntegerDigits);
1526 diff -= _decPointPos - info.IntegerDigits;
1527 _decPointPos = info.IntegerDigits;
1530 expPositive = diff <= 0;
1531 AppendNonNegativeNumber (sb_exp, diff < 0 ? -diff : diff);
1534 RoundDecimal (info.DecimalDigits);
1536 if (info.IntegerDigits != 0 || !IsZeroInteger)
1537 AppendIntegerString (IntegerDigits, sb_int);
1539 AppendDecimalString (DecimalDigits, sb_dec);
1541 if (info.UseExponent) {
1542 if (info.DecimalDigits <= 0 && info.IntegerDigits <= 0)
1545 if (sb_int.Length < info.IntegerDigits)
1546 sb_int.Insert (0, "0", info.IntegerDigits - sb_int.Length);
1548 while (sb_exp.Length < info.ExponentDigits - info.ExponentTailSharpDigits)
1549 sb_exp.Insert (0, '0');
1551 if (expPositive && !info.ExponentNegativeSignOnly)
1552 sb_exp.Insert (0, nfi.PositiveSign);
1553 else if (!expPositive)
1554 sb_exp.Insert (0, nfi.NegativeSign);
1557 if (sb_int.Length < info.IntegerDigits - info.IntegerHeadSharpDigits)
1558 sb_int.Insert (0, "0", info.IntegerDigits - info.IntegerHeadSharpDigits - sb_int.Length);
1559 if (info.IntegerDigits == info.IntegerHeadSharpDigits && IsZeroOnly (sb_int))
1560 sb_int.Remove (0, sb_int.Length);
1563 ZeroTrimEnd (sb_dec, true);
1564 while (sb_dec.Length < info.DecimalDigits - info.DecimalTailSharpDigits)
1565 sb_dec.Append ('0');
1566 if (sb_dec.Length > info.DecimalDigits)
1567 sb_dec.Remove (info.DecimalDigits, sb_dec.Length - info.DecimalDigits);
1569 return info.Format (format, offset, length, nfi, _positive, sb_int, sb_dec, sb_exp);
1571 #endregion public number formatting methods
1573 #region StringBuilder formatting helpers
1575 private static void ZeroTrimEnd (StringBuilder sb, bool canEmpty)
1578 for (int i = sb.Length - 1; (canEmpty ? i >= 0 : i > 0); i--) {
1585 sb.Remove (sb.Length - len, len);
1588 private static bool IsZeroOnly (StringBuilder sb)
1590 for (int i = 0; i < sb.Length; i++)
1591 if (char.IsDigit (sb [i]) && sb [i] != '0')
1596 private static void AppendNonNegativeNumber (StringBuilder sb, int v)
1599 throw new ArgumentException ();
1601 int i = ScaleOrder (v) - 1;
1603 int n = v / (int)GetTenPowerOf (i);
1604 sb.Append ((char)('0' | n));
1605 v -= (int)GetTenPowerOf (i--) * n;
1609 #endregion StringBuilder formatting helpers
1611 #region Append helpers
1613 private void AppendIntegerString (int minLength, StringBuilder sb)
1615 if (_decPointPos <= 0) {
1616 sb.Append ('0', minLength);
1620 if (_decPointPos < minLength)
1621 sb.Append ('0', minLength - _decPointPos);
1623 AppendDigits (_digitsLen - _decPointPos, _digitsLen, sb);
1626 private void AppendIntegerString (int minLength)
1628 if (_decPointPos <= 0) {
1629 Append ('0', minLength);
1633 if (_decPointPos < minLength)
1634 Append ('0', minLength - _decPointPos);
1636 AppendDigits (_digitsLen - _decPointPos, _digitsLen);
1639 private void AppendDecimalString (int precision, StringBuilder sb)
1641 AppendDigits (_digitsLen - precision - _decPointPos, _digitsLen - _decPointPos, sb);
1644 private void AppendDecimalString (int precision)
1646 AppendDigits (_digitsLen - precision - _decPointPos, _digitsLen - _decPointPos);
1649 private void AppendIntegerStringWithGroupSeparator (int[] groups, string groupSeparator)
1651 if (IsZeroInteger) {
1658 for (int i = 0; i < groups.Length; i++) {
1659 total += groups [i];
1660 if (total <= _decPointPos)
1666 if (groups.Length > 0 && total > 0) {
1668 int groupSize = groups [groupIndex];
1669 int fraction = _decPointPos > total ? _decPointPos - total : 0;
1670 if (groupSize == 0) {
1671 while (groupIndex >= 0 && groups [groupIndex] == 0)
1674 groupSize = fraction > 0 ? fraction : groups [groupIndex];
1677 counter = groupSize;
1679 groupIndex += fraction / groupSize;
1680 counter = fraction % groupSize;
1682 counter = groupSize;
1687 if (total >= _decPointPos) {
1688 int lastGroupSize = groups [0];
1689 if (total > lastGroupSize) {
1690 int lastGroupDiff = -(lastGroupSize - _decPointPos);
1693 if (lastGroupDiff < lastGroupSize)
1694 counter = lastGroupDiff;
1695 else if (lastGroupSize > 0 && (lastGroupMod = _decPointPos % lastGroupSize) > 0)
1696 counter = lastGroupMod;
1700 for (int i = 0; ;) {
1701 if ((_decPointPos - i) <= counter || counter == 0) {
1702 AppendDigits (_digitsLen - _decPointPos, _digitsLen - i);
1705 AppendDigits (_digitsLen - i - counter, _digitsLen - i);
1707 Append (groupSeparator);
1708 if (--groupIndex < groups.Length && groupIndex >= 0)
1709 groupSize = groups [groupIndex];
1710 counter = groupSize;
1714 AppendDigits (_digitsLen - _decPointPos, _digitsLen);
1718 // minDigits is in the range 1..3
1719 private void AppendExponent (NumberFormatInfo nfi, int exponent, int minDigits)
1721 if (_specifierIsUpper || _specifier == 'R')
1727 Append (nfi.PositiveSign);
1729 Append (nfi.NegativeSign);
1730 exponent = -exponent;
1734 Append ('0', minDigits);
1735 else if (exponent < 10) {
1736 Append ('0', minDigits - 1);
1737 Append ((char)('0' | exponent));
1740 uint hexDigit = FastToDecHex (exponent);
1741 if (exponent >= 100 || minDigits == 3)
1742 Append ((char)('0' | (hexDigit >> 8)));
1743 Append ((char)('0' | ((hexDigit >> 4) & 0xf)));
1744 Append ((char)('0' | (hexDigit & 0xf)));
1748 private void AppendOneDigit (int start)
1750 if (_ind == _cbuf.Length)
1759 else if (start < 16)
1761 else if (start < 24)
1763 else if (start < 32)
1767 v >>= (start & 0x7) << 2;
1768 _cbuf [_ind++] = (char)('0' | v & 0xf);
1771 #if UNSAFE_TABLES // No unsafe code under TARGET_JVM
1774 private void FastAppendDigits (int val, bool force)
1778 if (force || val >= 100) {
1779 int v = (val * 5243) >> 19;
1780 digits = DecHexDigits [v];
1781 if (force || val >= 1000)
1782 _cbuf [i++] = (char)('0' | digits >> 4);
1783 _cbuf [i++] = (char)('0' | (digits & 0xf));
1784 digits = DecHexDigits [val - v * 100];
1787 digits = DecHexDigits [val];
1789 if (force || val >= 10)
1790 _cbuf [i++] = (char)('0' | digits >> 4);
1791 _cbuf [i++] = (char)('0' | (digits & 0xf));
1795 private void AppendDigits (int start, int end)
1800 int i = _ind + (end - start);
1801 if (i > _cbuf.Length)
1808 for (int next = start + 8 - (start & 0x7); ; start = next, next += 8) {
1812 else if (next == 16)
1814 else if (next == 24)
1816 else if (next == 32)
1820 v >>= (start & 0x7) << 2;
1824 _cbuf [--i] = (char)('0' | v & 0xf);
1825 switch (next - start) {
1827 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1830 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1833 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1836 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1839 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1842 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1845 _cbuf [--i] = (char)('0' | (v >>= 4) & 0xf);
1855 private void AppendDigits (int start, int end, StringBuilder sb)
1860 int i = sb.Length + (end - start);
1866 for (int next = start + 8 - (start & 0x7); ; start = next, next += 8) {
1870 else if (next == 16)
1872 else if (next == 24)
1874 else if (next == 32)
1878 v >>= (start & 0x7) << 2;
1881 sb [--i] = (char)('0' | v & 0xf);
1882 switch (next - start) {
1884 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1887 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1890 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1893 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1896 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1899 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1902 sb [--i] = (char)('0' | (v >>= 4) & 0xf);
1912 #endregion Append helpers
1916 private void Multiply10(int count)
1918 if (count <= 0 || _digitsLen == 0)
1921 _decPointPos += count;
1924 private void Divide10(int count)
1926 if (count <= 0 || _digitsLen == 0)
1929 _decPointPos -= count;
1932 private NumberFormatter GetClone ()
1934 return (NumberFormatter)this.MemberwiseClone ();
1941 private class CustomInfo
1943 public bool UseGroup = false;
1944 public int DecimalDigits = 0;
1945 public int DecimalPointPos = -1;
1946 public int DecimalTailSharpDigits = 0;
1947 public int IntegerDigits = 0;
1948 public int IntegerHeadSharpDigits = 0;
1949 public int IntegerHeadPos = 0;
1950 public bool UseExponent = false;
1951 public int ExponentDigits = 0;
1952 public int ExponentTailSharpDigits = 0;
1953 public bool ExponentNegativeSignOnly = true;
1954 public int DividePlaces = 0;
1955 public int Percents = 0;
1956 public int Permilles = 0;
1958 public static void GetActiveSection (string format, ref bool positive, bool zero, ref int offset, ref int length)
1960 int[] lens = new int [3];
1963 bool quoted = false;
1965 for (int i = 0; i < format.Length; i++) {
1966 char c = format [i];
1968 if (c == '\"' || c == '\'') {
1969 if (i == 0 || format [i - 1] != '\\')
1975 if (c == ';' && !quoted && (i == 0 || format [i - 1] != '\\')) {
1976 lens [index++] = i - lastPos;
1985 length = format.Length;
1989 if (positive || zero) {
1994 if (lens [0] + 1 < format.Length) {
1996 offset = lens [0] + 1;
1997 length = format.Length - offset;
2008 offset = lens [0] + lens [1] + 2;
2009 length = format.Length - offset;
2019 offset = lens [0] + 1;
2031 offset = lens [0] + lens [1] + 2;
2042 offset = lens [0] + 1;
2053 throw new ArgumentException ();
2056 public static CustomInfo Parse (string format, int offset, int length, NumberFormatInfo nfi)
2058 char literal = '\0';
2059 bool integerArea = true;
2060 bool decimalArea = false;
2061 bool exponentArea = false;
2062 bool sharpContinues = true;
2064 CustomInfo info = new CustomInfo ();
2065 int groupSeparatorCounter = 0;
2067 for (int i = offset; i - offset < length; i++) {
2068 char c = format [i];
2070 if (c == literal && c != '\0') {
2074 if (literal != '\0')
2077 if (exponentArea && (c != '\0' && c != '0' && c != '#')) {
2078 exponentArea = false;
2079 integerArea = (info.DecimalPointPos < 0);
2080 decimalArea = !integerArea;
2091 if (c == '\"' || c == '\'') {
2096 if (sharpContinues && integerArea)
2097 info.IntegerHeadSharpDigits++;
2098 else if (decimalArea)
2099 info.DecimalTailSharpDigits++;
2100 else if (exponentArea)
2101 info.ExponentTailSharpDigits++;
2106 sharpContinues = false;
2108 info.DecimalTailSharpDigits = 0;
2109 else if (exponentArea)
2110 info.ExponentTailSharpDigits = 0;
2112 if (info.IntegerHeadPos == -1)
2113 info.IntegerHeadPos = i;
2116 info.IntegerDigits++;
2117 if (groupSeparatorCounter > 0)
2118 info.UseGroup = true;
2119 groupSeparatorCounter = 0;
2121 else if (decimalArea)
2122 info.DecimalDigits++;
2123 else if (exponentArea)
2124 info.ExponentDigits++;
2128 if (info.UseExponent)
2131 info.UseExponent = true;
2132 integerArea = false;
2133 decimalArea = false;
2134 exponentArea = true;
2135 if (i + 1 - offset < length) {
2136 char nc = format [i + 1];
2138 info.ExponentNegativeSignOnly = false;
2139 if (nc == '+' || nc == '-')
2141 else if (nc != '0' && nc != '#') {
2142 info.UseExponent = false;
2143 if (info.DecimalPointPos < 0)
2150 integerArea = false;
2152 exponentArea = false;
2153 if (info.DecimalPointPos == -1)
2154 info.DecimalPointPos = i;
2163 if (integerArea && info.IntegerDigits > 0)
2164 groupSeparatorCounter++;
2171 if (info.ExponentDigits == 0)
2172 info.UseExponent = false;
2174 info.IntegerHeadSharpDigits = 0;
2176 if (info.DecimalDigits == 0)
2177 info.DecimalPointPos = -1;
2179 info.DividePlaces += groupSeparatorCounter * 3;
2184 public string Format (string format, int offset, int length, NumberFormatInfo nfi, bool positive, StringBuilder sb_int, StringBuilder sb_dec, StringBuilder sb_exp)
2186 StringBuilder sb = new StringBuilder ();
2187 char literal = '\0';
2188 bool integerArea = true;
2189 bool decimalArea = false;
2190 int intSharpCounter = 0;
2191 int sb_int_index = 0;
2192 int sb_dec_index = 0;
2194 int[] groups = nfi.RawNumberGroupSizes;
2195 string groupSeparator = nfi.NumberGroupSeparator;
2196 int intLen = 0, total = 0, groupIndex = 0, counter = 0, groupSize = 0;
2197 if (UseGroup && groups.Length > 0) {
2198 intLen = sb_int.Length;
2199 for (int i = 0; i < groups.Length; i++) {
2200 total += groups [i];
2201 if (total <= intLen)
2204 groupSize = groups [groupIndex];
2205 int fraction = intLen > total ? intLen - total : 0;
2206 if (groupSize == 0) {
2207 while (groupIndex >= 0 && groups [groupIndex] == 0)
2210 groupSize = fraction > 0 ? fraction : groups [groupIndex];
2213 counter = groupSize;
2215 groupIndex += fraction / groupSize;
2216 counter = fraction % groupSize;
2218 counter = groupSize;
2226 for (int i = offset; i - offset < length; i++) {
2227 char c = format [i];
2229 if (c == literal && c != '\0') {
2233 if (literal != '\0') {
2241 if (i - offset < length)
2242 sb.Append (format [i]);
2246 if (c == '\"' || c == '\'')
2254 if (IntegerDigits - intSharpCounter < sb_int.Length + sb_int_index || c == '0')
2255 while (IntegerDigits - intSharpCounter + sb_int_index < sb_int.Length) {
2256 sb.Append (sb_int [sb_int_index++]);
2257 if (UseGroup && --intLen > 0 && --counter == 0) {
2258 sb.Append (groupSeparator);
2259 if (--groupIndex < groups.Length && groupIndex >= 0)
2260 groupSize = groups [groupIndex];
2261 counter = groupSize;
2266 else if (decimalArea) {
2267 if (sb_dec_index < sb_dec.Length)
2268 sb.Append (sb_dec [sb_dec_index++]);
2276 if (sb_exp == null || !UseExponent) {
2285 for (q = i + 1; q - offset < length; q++) {
2286 if (format [q] == '0') {
2290 if (q == i + 1 && (format [q] == '+' || format [q] == '-'))
2299 integerArea = (DecimalPointPos < 0);
2300 decimalArea = !integerArea;
2311 if (DecimalPointPos == i) {
2312 if (DecimalDigits > 0) {
2313 while (sb_int_index < sb_int.Length)
2314 sb.Append (sb_int [sb_int_index++]);
2316 if (sb_dec.Length > 0)
2317 sb.Append (nfi.NumberDecimalSeparator);
2319 integerArea = false;
2325 sb.Append (nfi.PercentSymbol);
2328 sb.Append (nfi.PerMilleSymbol);
2337 sb.Insert (0, nfi.NegativeSign);
2339 return sb.ToString ();