+2004-10-17 Ben Maurer <bmaurer@ximian.com>
+
+ * DateTime.cs (ZeroPad): Use unsafe code to speed this up. We
+ avoid entering slow integer formatting code.
+
+ (_ToString): Use ZeroPad here when possible, as it is faster.
+
2004-10-11 Martin Baulig <martin@ximian.com>
* Environment.cs: Bump corlib version to 28.
if (tokLen <= 2)
ZeroPad (result, dfi.Calendar.GetYear (this) % 100, tokLen);
else
- result.Append (dfi.Calendar.GetYear (this).ToString ("D" + (tokLen == 3 ? 3 : 4)));
+ ZeroPad (result, dfi.Calendar.GetYear (this), (tokLen == 3 ? 3 : 4));
break;
case 'g':
throw new FormatException("Un-ended quote");
}
- static void ZeroPad (StringBuilder output, int digits, int padding)
+ static unsafe void ZeroPad (StringBuilder output, int digits, int len)
{
- output.Append (digits.ToString (new string ('0', padding)));
+ // more than enough for an int
+ char* buffer = stackalloc char [16];
+ int pos = 16;
+
+ do {
+ buffer [-- pos] = (char) ('0' + digits % 10);
+ digits /= 10;
+ len --;
+ } while (digits > 0);
+
+ while (len -- > 0)
+ buffer [-- pos] = '0';
+
+ output.Append (new string (buffer, pos, 16 - pos));
}
public string ToString (string format, IFormatProvider fp)