Skip to content

Commit

Permalink
fix issue 16192 - std.conv.toChars() opSlice wrong for radix other th…
Browse files Browse the repository at this point in the history
…an 10
  • Loading branch information
aG0aep6G committed Jun 28, 2016
1 parent 1a740ef commit 18c9ffc
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions std/conv.d
Expand Up @@ -5692,7 +5692,7 @@ auto toChars(ubyte radix = 10, Char = char, LetterCase letterCase = LetterCase.l
enum SHIFT = 4;
else
static assert(0);
struct Result
static struct Result
{
this(UT value)
{
Expand Down Expand Up @@ -5733,7 +5733,7 @@ auto toChars(ubyte radix = 10, Char = char, LetterCase letterCase = LetterCase.l
Result opSlice(size_t lwr, size_t upr)
{
Result result = void;
result.value = value >>> ((len - upr - 1) * SHIFT);
result.value = value >>> ((len - upr) * SHIFT);
result.len = cast(ubyte)(upr - lwr);
return result;
}
Expand Down Expand Up @@ -5833,3 +5833,51 @@ unittest
}
}

unittest // opSlice (issue 16192)
{
import std.meta: AliasSeq;

static struct Test { ubyte radix; uint number; }

alias tests = AliasSeq!(
Test(2, 0b1_0110_0111u),
Test(2, 0b10_1100_1110u),
Test(8, octal!123456701u),
Test(8, octal!1234567012u),
Test(10, 123456789u),
Test(10, 1234567890u),
Test(16, 0x789ABCDu),
Test(16, 0x789ABCDEu),
);

foreach (test; tests)
{
enum ubyte radix = test.radix;
auto original = toChars!radix(test.number);

// opSlice vs popFront
auto r = original.save;
size_t i = 0;
for (; !r.empty; r.popFront(), ++i)
{
assert(original[i .. original.length].tupleof == r.tupleof);
// tupleof is used to work around issue 16216.
}

// opSlice vs popBack
r = original.save;
i = 0;
for (; !r.empty; r.popBack(), ++i)
{
assert(original[0 .. original.length - i].tupleof == r.tupleof);
}

// opSlice vs both popFront and popBack
r = original.save;
i = 0;
for (; r.length >= 2; r.popFront(), r.popBack(), ++i)
{
assert(original[i .. original.length - i].tupleof == r.tupleof);
}
}
}

0 comments on commit 18c9ffc

Please sign in to comment.