diff options
Diffstat (limited to 'libphobos/src/std/format')
-rw-r--r-- | libphobos/src/std/format/internal/write.d | 20 | ||||
-rw-r--r-- | libphobos/src/std/format/package.d | 31 | ||||
-rw-r--r-- | libphobos/src/std/format/write.d | 20 |
3 files changed, 53 insertions, 18 deletions
diff --git a/libphobos/src/std/format/internal/write.d b/libphobos/src/std/format/internal/write.d index 8089cfa..32c82995 100644 --- a/libphobos/src/std/format/internal/write.d +++ b/libphobos/src/std/format/internal/write.d @@ -3011,7 +3011,7 @@ void enforceValidFormatSpec(T, Char)(scope const ref FormatSpec!Char f) /* `enum`s are formatted like their base value */ -void formatValueImpl(Writer, T, Char)(auto ref Writer w, const(T) val, scope const ref FormatSpec!Char f) +void formatValueImpl(Writer, T, Char)(auto ref Writer w, T val, scope const ref FormatSpec!Char f) if (is(T == enum)) { import std.array : appender; @@ -3020,21 +3020,15 @@ if (is(T == enum)) if (f.spec != 's') return formatValueImpl(w, cast(OriginalType!T) val, f); - static foreach (e; EnumMembers!T) - { - if (val == e) - { - formatValueImpl(w, __traits(identifier, e), f); - return; - } - } + foreach (immutable member; __traits(allMembers, T)) + if (val == __traits(getMember, T, member)) + return formatValueImpl(w, member, f); auto w2 = appender!string(); // val is not a member of T, output cast(T) rawValue instead. - put(w2, "cast("); - put(w2, T.stringof); - put(w2, ")"); + enum prefix = "cast(" ~ T.stringof ~ ")"; + put(w2, prefix); static assert(!is(OriginalType!T == T), "OriginalType!" ~ T.stringof ~ "must not be equal to " ~ T.stringof); @@ -3154,7 +3148,7 @@ if (isPointer!T && !is(T == enum) && !hasToString!(T, Char)) auto a = iota(0, 10); auto b = iota(0, 10); - auto p = () @trusted { auto p = &a; return p; }(); + auto p = () @trusted { auto result = &a; return result; }(); assert(format("%s",p) != format("%s",b)); } diff --git a/libphobos/src/std/format/package.d b/libphobos/src/std/format/package.d index d83f028..f1d4705 100644 --- a/libphobos/src/std/format/package.d +++ b/libphobos/src/std/format/package.d @@ -212,17 +212,17 @@ There are several flags that affect the outcome of the formatting. $(BOOKTABLE , $(TR $(TH Flag) $(TH Semantics)) $(TR $(TD $(B '-')) - $(TD When the formatted result is shorter then the value - given by the width parameter, the output is right - justified. With the $(B '-') flag this is changed - to left justification. + $(TD When the formatted result is shorter than the value + given by the width parameter, the output is left + justified. Without the $(B '-') flag, the output remains + right justified. There are two exceptions where the $(B '-') flag has a different meaning: (1) with $(B 'r') it denotes to use little endian and (2) in case of a compound indicator it means that no special handling of the members is applied.)) $(TR $(TD $(B '=')) - $(TD When the formatted result is shorter then the value + $(TD When the formatted result is shorter than the value given by the width parameter, the output is centered. If the central position is not possible it is moved slightly to the right. In this case, if $(B '-') flag is present in @@ -1563,6 +1563,14 @@ char[] sformat(Char, Args...)(return scope char[] buf, scope const(Char)[] fmt, { char[] buf; size_t i; + void put(char c) + { + if (buf.length <= i) + throw new RangeError(__FILE__, __LINE__); + + buf[i] = c; + i += 1; + } void put(dchar c) { char[4] enc; @@ -1687,6 +1695,19 @@ if (isSomeString!(typeof(fmt))) assert(u == v); } +@safe unittest // https://issues.dlang.org/show_bug.cgi?id=23488 +{ + static struct R + { + string s = "Ü"; + bool empty() { return s.length == 0; } + char front() { return s[0]; } + void popFront() { s = s[1 .. $]; } + } + char[2] buf; + assert(sformat(buf, "%s", R()) == "Ü"); +} + version (StdUnittest) private void formatReflectTest(T)(ref T val, string fmt, string formatted, string fn = __FILE__, size_t ln = __LINE__) { diff --git a/libphobos/src/std/format/write.d b/libphobos/src/std/format/write.d index cbab512..2aa45d7 100644 --- a/libphobos/src/std/format/write.d +++ b/libphobos/src/std/format/write.d @@ -1310,3 +1310,23 @@ void formatValue(Writer, T, Char)(auto ref Writer w, auto ref T val, scope const writer.formatValue(a, spec); assert(writer.data == "0"); } + +// https://issues.dlang.org/show_bug.cgi?id=23400 +@safe pure unittest +{ + import std.range : nullSink; + import std.format.spec : singleSpec; + + static struct S + { + // non-const opEquals method + bool opEquals(S rhs) { return false; } + } + + enum E { a = S() } + + E e; + auto writer = nullSink; + const spec = singleSpec("%s"); + writer.formatValue(e, spec); +} |