Merge branch 'master' into msbuilddll2
[mono.git] / mcs / class / corlib / System / NumberFormatter.cs
index 90dec6c3734187705f627672ca2d1d2e7083e9fa..dc0fb5a1e11800578f51294235ee4d2d5884cb05 100644 (file)
@@ -106,9 +106,11 @@ namespace System
 
                #region Fields
 
-               private Thread _thread;
                private NumberFormatInfo _nfi;
 
+               //part of the private stringbuffer
+               private char[] _cbuf;
+
                private bool _NaN;
                private bool _infinity;
                private bool _isCustomFormat;
@@ -319,11 +321,10 @@ namespace System
                //   _isCustomFormat, _specifierIsUpper, _specifier & _precision.
                public NumberFormatter (Thread current)
                {
-                       _cbuf = new char [0];
+                       _cbuf = EmptyArray<char>.Value;
                        if (current == null)
                                return;
-                       _thread = current;
-                       CurrentCulture = _thread.CurrentCulture;
+                       CurrentCulture = current.CurrentCulture;
                }
 
                private void Init (string format)
@@ -544,7 +545,7 @@ namespace System
 
                #region Inner String Buffer
 
-               private char[] _cbuf;
+               //_cbuf moved to before other fields to improve layout
                private int _ind;
 
                private void ResetCharBuf (int size)
@@ -556,9 +557,7 @@ namespace System
 
                private void Resize (int len)
                {
-                       char[] newBuf = new char [len];
-                       Array.Copy (_cbuf, newBuf, _ind);
-                       _cbuf = newBuf;
+                       Array.Resize (ref _cbuf, len);
                }
 
                private void Append (char c)
@@ -773,14 +772,22 @@ namespace System
 
                #region public number formatting methods
 
+               [ThreadStatic]
+               static NumberFormatter threadNumberFormatter;
+
                private static NumberFormatter GetInstance()
                {
-                       return Thread.CurrentThread.AcquireNumberFormatter();
+                       NumberFormatter res = threadNumberFormatter;
+                       threadNumberFormatter = null;
+                       if (res == null)
+                               return new NumberFormatter (Thread.CurrentThread);
+                       res.CurrentCulture = Thread.CurrentThread.CurrentCulture;
+                       return res;
                }
 
                private void Release()
                {
-                       _thread.ReleaseNumberFormatter (this);
+                       threadNumberFormatter = this;
                }
 
                public static string NumberToString (string format, sbyte value, IFormatProvider fp)
@@ -1554,11 +1561,6 @@ namespace System
 
                #region StringBuilder formatting helpers
 
-               private static void ZeroTrimEnd (StringBuilder sb)
-               {
-                       ZeroTrimEnd (sb, false);
-               }
-
                private static void ZeroTrimEnd (StringBuilder sb, bool canEmpty)
                {
                        int len = 0;
@@ -1671,6 +1673,19 @@ namespace System
                                                groupIndex++;
                                }
 
+                               if (total >= _decPointPos) {
+                                       int lastGroupSize = groups [0];
+                                       if (total > lastGroupSize) {
+                                               int lastGroupDiff = -(lastGroupSize - _decPointPos);
+                                               int lastGroupMod;
+
+                                               if (lastGroupDiff < lastGroupSize)
+                                                       counter = lastGroupDiff;
+                                               else if (lastGroupSize > 0 && (lastGroupMod = _decPointPos % lastGroupSize) > 0)
+                                                       counter = lastGroupMod;
+                                       }
+                               }
+                               
                                for (int i = 0; ;) {
                                        if ((_decPointPos - i) <= counter || counter == 0) {
                                                AppendDigits (_digitsLen - _decPointPos, _digitsLen - i);
@@ -1934,19 +1949,19 @@ namespace System
                                int[] lens = new int [3];
                                int index = 0;
                                int lastPos = 0;
-                               char literal = '\0';
+                               bool quoted = false;
+
                                for (int i = 0; i < format.Length; i++) {
                                        char c = format [i];
 
-                                       if (c == literal || (literal == '\0' && (c == '\"' || c == '\''))) {
-                                               if (literal == '\0')
-                                                       literal = c;
-                                               else
-                                                       literal = '\0';
+                                       if (c == '\"' || c == '\'') {
+                                               if (i == 0 || format [i - 1] != '\\')
+                                                       quoted = !quoted;
+
                                                continue;
                                        }
 
-                                       if (literal == '\0' && format [i] == ';' && (i == 0 || format [i - 1] != '\\')) {
+                                       if (c == ';' && !quoted && (i == 0 || format [i - 1] != '\\')) {
                                                lens [index++] = i - lastPos;
                                                lastPos = i + 1;
                                                if (index == 3)