diff --git a/source/lex.h b/source/lex.h index b5fa33f60a..6fe5529c30 100644 --- a/source/lex.h +++ b/source/lex.h @@ -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] == '$' @@ -426,7 +428,22 @@ 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 escape = false; + auto from = chunk.begin(); + auto to = chunk.begin(); + auto end = chunk.end(); + for (; from != end && to != end; ++from) { + if (!escape && *from == '\\') { + escape = true; + continue; + } + escape = false; + *to = *from; + ++to; + } + chunk.erase(to, end); + } parts.add_code("cpp2::to_string" + chunk); current_start = pos+1;