Permalink
Browse files

fix Issue 6892 - Formatted write with specified length of enum member

  • Loading branch information...
9rnsr authored and jmdavis committed Feb 29, 2012
1 parent 627c48f commit 12571977d821a2e7e7a799012baea3e8f73dee0d
Showing with 26 additions and 9 deletions.
  1. +26 −9 std/format.d
View
@@ -2102,19 +2102,24 @@ unittest
void formatValue(Writer, T, Char)(Writer w, T val, ref FormatSpec!Char f)
if (is(T == enum))
{
foreach (i, e; EnumMembers!T)
if (f.spec == 's')
{
if (val == e)
foreach (i, e; EnumMembers!T)
{
put(w, __traits(allMembers, T)[i]);
return;
if (val == e)
{
formatValue(w, __traits(allMembers, T)[i], f);
return;
}
}
}
// val is not a member of T, output cast(T)rawValue instead.
put(w, "cast(" ~ T.stringof ~ ")");
static assert(!is(OriginalType!T == T));
formatValue(w, cast(OriginalType!T)val, f);
// val is not a member of T, output cast(T)rawValue instead.
put(w, "cast(" ~ T.stringof ~ ")");
static assert(!is(OriginalType!T == T));
formatValue(w, cast(OriginalType!T)val, f);
}
else
formatValue(w, cast(OriginalType!T)val, f);
}
unittest
{
@@ -2134,6 +2139,18 @@ unittest
formatTest( A.yes, "yes" );
formatTest( A.no, "no" );
}
unittest
{
// Test for bug 6892
enum Foo { A = 10 }
formatTest("%s", Foo.A, "A");
formatTest(">%4s<", Foo.A, "> A<");
formatTest("%04d", Foo.A, "0010");
formatTest("%+2u", Foo.A, "+10");
formatTest("%02x", Foo.A, "0a");
formatTest("%3o", Foo.A, " 12");
formatTest("%b", Foo.A, "1010");
}
/**
Pointers are formatted as hex integers.

0 comments on commit 1257197

Please sign in to comment.