fix Issue 7909 - to!(enum)(string) and to!(string)(enum) break when enum is integral

This commit is contained in:
k-hara 2012-04-15 10:55:21 +09:00
parent c27262cdb9
commit 52462bec6e
3 changed files with 31 additions and 25 deletions

View file

@ -6,6 +6,7 @@ $(VERSION 060, ddd mm, 2012, =================================================,
)
$(LIBBUGSFIXED
$(LI $(BUGZILLA 7909): to!enum(string) and to!string(enum) break when enum is integral)
)
)
$(VERSION 059, ddd mm, 2012, =================================================,

View file

@ -1048,21 +1048,23 @@ T toImpl(T, S)(S s)
unittest
{
debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded.");
enum E { a, b, c }
assert(to! string(E.a) == "a"c);
assert(to!wstring(E.b) == "b"w);
assert(to!dstring(E.c) == "c"d);
enum F : real { x = 1.414, y = 1.732, z = 2.236 }
assert(to! string(F.x) == "x"c);
assert(to!wstring(F.y) == "y"w);
assert(to!dstring(F.z) == "z"d);
enum EU { a = 0, b = 1, c = 2 } // base type is unsigned
enum EI { a = -1, b = 0, c = 1 } // base type is signed (bug 7909)
enum EF : real { a = 1.414, b = 1.732, c = 2.236 }
foreach (E; TypeTuple!(EU, EI, EF))
{
assert(to! string(E.a) == "a"c);
assert(to!wstring(E.a) == "a"w);
assert(to!dstring(E.a) == "a"d);
}
// Test an value not corresponding to an enum member.
auto o = cast(E)5;
assert(to! string(o) == "cast(E)5"c);
assert(to!wstring(o) == "cast(E)5"w);
assert(to!dstring(o) == "cast(E)5"d);
auto o = cast(EU)5;
assert(to! string(o) == "cast(EU)5"c);
assert(to!wstring(o) == "cast(EU)5"w);
assert(to!dstring(o) == "cast(EU)5"d);
}
/// ditto

View file

@ -2700,7 +2700,7 @@ unittest
/*
*/
template BooleanTypeOf(T)
template BooleanTypeOf(T) if (!is(T == enum))
{
inout(bool) idx( inout(bool) );
shared(inout bool) idx( shared(inout bool) );
@ -2734,7 +2734,7 @@ unittest
/*
*/
template IntegralTypeOf(T)
template IntegralTypeOf(T) if (!is(T == enum))
{
inout( byte) idx( inout( byte) );
inout( ubyte) idx( inout( ubyte) );
@ -2793,7 +2793,7 @@ unittest
/*
*/
template FloatingPointTypeOf(T)
template FloatingPointTypeOf(T) if (!is(T == enum))
{
inout( float) idx( inout( float) );
inout(double) idx( inout(double) );
@ -2832,7 +2832,7 @@ unittest
/*
*/
template NumericTypeOf(T)
template NumericTypeOf(T) if (!is(T == enum))
{
static if (is(IntegralTypeOf!T X))
alias X NumericTypeOf;
@ -2860,7 +2860,7 @@ unittest
/*
*/
template UnsignedTypeOf(T)
template UnsignedTypeOf(T) if (!is(T == enum))
{
static if (is(IntegralTypeOf!T X) &&
staticIndexOf!(Unqual!X, UnsignedIntTypeList) >= 0)
@ -2869,7 +2869,7 @@ template UnsignedTypeOf(T)
static assert(0, T.stringof~" is not an unsigned type.");
}
template SignedTypeOf(T)
template SignedTypeOf(T) if (!is(T == enum))
{
static if (is(IntegralTypeOf!T X) &&
staticIndexOf!(Unqual!X, SignedIntTypeList) >= 0)
@ -2882,7 +2882,7 @@ template SignedTypeOf(T)
/*
*/
template CharTypeOf(T)
template CharTypeOf(T) if (!is(T == enum))
{
inout( char) idx( inout( char) );
inout(wchar) idx( inout(wchar) );
@ -2937,7 +2937,7 @@ unittest
/*
*/
template StaticArrayTypeOf(T)
template StaticArrayTypeOf(T) if (!is(T == enum))
{
inout(U[n]) idx(U, size_t n)( inout(U[n]) );
@ -2968,7 +2968,7 @@ unittest
/*
*/
template DynamicArrayTypeOf(T)
template DynamicArrayTypeOf(T) if (!is(T == enum))
{
inout(U[]) idx(U)( inout(U[]) );
@ -3008,7 +3008,7 @@ unittest
/*
*/
template ArrayTypeOf(T)
template ArrayTypeOf(T) if (!is(T == enum))
{
static if (is(StaticArrayTypeOf!T X))
alias X ArrayTypeOf;
@ -3024,7 +3024,7 @@ unittest
/*
*/
template StringTypeOf(T) if (isSomeString!T)
template StringTypeOf(T) if (!is(T == enum) && isSomeString!T)
{
alias ArrayTypeOf!T StringTypeOf;
}
@ -3054,7 +3054,7 @@ unittest
/*
*/
template AssocArrayTypeOf(T)
template AssocArrayTypeOf(T) if (!is(T == enum))
{
immutable(V [K]) idx(K, V)( immutable(V [K]) );
@ -3286,6 +3286,9 @@ Detect whether we can treat T as one of the built-in string types.
*/
template isSomeString(T)
{
static if (is(T == enum))
enum isSomeString = false;
else
enum isSomeString = isNarrowString!T || is(T : const(dchar[]));
}