Bug 7282 – std.string.format throws at runtime where writef works fine

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2012-01-12T12:11:00Z
Last change time
2013-01-21T17:51:28Z
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2012-01-12T12:11:01Z
module test; import std.array; import std.conv; import std.range; import std.string; import std.stdio; auto pretty(string src) { Appender!(dchar[]) res; size_t i; foreach (dchar ch; retro(src)) { if (++i % 3 == 0) res.put("_"); res.put(ch); } return retro(res.data); } void main() { // ok, "123_456" writefln("%s", pretty("123456")); // FormatException // std.format.FormatException@std\format.d(3956): // Can't convert std.range.retro!(dchar[]).retro.Result to string: // "string toString()" not defined auto str1 = format("%s", pretty("123456")); }
Comment #1 by andrej.mitrovich — 2012-01-12T12:12:49Z
Btw a workaround: string str = to!string(pretty("123456")); auto str1 = format("%s", str); So to!string works as well, it's just format() that bails.
Comment #2 by andrej.mitrovich — 2012-01-12T12:23:57Z
Also that function is broken (I mean its result), fixed one is: auto pretty(string src) { Appender!(dchar[]) res; size_t i = 1; while (!src.empty) { res.put(src.back); src.popBack; if (!src.empty && (i++ % 3 == 0)) res.put("_"); } return retro(res.data); } I was hoping there was something like this in Phobos but it didn't catch my eye. Anywho..
Comment #3 by andrej.mitrovich — 2013-01-21T17:51:28Z
Works in 2.061.