percentDecode

Percent-decode a string.

Url components cannot contain non-ASCII characters, and there are very few characters that are safe to include as Url components. Domain names using Unicode values use Punycode. For everything else, there is percent encoding.

This explicitly ensures that the result is a valid UTF-8 string.

pure @safe
string
percentDecode
(
string encoded
)

Examples

assert(percentDecode("IDontNeedNoPercentDecoding") == "IDontNeedNoPercentDecoding");
assert(percentDecode("~~--..__") == "~~--..__");
assert(percentDecode("0123456789") == "0123456789");

string e;

e = percentDecode("%E2%98%83");
assert(e == "☃", "expected a snowman but got" ~ e);

e = percentDecode("%e2%98%83");
assert(e == "☃", "expected a snowman but got" ~ e);

try {
    // %ES is an invalid percent sequence: 'S' is not a hex digit.
    percentDecode("%es");
    assert(false, "expected exception not thrown");
} catch (UrlException) {
}

try {
    percentDecode("%e");
    assert(false, "expected exception not thrown");
} catch (UrlException) {
}

Meta