Thread overview
String "dequote" in phobos?
May 13, 2021
cc
May 13, 2021
Paul Backus
May 13, 2021
Imperatorn
May 13, 2021
cc
May 13, 2021
kdevel
May 13, 2021
Imperatorn
May 13, 2021
Anonymouse
May 13, 2021

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:

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?
May 13, 2021

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:

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 don't think there's anything like this in the standard library. If you write your own, consider publishing it on code.dlang.org.

May 13, 2021

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:

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?

Wouldn't this just this do that? 🤔

string dequote(string s)
{
    return s[1..$-1];
}
May 13, 2021

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:

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 unquoted1 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.

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.

May 13, 2021

On Thursday, 13 May 2021 at 16:40:29 UTC, Imperatorn wrote:

>

Wouldn't this just this do that? 🤔

string dequote(string s)
{
    return s[1..$-1];
}

The idea would be for situations where it isn't known in advance whether the string is quoted, if it is quoted properly, and whether there are escaped quotes within the string that need to be un-escaped. Additionally some data sources may handle escaping quotes in strings differently (e.g. \" vs "")

May 13, 2021
On Thursday, 13 May 2021 at 16:40:29 UTC, Imperatorn wrote:
> Wouldn't this just this do that? 🤔
>
> ```d
> string dequote(string s)
> {
>     return s[1..$-1];
> }
> ```

1. Your code throws Range errors if s.length < 2.
2. assert(dequote(`"fo\"o"`) == `fo"o`) fails
3. dequote(`"fo"o"`) does not throw.
May 13, 2021
On Thursday, 13 May 2021 at 17:13:40 UTC, kdevel wrote:
> On Thursday, 13 May 2021 at 16:40:29 UTC, Imperatorn wrote:
>> Wouldn't this just this do that? 🤔
>>
>> ```d
>> string dequote(string s)
>> {
>>     return s[1..$-1];
>> }
>> ```
>
> 1. Your code throws Range errors if s.length < 2.
> 2. assert(dequote(`"fo\"o"`) == `fo"o`) fails
> 3. dequote(`"fo"o"`) does not throw.

WI spent 2 seconds thinking about it and 18 seconds typing on my phone so I'm not so surprised it didn't work. It did "Ok" for a 20 sec attempt tho 😁