Skip to content

Instantly share code, notes, and snippets.

@NinoFloris
Created November 20, 2016 21:30
Show Gist options
  • Save NinoFloris/7ece499b0c8e38ac150614bb141e5d82 to your computer and use it in GitHub Desktop.
Save NinoFloris/7ece499b0c8e38ac150614bb141e5d82 to your computer and use it in GitHub Desktop.
FastIntAsTextToStringBuilder
static unsafe void FastIntAsTextToStringBuilder(int input, StringBuilder sb)
{
const string sign = "-";
const int signSize = 1;
const int size = 15 + signSize;
var charbuf = stackalloc char[size];
var p = charbuf + size - 1;
var minDigits = 1;
var value = (uint)(input > 0 ? input : -input);
while (--minDigits >= 0 || value != 0)
{
*--p = (char)(value % 10 + '0');
value /= 10;
}
if (input < 0)
{
for (var i = signSize - 1; i >= 0; i--)
{
*--p = sign[i];
}
}
sb.Append(p, (int)(charbuf + (size - 1) - p));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment