Skip to content

Commit e294d17

Browse files
authored
Add handling of escaped backslashes in expanded string (#464)
The current change iterate over entire string end removes extra backslashes that are used to indicate escape sequence. Closes #461
1 parent 37c2274 commit e294d17

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

source/lex.h

+10-2
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,13 @@ 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 last_it = std::copy_if(std::begin(chunk), std::end(chunk), std::begin(chunk), [escape = false](const auto& e) mutable {
433+
escape = !escape && e == '\\';
434+
return !escape;
435+
});
436+
chunk.erase(last_it, std::end(chunk));
437+
}
430438
parts.add_code("cpp2::to_string" + chunk);
431439

432440
current_start = pos+1;

0 commit comments

Comments
 (0)