Permalink
Browse files

Faster integer to text conversion

  • Loading branch information...
andralex committed Oct 4, 2012
1 parent 08722ad commit 28e1c98d69a0258992068f4410bdd62c46f5474a
Showing with 9 additions and 15 deletions.
  1. +9 −15 std/conv.d
View
@@ -3651,34 +3651,28 @@ unittest
void toTextRange(T, W)(T value, W writer)
if (isIntegral!T && isOutputRange!(W, char))
{
+ char[value.sizeof * 4] buffer = void;
+ uint i = cast(uint) (buffer.length - 1);
+
Unqual!(Unsigned!T) v = void;
if (value < 0)
{
- put(writer, '-');
+ buffer[i--] = '-';
v = -value;
}
else
{
v = value;
}
- if (v < 10 && v < hexDigits.length)
+ while (v >= 10)
{
- put(writer, hexDigits[cast(size_t) v]);
- return;
+ auto c = cast(uint) (v % 10);
+ v /= 10;
+ buffer[i--] = cast(char) (c + '0');
}
- char[v.sizeof * 4] buffer = void;
- auto i = buffer.length;
-
- do
- {
- auto c = cast(ubyte) (v % 10);
- v = v / 10;
- i--;
- buffer[i] = cast(char) (c + '0');
- } while (v);
-
+ buffer[i] = cast(char) (v + '0'); //hexDigits[cast(uint) v];
put(writer, buffer[i .. $]);
}

2 comments on commit 28e1c98

@braddr

This comment has been minimized.

Show comment Hide comment
@braddr

braddr Oct 7, 2012

Member

Tsk tsk tsk.. broken and no unit tests to cover this code.. see pull #848 for the fix

Member

braddr replied Oct 7, 2012

Tsk tsk tsk.. broken and no unit tests to cover this code.. see pull #848 for the fix

@andralex

This comment has been minimized.

Show comment Hide comment
@andralex

andralex Oct 7, 2012

Member

yah kinda embarrassing

Member

andralex replied Oct 7, 2012

yah kinda embarrassing

Please sign in to comment.