Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 20, 2010 [phobos] enquote() -- a small function for std.metastrings | ||||
---|---|---|---|---|
| ||||
I've found that the function below is invaluable for CTFE programming; it gets rid of a huge fraction of the ugliness. I think this function, or something like it, should be in std.metastrings. ================== /** Escape any quotes and backslashes inside the given string, * prefixing them with the given escape sequence. Use `\` to escape * once, `\\\` to escape twice. */ string enquote(string instr, string escape = `\`) { // This function is critical for compilation speed. // Need to minimise the number of allocations. // It's worth using copy-on-write even for CTFE. for(int i = 0; i < instr.length; ++i) { if (instr[i] == '"' || instr[i] == '\\') { string str = instr[0..i] ~ escape; int m = i; foreach(int k, char c; instr[i+1..$]) { if (c=='"' || c=='\\') { str ~= instr[m..i+1+k] ~ escape; m = i+k+1; } } return str ~ instr[m..$]; } } return instr; } unittest { assert(enquote(`"a\"`)==`\"a\\\"`); assert(enquote(`abc`)==`abc`); } |
April 20, 2010 [phobos] enquote() -- a small function for std.metastrings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Great. I suggest we actually put that in std.string such that run-time code could use it as well.
Andrei
On 04/20/2010 10:13 AM, Don Clugston wrote:
> I've found that the function below is invaluable for CTFE programming;
> it gets rid of a huge fraction of the ugliness.
> I think this function, or something like it, should be in std.metastrings.
>
> ==================
>
> /** Escape any quotes and backslashes inside the given string,
> * prefixing them with the given escape sequence. Use `\` to escape
> * once, `\\\` to escape twice.
> */
> string enquote(string instr, string escape = `\`)
> {
> // This function is critical for compilation speed.
> // Need to minimise the number of allocations.
> // It's worth using copy-on-write even for CTFE.
>
> for(int i = 0; i< instr.length; ++i)
> {
> if (instr[i] == '"' || instr[i] == '\\')
> {
> string str = instr[0..i] ~ escape;
> int m = i;
> foreach(int k, char c; instr[i+1..$])
> {
> if (c=='"' || c=='\\')
> {
> str ~= instr[m..i+1+k] ~ escape;
> m = i+k+1;
> }
> }
> return str ~ instr[m..$];
> }
> }
> return instr;
> }
>
> unittest {
> assert(enquote(`"a\"`)==`\"a\\\"`);
> assert(enquote(`abc`)==`abc`);
> }
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
|
April 20, 2010 [phobos] enquote() -- a small function for std.metastrings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Why use that instead of raw strings?
Don Clugston wrote:
> I've found that the function below is invaluable for CTFE programming; it gets rid of a huge fraction of the ugliness.
>
>
|
April 20, 2010 [phobos] enquote() -- a small function for std.metastrings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 20 April 2010 19:07, Walter Bright <walter at digitalmars.com> wrote: > Why use that instead of raw strings? Because of cases where you have recursive mixins. Eg, return `mixin("int" ~mixin(\"abc\") ~";)`; You can use ` `-delimited strings, but only once. So you end up with some ugly morass of `"` ~ or `\"` everywhere. It gets quite disgusting. With enquote, that example is: return `mixin("int" ~ enquote(mixin("abc")) )`; Then if you want to put it in another mixin, it becomes: return `mixin(enquote(mixin("int" ~ enquote(mixin("abc")) ))`; but without enquote, it is: return `mixin("mixin(\"int\" ~mixin(\\"abc\\") ~\";)")`; which is practically unreadable. > > Don Clugston wrote: >> >> I've found that the function below is invaluable for CTFE programming; it gets rid of a huge fraction of the ugliness. >> >> > > _______________________________________________ > phobos mailing list > phobos at puremagic.com > http://lists.puremagic.com/mailman/listinfo/phobos > |
April 21, 2010 [phobos] enquote() -- a small function for std.metastrings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | I thought the plan was to get rid of std.string?
-Lars
Andrei Alexandrescu wrote:
> Great. I suggest we actually put that in std.string such that run-time code could use it as well.
>
> Andrei
>
> On 04/20/2010 10:13 AM, Don Clugston wrote:
>> I've found that the function below is invaluable for CTFE programming;
>> it gets rid of a huge fraction of the ugliness.
>> I think this function, or something like it, should be in
>> std.metastrings.
>>
>> ==================
>>
>> [...]
|
April 21, 2010 [phobos] enquote() -- a small function for std.metastrings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars Tandle Kyllingstad | Well let's put it there for the time being.
Andrei
On 04/21/2010 06:49 AM, Lars Tandle Kyllingstad wrote:
> I thought the plan was to get rid of std.string?
>
> -Lars
>
>
> Andrei Alexandrescu wrote:
>> Great. I suggest we actually put that in std.string such that run-time code could use it as well.
>>
>> Andrei
>>
>> On 04/20/2010 10:13 AM, Don Clugston wrote:
>>> I've found that the function below is invaluable for CTFE programming;
>>> it gets rid of a huge fraction of the ugliness.
>>> I think this function, or something like it, should be in
>>> std.metastrings.
>>>
>>> ==================
>>>
>>> [...]
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
|
Copyright © 1999-2021 by the D Language Foundation