public string Format (string format, int offset, int length, NumberFormatInfo nfi, bool positive, StringBuilder sb_int, StringBuilder sb_dec, StringBuilder sb_exp)
{
StringBuilder sb = new StringBuilder ();
char literal = '\0';
bool integerArea = true;
bool decimalArea = false;
int intSharpCounter = 0;
int sb_int_index = 0;
int sb_dec_index = 0;
int[] groups = nfi.RawNumberGroupSizes;
string groupSeparator = nfi.NumberGroupSeparator;
int intLen = 0, total = 0, groupIndex = 0, counter = 0, groupSize = 0;
if (UseGroup && groups.Length > 0) {
intLen = sb_int.Length;
for (int i = 0; i < groups.Length; i++) {
total += groups [i];
if (total <= intLen)
groupIndex = i;
}
groupSize = groups [groupIndex];
int fraction = intLen > total ? intLen - total : 0;
if (groupSize == 0) {
while (groupIndex >= 0 && groups [groupIndex] == 0)
groupIndex--;
groupSize = fraction > 0 ? fraction : groups [groupIndex];
}
if (fraction == 0)
counter = groupSize;
else {
groupIndex += fraction / groupSize;
counter = fraction % groupSize;
if (counter == 0)
counter = groupSize;
else
groupIndex++;
}
}
else
UseGroup = false;
for (int i = offset; i - offset < length; i++) {
char c = format [i];
if (c == literal && c != '\0') {
literal = '\0';
continue;
}
if (literal != '\0') {
sb.Append (c);
continue;
}
switch (c) {
case '\\':
i++;
if (i - offset < length)
sb.Append (format [i]);
continue;
case '\'':
case '\"':
if (c == '\"' || c == '\'')
literal = c;
continue;
case '#':
goto case '0';
case '0':
if (integerArea) {
intSharpCounter++;
if (IntegerDigits - intSharpCounter < sb_int.Length + sb_int_index || c == '0')
while (IntegerDigits - intSharpCounter + sb_int_index < sb_int.Length) {
sb.Append (sb_int [sb_int_index++]);
if (UseGroup && --intLen > 0 && --counter == 0) {
sb.Append (groupSeparator);
if (--groupIndex < groups.Length && groupIndex >= 0)
groupSize = groups [groupIndex];
counter = groupSize;
}
}
break;
}
else if (decimalArea) {
if (sb_dec_index < sb_dec.Length)
sb.Append (sb_dec [sb_dec_index++]);
break;
}
sb.Append (c);
break;
case 'e':
case 'E':
if (sb_exp == null || !UseExponent) {
sb.Append (c);
break;
}
bool flag1 = true;
bool flag2 = false;
int q;
for (q = i + 1; q - offset < length; q++) {
if (format [q] == '0') {
flag2 = true;
continue;
}
if (q == i + 1 && (format [q] == '+' || format [q] == '-'))
continue;
if (!flag2)
flag1 = false;
break;
}
if (flag1) {
i = q - 1;
integerArea = (DecimalPointPos < 0);
decimalArea = !integerArea;
sb.Append (c);
sb.Append (sb_exp);
sb_exp = null;
}
else
sb.Append (c);
break;
case '.':
if (DecimalPointPos == i) {
if (DecimalDigits > 0) {
while (sb_int_index < sb_int.Length)
sb.Append (sb_int [sb_int_index++]);
}
if (sb_dec.Length > 0)
sb.Append (nfi.NumberDecimalSeparator);
}
integerArea = false;
decimalArea = true;
break;
case ',':
break;
case '%':
sb.Append (nfi.PercentSymbol);
break;
case '\u2030':
sb.Append (nfi.PerMilleSymbol);
break;
default:
sb.Append (c);
break;
}
}
if (!positive)
sb.Insert (0, nfi.NegativeSign);
return sb.ToString ();
}
}