unescapeString

Unescapes a D string, effectively being the same as mixing in the string into some function call, but only for single string literals.

Strips quotes, prefixes and suffixes, interprets escape sequences in normal double quoted strings and interprets hex strings. Returns simple slices for non-escaped strings.

It's undefined how invalid/malformed strings are evaluated.

string
unescapeString
(
string input
)

Bugs

doesn't check for validity of token strings, doesn't interpret named character entity escape sequences, (HTML-kind escape sequences) doesn't check nesting level of delimited strings.

Examples

1 	assert(unescapeString(q{r"I am Oz"}) == r"I am Oz");
2 	assert(unescapeString(q{r"c:\games\Sudoku.exe"}) == r"c:\games\Sudoku.exe");
3 	assert(unescapeString(q{r"ab\n"}) == r"ab\n");
4 
5 	assert(unescapeString(q{`the Great and Powerful.`}) == `the Great and Powerful.`);
6 	assert(unescapeString(q{`c:\games\Empire.exe`}) == `c:\games\Empire.exe`);
7 	assert(unescapeString(q{`The "lazy" dog`}) == `The "lazy" dog`);
8 	assert(unescapeString(q{`a"b\n`}) == `a"b\n`);
9 
10 	assert(unescapeString(q{"Who are you?"}) == "Who are you?");
11 	assert(unescapeString(q{"c:\\games\\Doom.exe"}) == "c:\\games\\Doom.exe");
12 	assert(unescapeString(q{"ab\n"}) == "ab\n");
13 
14 	assert(unescapeString(`x"0A"`) == hexString!"0A");
15 	assert(unescapeString(`x"00 FBCD 32FD 0A"`) == hexString!"00 FBCD 32FD 0A");
16 
17 	assert(unescapeString(`q"(foo(xxx))"`) == q"(foo(xxx))");
18 	assert(unescapeString(`q"[foo{]"`) == q"[foo{]");
19 	assert(unescapeString(`q"<foo{>"`) == q"<foo{>");
20 	assert(unescapeString(`q"{foo(}"`) == q"{foo(}");
21 	assert(unescapeString(`q"EOS
22 This
23 is a multi-line
24 heredoc string
25 EOS"`) == q"EOS
26 This
27 is a multi-line
28 heredoc string
29 EOS");
30 	assert(unescapeString(`q"/foo]/"`) == `foo]`);
31 
32 	assert(unescapeString(`q{this is the voice of}`) == q{this is the voice of});
33 	assert(unescapeString(`q{/*}*/ }`) == q{/*}*/ });
34 	assert(unescapeString(`q{ world(q{control}); }`) == q{ world(q{control}); });
35 	assert(unescapeString(`q{ __TIME__ }`) == q{ __TIME__ });
36 
37 	assert(unescapeString(q{"hello"c}) == "hello");
38 	assert(unescapeString(q{"hello"w}) == "hello");
39 	assert(unescapeString(q{"hello"d}) == "hello");
40 
41 	assert(unescapeString(`""`) == "");
42 	assert(unescapeString(`"hello\'world\"cool\""`) == "hello\'world\"cool\"");
43 	assert(unescapeString(`"\x0A"`) == "\x0A");
44 	assert(unescapeString(`"\u200b"`) == "\u200b");
45 	assert(unescapeString(`"\U0001F4A9"`) == "\U0001F4A9");
46 	assert(unescapeString(`"\0"`) == "\0");
47 	assert(unescapeString(`"\1"`) == "\1");
48 	assert(unescapeString(`"\12"`) == "\12");
49 	assert(unescapeString(`"\127"`) == "\127");
50 	assert(unescapeString(`"\1278"`) == "\1278");
51 	assert(unescapeString(`"\12a8"`) == "\12a8");
52 	assert(unescapeString(`"\1a28"`) == "\1a28");
53 	assert(unescapeString(`x"afDE"`) == "\xaf\xDE");
54 	assert(unescapeString("\"hello\nworld\rfoo\r\nbar\u2028ok\u2029\"")
55 			== "hello\nworld\nfoo\nbar\nok\n");

Meta