Escaping control in formatting

Denis Shelomovskij verylonglogin.reg at gmail.com
Mon Apr 23 05:36:09 PDT 2012


I've never used new excellent range formatting syntax by Kenji Hara 
until now. And I've met with difficulties, because "%(%(%c%), %)" is the 
most common format for string array for me and it neither obvious nor 
elegant. It occurs that "%c" disables character escaping. What the hell? 
Why? Not obvious at all.

So I think it will be good to add 'Escaping' part after 'Precision' in 
format specifications:

Escaping:
   empty
   !-
   !+
   !'
   !"
   !?'
   !?"
   !?!

Escaping affect formatting depending on the specifier as follows.

Escaping    Semantics
   !-      disable escaping, for a range it also disables [,]
   !+      enable escaping using single quotes for chars and double 
quotes for strings
   !'      enable escaping using single quotes
   !"      enable escaping using double quotes
   !?'     like !' but without adding the quotes and [,] for a range
   !?"     like !" but without adding the quotes and [,] for a range
   !?!     enable escaping, both single and double quotes will be 
escaped without adding any quotes and [,] for a range

Escaping is enabled by default only for associative arrays, ranges (not 
strings), user-defined types, and all its sub-elements.

I'd like to remove "%c"'s ability to magically disable escaping and it 
looks possible until it is documented.

Look at the example:
---
import std.stdio;

void main() {
     writeln("    char");
     char c = '\'';
     writefln("unescaped: %s."  ,   c  );
     writefln(`escaped+': %(%).`, [ c ]); // proposal: %!+s or %!'s
     writefln(`escaped+": %(%).`, [[c]]); // proposal: %!"s
     writeln (`  escaped: \t.`);          // proposal: %!?'s
     writeln();
     writeln("    string");
     string s = "a\tb";
     writefln("unescaped: %s."  ,  s );
     writefln(`escaped+": %(%).`, [s]); // proposal: %!+s or %!"s
     writeln (`  escaped: a\tb.`);      // proposal: %!?"s
     writeln();
     writeln("    strings");
     string[] ss = ["a\tb", "cd"];
     writefln("unescaped: %(%(%c%)%).", ss); // proposal: %!-s
     writefln(`escaped+": %(%).`      , ss);
     writeln (`  escaped: a\tbcd.`    , ss); // proposal: %!?"s
}
---

If it will be accepted, I can volunteer to try to implement it. If not, 
escaping should be at least documented (and do not forget about "%c"'s 
magic!).

Any thoughts?

P.S.
If it has already been discussed, please give me a link.

-- 
Денис В. Шеломовский
Denis V. Shelomovskij


More information about the Digitalmars-d mailing list