How to escape control characters?
Salih Dincer
salihdb at hotmail.com
Tue Aug 23 23:17:21 UTC 2022
On Tuesday, 23 August 2022 at 13:09:01 UTC, Steven Schveighoffer
wrote:
> Without allocations. This took me longer than I had hoped it
> would. It needs the 1-char buffer to avoid sending the
> surrounding quotes.
Surely what Steve does is better. But for some reason I like
simple and useful things.
I sometimes use custom string wrapping. Maybe it will be useful
for you, for example:
```d
struct String {
string str;
alias str this;
import std.format, std.string;
void toString(scope void delegate(const(char)[]) sink,
FormatSpec!char fmt)const
{
auto arr = str.lineSplitter;
if(fmt.spec == 'n') {
sink.formattedWrite("%-(%s\\n%)", arr);
} else sink.formattedWrite("%s", str);
}
} unittest {
import std.stdio;
auto str = `this
is a
string`;
String Str; // simple constructor
Str = str; // direct assign
Str.writefln!"%n"; // "this\n is a\n string"
Str.writefln!"%s"; /*
this
is a
string
*/
}
```
The nice thing here is that you can change the format specifiers
as you wish.
Thank you all...
SDB at 79
More information about the Digitalmars-d-learn
mailing list