#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;
// _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)
#region Inner String Buffer
- private char[] _cbuf;
+ //_cbuf moved to before other fields to improve layout
private int _ind;
private void ResetCharBuf (int size)
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)
#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)
#region StringBuilder formatting helpers
- private static void ZeroTrimEnd (StringBuilder sb)
- {
- ZeroTrimEnd (sb, false);
- }
-
private static void ZeroTrimEnd (StringBuilder sb, bool canEmpty)
{
int len = 0;
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);
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)