diff options
Diffstat (limited to 'libphobos/src/std/format')
-rw-r--r-- | libphobos/src/std/format/spec.d | 35 | ||||
-rw-r--r-- | libphobos/src/std/format/write.d | 32 |
2 files changed, 61 insertions, 6 deletions
diff --git a/libphobos/src/std/format/spec.d b/libphobos/src/std/format/spec.d index e5564c9..b828bb6 100644 --- a/libphobos/src/std/format/spec.d +++ b/libphobos/src/std/format/spec.d @@ -127,14 +127,16 @@ if (is(Unqual!Char == Char)) Counting starts with `1`. Set to `0` if not used. Default: `0`. */ - ubyte indexStart; + ushort indexStart; /** Index of the last argument for positional parameter ranges. Counting starts with `1`. Set to `0` if not used. Default: `0`. + + The maximum value of this field is used as a sentinel to indicate the arguments' length. */ - ubyte indexEnd; + ushort indexEnd; version (StdDdoc) { @@ -296,6 +298,8 @@ if (is(Unqual!Char == Char)) } width = 0; + indexStart = 0; + indexEnd = 0; precision = UNSPECIFIED; nested = null; // Parse the spec (we assume we're past '%' already) @@ -834,6 +838,33 @@ if (is(Unqual!Char == Char)) == "$ expected after '*10' in format string"); } +// https://github.com/dlang/phobos/issues/10713 +@safe pure unittest +{ + import std.array : appender; + auto f = FormatSpec!char("%3$d%d"); + + auto w = appender!(char[])(); + f.writeUpToNextSpec(w); + assert(f.indexStart == 3); + + f.writeUpToNextSpec(w); + assert(w.data.length == 0); + assert(f.indexStart == 0); +} + +// https://github.com/dlang/phobos/issues/10699 +@safe pure unittest +{ + import std.array : appender; + auto f = FormatSpec!char("%1:$d"); + auto w = appender!(char[])(); + + f.writeUpToNextSpec(w); + assert(f.indexStart == 1); + assert(f.indexEnd == ushort.max); +} + /** Helper function that returns a `FormatSpec` for a single format specifier. diff --git a/libphobos/src/std/format/write.d b/libphobos/src/std/format/write.d index 078fa78..68a96d4 100644 --- a/libphobos/src/std/format/write.d +++ b/libphobos/src/std/format/write.d @@ -534,6 +534,8 @@ uint formattedWrite(Writer, Char, Args...)(auto ref Writer w, const scope Char[] // Are we already done with formats? Then just dump each parameter in turn uint currentArg = 0; + bool lastWasConsumeAll; + while (spec.writeUpToNextSpec(w)) { if (currentArg == Args.length && !spec.indexStart) @@ -648,12 +650,23 @@ uint formattedWrite(Writer, Char, Args...)(auto ref Writer w, const scope Char[] break SWITCH; } default: - throw new FormatException( - text("Positional specifier %", spec.indexStart, '$', spec.spec, - " index exceeds ", Args.length)); + if (spec.indexEnd == spec.indexEnd.max) + { + lastWasConsumeAll = true; + break; + } + else if (spec.indexEnd == spec.indexStart) + throw new FormatException( + text("Positional specifier %", spec.indexStart, '$', spec.spec, + " index exceeds ", Args.length)); + else + throw new FormatException( + text("Positional specifier %", spec.indexStart, ":", spec.indexEnd, '$', spec.spec, + " index exceeds ", Args.length)); } } - return currentArg; + + return lastWasConsumeAll ? Args.length : currentArg; } /// @@ -1199,6 +1212,17 @@ if (isSomeString!(typeof(fmt))) formattedWrite(stream, "%s", aa); } +// https://github.com/dlang/phobos/issues/10699 +@safe pure unittest +{ + import std.array : appender; + auto w = appender!(char[])(); + + uint count = formattedWrite(w, "%1:$d", 1, 2, 3); + assert(count == 3); + assert(w.data == "123"); +} + /** Formats a value of any type according to a format specifier and writes the result to an output range. |