Skip to content

Commit

Permalink
properly handle negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
braddr committed Oct 7, 2012
1 parent 7583e5f commit 99d729e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -3670,16 +3670,8 @@ void toTextRange(T, W)(T value, W writer)
char[value.sizeof * 4] buffer = void;
uint i = cast(uint) (buffer.length - 1);

Unqual!(Unsigned!T) v = void;
if (value < 0)
{
buffer[i--] = '-';
v = -value;
}
else
{
v = value;
}
bool negative = value < 0;
Unqual!(Unsigned!T) v = negative ? -value : value;

while (v >= 10)
{
Expand All @@ -3689,9 +3681,17 @@ void toTextRange(T, W)(T value, W writer)
}

buffer[i] = cast(char) (v + '0'); //hexDigits[cast(uint) v];
if (negative)
buffer[--i] = '-';
put(writer, buffer[i .. $]);
}

unittest
{
auto result = appender!(char[])();
toTextRange(-1, result);
assert(result.data == "-1");
}

template hardDeprec(string vers, string date, string oldFunc, string newFunc)
{
Expand Down

0 comments on commit 99d729e

Please sign in to comment.