Skip to content

Commit bd22c59

Browse files
committed
Add handling of escaped backslashes in expanded string
The current change iterate over entire string end removes extra backslashes that are used to indicate escape sequence. Closes #461
1 parent c84bf47 commit bd22c59

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

source/lex.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,15 @@ auto expand_string_literal(
379379
"\"", // end sequence
380380
string_parts::on_both_ends}; // add opening and closing sequence to generated string
381381

382+
bool escape = false;
382383
// Now we're on the first character of the string itself
383384
for (
384385
;
385-
pos < length && (text[pos] != '"' || (text[pos-1] == '\\' && pos>=2 && text[pos-2] != '\\'));
386+
pos < length && !(!escape && text[pos] == '"');
386387
++pos
387388
)
388389
{
390+
escape = (text[pos] == '\\' && !escape);
389391
// Find the next )$
390392
if (
391393
text[pos] == '$'
@@ -426,7 +428,22 @@ auto expand_string_literal(
426428

427429
// Then put interpolated chunk into ret
428430
auto chunk = std::string{text.substr(open, pos - open)};
429-
replace_all(chunk, "\\\"", "\"");
431+
{ // unescape chunk string
432+
auto escape = false;
433+
auto from = chunk.begin();
434+
auto to = chunk.begin();
435+
auto end = chunk.end();
436+
for (; from != end && to != end; ++from) {
437+
if (!escape && *from == '\\') {
438+
escape = true;
439+
continue;
440+
}
441+
escape = false;
442+
*to = *from;
443+
++to;
444+
}
445+
chunk.erase(to, end);
446+
}
430447
parts.add_code("cpp2::to_string" + chunk);
431448

432449
current_start = pos+1;

0 commit comments

Comments
 (0)