On Thursday, 13 May 2021 at 14:10:08 UTC, cc wrote:
Does something to dequote (unquote? or what would you call it?)
a string exist in the standard library? I didn't see one in
std.string, just wondering before reinventing the wheel.
Something like:
```d
assert(dequote(`"foo"`) == "foo");
assert(dequote(`'foo'`) == "foo");
assert(dequote(`"foo's"`) == "foo's");
assert(dequote(`'foo "bar"'`) == `foo "bar"`);
assert(dequote(`"fo\"o"`) == `fo"o`);
dequote(`"fo"o"`); // bad quoting, maybe throw an exception
here or something?
```
I called mine `unquoted`[1] and `unsinglequoted`. Not very
imaginative, I concede.
I also assumed ASCII and fearlessly sliced `line[1..$-1]`, which
you may or may not be able to do.
```d
private T unenclosed(char token = '"', T)(const T line) pure
nothrow @nogc
if (isSomeString!T)
{
enum escaped = "\\" ~ token;
if (line.length < 2)
{
return line;
}
else if ((line[0] == token) && (line[$-1] == token))
{
if ((line.length >= 3) && (line[$-2..$] == escaped))
{
// End quote is escaped
return line;
}
return line[1..$-1].unenclosed!token;
}
else
{
return line;
}
}
pragma(inline, true)
T unquoted(T)(const T line) pure nothrow @nogc
{
return unenclosed!'"'(line);
}
unittest
{
assert(`"Lorem ipsum sit amet"`.unquoted == "Lorem ipsum sit
amet");
assert(`"""""Lorem ipsum sit amet"""""`.unquoted == "Lorem
ipsum sit amet");
// Unbalanced quotes are left untouched
assert(`"Lorem ipsum sit amet`.unquoted == `"Lorem ipsum sit
amet`);
assert(`"Lorem \"`.unquoted == `"Lorem \"`);
assert("\"Lorem \\\"".unquoted == "\"Lorem \\\"");
assert(`"\"`.unquoted == `"\"`);
}
pragma(inline, true)
T unsinglequoted(T)(const T line) pure nothrow @nogc
{
return unenclosed!'\''(line);
}
// ...
```
I'm not sure it's quite correct for all valid inputs but it
worked well enough for my purposes.
[1]: http://lu.dpldocs.info/lu.string.unquoted.html