Skip to content

Add handling of escaped backslashes in expanded string - closes #461 #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions source/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,15 @@ auto expand_string_literal(
"\"", // end sequence
string_parts::on_both_ends}; // add opening and closing sequence to generated string

bool escape = false;
// Now we're on the first character of the string itself
for (
;
pos < length && (text[pos] != '"' || (text[pos-1] == '\\' && pos>=2 && text[pos-2] != '\\'));
pos < length && !(!escape && text[pos] == '"');
++pos
)
{
escape = (text[pos] == '\\' && !escape);
// Find the next )$
if (
text[pos] == '$'
Expand Down Expand Up @@ -426,7 +428,13 @@ auto expand_string_literal(

// Then put interpolated chunk into ret
auto chunk = std::string{text.substr(open, pos - open)};
replace_all(chunk, "\\\"", "\"");
{ // unescape chunk string
auto last_it = std::copy_if(std::begin(chunk), std::end(chunk), std::begin(chunk), [escape = false](const auto& e) mutable {
escape = !escape && e == '\\';
return !escape;
});
chunk.erase(last_it, std::end(chunk));
}
parts.add_code("cpp2::to_string" + chunk);

current_start = pos+1;
Expand Down