Oh, cool.
On Thursday, 31 March 2016 at 03:29:19 UTC, H. S. Teoh wrote:
Or implement manual substitution with a pipeline:
string myString = ...;
string escapedStr = myString
.chunks(1)
.map!(c => (c == "\n") ? "\\n" :
(c == "\r") ? "\\r" :
(c == "\t") ? "\\t" :
c)
.joiner
.array;
What I did was
string escapedStr = myString
.replace("\n",`\n`)
.replace("\r",`\r`)
.replace("\t",`\t`);
That makes like 3 copies of the string I guess, but whatever. I'm
not sure how efficient a general chunking filter would be on
1-byte chunks, and I certainly don't want to be creating a
zillion unescaped 1-byte strings, so if I cared I'd probably do
something like this:
auto escapedStr = appender!string;
for(c;myString) {
switch(c) {
case '\n':
escapedStr.put("\\n");
case '\r':
escapedStr.put("\\r");
...
default:
escapedStr.put(c);
}
}
It'd have to be long and boring to get all the control characters
though, and possibly unicode ones too, or do "\xNN" style byte
escapes. So I was hoping something standard already existed.