Bug 4532 – std.string.format, std.stream methods, etc. still use the old doFormat instead of formattedWrite

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-07-29T11:18:00Z
Last change time
2016-03-22T12:58:12Z
Assigned to
nobody
Creator
bus_dbugzilla

Comments

Comment #0 by bus_dbugzilla — 2010-07-29T11:18:09Z
writefln("%1$s", "A"); // Prints "A" format("%1$s", "A"); // Throws FormatError
Comment #1 by smjg — 2010-07-30T10:27:52Z
This is most peculiar. Why has it been added directly into writef, rather than being put in doFormat where it belongs?
Comment #2 by smjg — 2010-08-18T08:47:04Z
I've just looked at the code and seem to have figured out what's going on. Traditionally, std.format.doFormat is the function that underlies all the writef* functions and std.string.format. However, a new templated function has been written to replace it, std.format.formattedWrite. The problem is that std.string.format still uses doFormat, as do the writef* functions in std.stream and others. We need to get this fixed, and then deprecate doFormat.
Comment #3 by bus_dbugzilla — 2011-08-16T12:42:22Z
*** Issue 6455 has been marked as a duplicate of this issue. ***
Comment #4 by smjg — 2012-04-01T15:04:29Z
*** Issue 5687 has been marked as a duplicate of this issue. ***
Comment #5 by smjg — 2012-04-01T15:04:46Z
*** Issue 6595 has been marked as a duplicate of this issue. ***
Comment #6 by smjg — 2012-04-01T15:05:31Z
*** Issue 5444 has been marked as a duplicate of this issue. ***
Comment #7 by smjg — 2012-04-01T15:05:44Z
*** Issue 7571 has been marked as a duplicate of this issue. ***
Comment #8 by smjg — 2012-04-01T15:06:00Z
*** Issue 7620 has been marked as a duplicate of this issue. ***
Comment #9 by smjg — 2012-04-09T13:54:23Z
*** Issue 7877 has been marked as a duplicate of this issue. ***
Comment #10 by k.hara.pg — 2012-04-09T21:38:54Z
*** Issue 7881 has been marked as a duplicate of this issue. ***
Comment #11 by k.hara.pg — 2012-04-09T21:40:28Z
Issue 6595 was a bug report only about std.string.format/sformat functions. From issue 6595 comment #0: > This enhancement issue is nearly a bug report. > > format() and sformat() use std.format.doFormat as their implementations, but it > is old feature, and its features are fewer than formatValue family. > > And it is causing not a few issues: > bug 3715 - std.string.format can't use const/immutable toString functions > bug 4266 - add support for structs in std.format.doFormat > bug 4532 - Position specifiers don't work in format > bug 5444 - std.string.format: arguments without format specifier appended to > result > bug 5970 - fix BigInt.toString > > I think format() should be implemented just appender and formattedWrite like > follows: > > string format(Char, Args...)(in Char[] fmt, Args args) > { > auto w = appender!string(); > formattedWrite(w, fmt, args); > return w.data; > } > > This 'format()' provides just the same features as writef(ln) functions about > formatting. > > And sformat() also could replace like follows: > > char[] sformat(Char, Args...)(char[] buf, in Char[] fmt, Args args) > { > size_t i; > void sink(const(char)[] s) { > if (buf.length < i + s.length) > onRangeError("std.string.sformat", 0); > buf[i .. i + s.length] = s[]; > i += s.length; > } > formattedWrite(&sink, fmt, args); > return buf[0 .. i]; > } And I proposed a pull and it is discussed in here: https://github.com/D-Programming-Language/phobos/pull/231
Comment #12 by smjg — 2012-04-10T03:38:13Z
*** Issue 4754 has been marked as a duplicate of this issue. ***
Comment #13 by bearophile_hugs — 2012-04-20T14:41:35Z
This seems another example of the same problem: import std.stdio: writeln; import std.conv: text; struct Foo {} void main() { Foo* f = new Foo; writeln(f); writeln(text(f)); } Output: 15D2FD0 22884304 Expected output: 15D2FD0 15D2FD0
Comment #14 by bearophile_hugs — 2012-04-20T14:49:05Z
I've taken a look here: https://github.com/D-Programming-Language/phobos/pull/231 Kenji Hara sasy: >The same name with core.stdc.sprintf is very confusing. And std.string.format returns new string, but core.stdc.sprintf write the formatted string into specified buffer. The two behaviors are quite different.< Andrei Alexandrescu says: >There's no problem with core.stdc.sprintf bearing the same name as std.format.sprintf. Phobos has had such duplicate names for years without a problem.< I agree that Phobos has some name clashes. But I don't agree it's a good thing. It should be avoided if possible for newly added names. And I agree the behavior of the new functions is different from the C functions. So I agree with Kenji Hara, they should have different names.
Comment #15 by smjg — 2012-04-20T16:29:30Z
(In reply to comment #13) text is just to!string wrapped in a variadic template to support multiple arguments. The implementation of to!string for pointer types (line 1259 in conv.d, in 2.059) converts it to a size_t, which is an integral type, leading to it being formatted as decimal. I'm not sure whether it's related or not. But what it does show is that we need to investigate the long chains of template instantiations all these various functions go through.
Comment #16 by andrej.mitrovich — 2013-01-24T16:22:09Z
It seems all code samples in this issue are fixed. There seems to be very few doFormat calls left in Phobos: std\stdio.d:1498: std.format.doFormat(&putc, arguments, argptr); std\stdio.d:1533: std.format.doFormat(&putcw, arguments, argptr); std\stream.d:1209: doFormat(&doFormatCallback,arguments,argptr);
Comment #17 by hsteoh — 2014-08-07T22:32:52Z
Update: as of today, phobos git HEAD really has only 2 calls to doFormat left: std.stream.Stream.writefx(), and std.stdio.writefx(), the latter of which is private and apparently unused (I deleted it and all unittests pass). So here's a pull that eliminates std.stdio.writefx(): https://github.com/D-Programming-Language/phobos/pull/2403 Once that's done, there will only be std.stream.Stream.writefx() left before we can get rid of doFormat forever.
Comment #18 by github-bugzilla — 2014-08-08T04:00:41Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/16822243a1e18f1df2f0e02e60bcfaa3fb252a30 Remove unused private writefx() function. This eliminates the 3rd last usage of doFormat in Phobos, thus inching us closer to fully fixing bug #4532. https://github.com/D-Programming-Language/phobos/commit/df3fbff9488afa12b9f3b6ca4de5cd46f2e240dd Merge pull request #2403 from quickfur/issue4532b Remove unused private writefx() function.
Comment #19 by dmitry.olsh — 2016-03-22T12:58:12Z
Was fixed quite sometime ago.