From 97edfbea90d72d8f4b6483c091a5b6d0e54db018 Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Wed, 1 Jun 2016 11:18:05 -0400 Subject: [PATCH] Fixed Issue 15800: std.conv.to does not work with ranges of any char type --- std/conv.d | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/std/conv.d b/std/conv.d index 9ccca922ac0..b7ad9bc257f 100644 --- a/std/conv.d +++ b/std/conv.d @@ -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); } @@ -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); } @@ -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