Thread overview
[Issue 13480] Input range formatting should not format as "elements"
Sep 16, 2014
Kenji Hara
Sep 16, 2014
Jakob Ovrum
Sep 16, 2014
Kenji Hara
Sep 16, 2014
Jakob Ovrum
September 16, 2014
https://issues.dlang.org/show_bug.cgi?id=13480

--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> ---
(In reply to Jakob Ovrum from comment #0)
> writefln statement #2 and #3 don't make any sense. Formatting characters and strings using the element markup style (`formatElement`), which forces single and double quotes around the character or string respectively, defeats the purpose of range-based formatting, which allows you to customize the fluff around each element.

If you want to stop automatic element quoting, you can use "%-(".

void main()
{
    auto ror = ["one", "two", "three"];
    writefln("%-(%s%| %)", [1, 2, 3]); // 1 2 3
    writefln("%-(%s%| %)", "abc");     // a b c
    writefln("%-(%s%|, %)", ror);      // one, two, three
}

It's documented in: http://dlang.org/phobos/std_format

> Inside a compound format specifier, strings and characters are escaped automatically. To avoid this behavior, add '-' flag to "%(".

--
September 16, 2014
https://issues.dlang.org/show_bug.cgi?id=13480

Jakob Ovrum <jakobovrum@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #2 from Jakob Ovrum <jakobovrum@gmail.com> ---
(In reply to Kenji Hara from comment #1)
> If you want to stop automatic element quoting, you can use "%-(".
> 
> void main()
> {
>     auto ror = ["one", "two", "three"];
>     writefln("%-(%s%| %)", [1, 2, 3]); // 1 2 3
>     writefln("%-(%s%| %)", "abc");     // a b c
>     writefln("%-(%s%|, %)", ror);      // one, two, three
> }
> 
> It's documented in: http://dlang.org/phobos/std_format
> 
> > Inside a compound format specifier, strings and characters are escaped automatically. To avoid this behavior, add '-' flag to "%(".

Thanks, nice to know it's possible to work around.

However, I don't think this flag should need to exist.

If strings were simply not quoted, one could get quoting by doing the much more intuitive explicit quoting: `%("%s"%|, %)`. Using '-' is just a hack - it has nothing to do with left-justification and thus the reader has to look it up to know what it does. We should follow the principle of least surprise here, by formatting the string as-is unless quoting is added by the user.

(changed severity to enhancement)

--
September 16, 2014
https://issues.dlang.org/show_bug.cgi?id=13480

--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> ---
(In reply to Jakob Ovrum from comment #2)
> Thanks, nice to know it's possible to work around.
> 
> However, I don't think this flag should need to exist.
> 
> If strings were simply not quoted, one could get quoting by doing the much more intuitive explicit quoting: `%("%s"%|, %)`. Using '-' is just a hack - it has nothing to do with left-justification and thus the reader has to look it up to know what it does. We should follow the principle of least surprise here, by formatting the string as-is unless quoting is added by the user.

Handmade quoting is not enough for strings which contain double-quote
character.
See:

import std.stdio;
void main() {
  string s = `Hello "D" world!`;
  writefln("[%(%s%)]", [s]);
}

will output:
["Hello \"D\" world!"]

By design, std.format.formatValue functions stringize values by using unformattable representation with unformatValue functions by default. That's the reason why automatic quoting is done by default.

--
September 16, 2014
https://issues.dlang.org/show_bug.cgi?id=13480

Jakob Ovrum <jakobovrum@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #4 from Jakob Ovrum <jakobovrum@gmail.com> ---
(In reply to Kenji Hara from comment #3)
> (In reply to Jakob Ovrum from comment #2)
> > Thanks, nice to know it's possible to work around.
> > 
> > However, I don't think this flag should need to exist.
> > 
> > If strings were simply not quoted, one could get quoting by doing the much more intuitive explicit quoting: `%("%s"%|, %)`. Using '-' is just a hack - it has nothing to do with left-justification and thus the reader has to look it up to know what it does. We should follow the principle of least surprise here, by formatting the string as-is unless quoting is added by the user.
> 
> Handmade quoting is not enough for strings which contain double-quote
> character.
> See:
> 
> import std.stdio;
> void main() {
>   string s = `Hello "D" world!`;
>   writefln("[%(%s%)]", [s]);
> }
> 
> will output:
> ["Hello \"D\" world!"]
> 
> By design, std.format.formatValue functions stringize values by using unformattable representation with unformatValue functions by default. That's the reason why automatic quoting is done by default.

OK, good point. Thanks for explaining!

--