The @@@BUG@@@ the size of China - std.conv.d - Target parse(Target, Source)(ref Source s, uint radix)
tsbockman via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Dec 7 19:13:34 PST 2015
On Monday, 7 December 2015 at 20:56:24 UTC, John Carter wrote:
> So whilst attempt to convert from a hex string (without the 0x)
> to int I bumped into the @@@BUG@@@ the size of China....
>
> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L2270
>
> Is there a bugzilla issue number tracking this?
>
> Searching for conv and parse in the issue tracker didn't turn
> it up....
>
> Is this a phobos bug or a compiler bug?
>
> I followed the example in the unit test to get a workaround....
> but I don't understand why the workaround works!
Despite the note in the unittest, it's actually not a bug after
all - parse is just designed for a different use case than yours.
What you want is T std.conv.to(T, S)(S value, uint radix). For
example, this works fine:
import std.conv;
foreach (i; 2..37)
{
assert(to!int("0", i) == 0);
assert(to!int("1", i) == 1);
assert(to!byte("10", i) == i);
}
Or using UFCS, if you prefer:
import std.conv;
foreach (i; 2..37)
{
assert("0".to!int(i) == 0);
assert("1".to!int(i) == 1);
assert("10".to!byte(i) == i);
}
The technical reason that the compiler won't let you use parse
that way, is that its first argument is annotated with ref and
cannot accept rvalues for memory safety reasons.
More information about the Digitalmars-d-learn
mailing list