Skip to content

Commit fbd4e01

Browse files
committed
Fix isStringLiteral with unicode strings
1 parent abeefa2 commit fbd4e01

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/dparse/strings.d

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@ bool isStringLiteral(const(char)[] literal, out char stringCloseChar,
6969

7070
if (parseEscapes)
7171
{
72-
// check if end escapes the quote, making this an invalid string
73-
auto end = literal[0 .. $ - 1].lastIndexOfNeither("\\");
74-
if (end != -1)
72+
size_t countBackslashes = 0;
73+
foreach_reverse (dchar c; literal[0 .. $ - 1])
7574
{
76-
// don't need to subtract 1
77-
size_t countBackslashes = literal.length - end;
78-
79-
if ((countBackslashes % 2) != 0)
80-
return false; // uneven backslash count -> invalid end
75+
if (c != '\\')
76+
break;
77+
countBackslashes++;
8178
}
79+
80+
// check if end escapes the quote, making this an invalid string
81+
if ((countBackslashes % 2) != 0)
82+
return false; // uneven backslash count -> invalid end
8283
}
8384

8485
return true;
@@ -98,6 +99,7 @@ bool isStringLiteral(const(char)[] literal)
9899
unittest
99100
{
100101
assert(isStringLiteral(`"hello"`));
102+
assert(isStringLiteral(`"ñ"`));
101103
assert(isStringLiteral(`"hello world!"`));
102104
assert(isStringLiteral(`r"hello world!"c`));
103105
assert(isStringLiteral(`r"hello world!"d`));
@@ -107,6 +109,8 @@ unittest
107109
assert(!isStringLiteral(`"\\\"`));
108110
assert(isStringLiteral(`"\\\\"`));
109111
assert(isStringLiteral(`"a\\\\"`));
112+
assert(!isStringLiteral(`"ñ\"`));
113+
assert(isStringLiteral(`"ñ\\"`));
110114
assert(isStringLiteral(`""`));
111115
assert(isStringLiteral(`q""`));
112116
assert(isStringLiteral(`x""`));
@@ -563,6 +567,7 @@ unittest
563567
assert(unescapeDoubleQuotedContent(`hello\tworld`) == "hello\tworld");
564568
assert(unescapeDoubleQuotedContent(`hello\u200bworld`) == "hello\u200bworld");
565569
assert(unescapeDoubleQuotedContent(`hello \"\\ok`) == "hello \"\\ok");
570+
assert(unescapeDoubleQuotedContent(`こんにちは \"\\ñ`) == "こんにちは \"\\ñ");
566571
}
567572

568573
private string parseHexStringContent(

0 commit comments

Comments
 (0)