Skip to content

Commit

Permalink
Merge pull request #4390 from JackStouffer/issue15800
Browse files Browse the repository at this point in the history
Fixed Issue 15800: std.conv.to!int does not work with ranges of any char …
  • Loading branch information
schveiguy committed Jun 3, 2016
2 parents d80f31e + 97edfbe commit d5119c3
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions std/conv.d
Original file line number Diff line number Diff line change
Expand Up @@ -1728,19 +1728,19 @@ private void testFloatingToIntegral(Floating, Integral)()


/**
String to non-string conversion runs parsing.
String, or string-like input range, to non-string conversion runs parsing.
$(UL
$(LI When the source is a wide string, it is first converted to a narrow
string and then parsed.)
$(LI When the source is a narrow string, normal text parsing occurs.))
*/
private T toImpl(T, S)(S value)
if ( isExactSomeString!S && isDynamicArray!S &&
if (isInputRange!S && isSomeChar!(ElementEncodingType!S) &&
!isExactSomeString!T && is(typeof(parse!T(value))))
{
scope(success)
{
if (value.length)
if (!value.empty)
{
throw convError!(S, T)(value);
}
Expand All @@ -1750,12 +1750,12 @@ private T toImpl(T, S)(S value)

/// ditto
private T toImpl(T, S)(S value, uint radix)
if ( isExactSomeString!S && isDynamicArray!S &&
if (isInputRange!S && isSomeChar!(ElementEncodingType!S) &&
!isExactSomeString!T && is(typeof(parse!T(value, radix))))
{
scope(success)
{
if (value.length)
if (!value.empty)
{
throw convError!(S, T)(value);
}
Expand Down Expand Up @@ -1784,6 +1784,24 @@ private T toImpl(T, S)(S value, uint radix)
assert(n == 255);
}

// bugzilla 15800
unittest
{
import std.utf : byCodeUnit, byChar, byWchar, byDchar;

assert(to!int(byCodeUnit("10")) == 10);
assert(to!int(byCodeUnit("10"), 10) == 10);
assert(to!int(byCodeUnit("10"w)) == 10);
assert(to!int(byCodeUnit("10"w), 10) == 10);

assert(to!int(byChar("10")) == 10);
assert(to!int(byChar("10"), 10) == 10);
assert(to!int(byWchar("10")) == 10);
assert(to!int(byWchar("10"), 10) == 10);
assert(to!int(byDchar("10")) == 10);
assert(to!int(byDchar("10"), 10) == 10);
}

/**
Convert a value that is implicitly convertible to the enum base type
into an Enum value. If the value does not match any enum member values
Expand Down

0 comments on commit d5119c3

Please sign in to comment.