@@ -1799,32 +1799,37 @@ void Lexer::lexStringLiteral(unsigned CustomDelimiterLen) {
1799
1799
" Single quoted string cannot have custom delimitor, nor multiline" );
1800
1800
assert (*TokStart == ' \' ' && CurPtr[-1 ] == ' \' ' );
1801
1801
1802
- StringRef orig (TokStart, CurPtr - TokStart);
1803
- llvm::SmallString<32 > replacement;
1804
- replacement += ' "' ;
1805
- std::string str = orig.slice (1 , orig.size () - 1 ).str ();
1806
- std::string quot = " \" " ;
1807
- size_t pos = 0 ;
1808
- while (pos != str.length ()) {
1809
- if (str.at (pos) == ' \\ ' ) {
1810
- if (str.at (pos + 1 ) == ' \' ' ) {
1811
- // Un-escape escaped single quotes.
1812
- str.replace (pos, 2 , " '" );
1813
- ++pos;
1814
- } else {
1815
- // Skip over escaped characters.
1816
- pos += 2 ;
1802
+ SmallString<32 > replacement;
1803
+ replacement.push_back (' "' );
1804
+ const char *Ptr = TokStart + 1 ;
1805
+ const char *OutputPtr = Ptr;
1806
+
1807
+ while (*Ptr++ != ' \' ' ) {
1808
+ if (Ptr[-1 ] == ' \\ ' ) {
1809
+ if (*Ptr == ' \' ' ) {
1810
+ replacement.append (OutputPtr, Ptr - 1 );
1811
+ OutputPtr = Ptr + 1 ;
1812
+ // Un-escape single quotes.
1813
+ replacement.push_back (' \' ' );
1814
+ } else if (*Ptr == ' (' ) {
1815
+ // Preserve the contents of interpolation.
1816
+ Ptr = skipToEndOfInterpolatedExpression (Ptr + 1 , replacement.end (),
1817
+ /* IsMultiline=*/ false );
1818
+ assert (*Ptr == ' )' );
1817
1819
}
1818
- } else if (str.at (pos) == ' "' ) {
1819
- str.replace (pos, 1 , " \\\" " );
1820
- // Advance past the newly added ["\""].
1821
- pos += 2 ;
1822
- } else {
1823
- ++pos;
1820
+ // Skip over escaped characters.
1821
+ ++Ptr;
1822
+ } else if (Ptr[-1 ] == ' "' ) {
1823
+ replacement.append (OutputPtr, Ptr - 1 );
1824
+ OutputPtr = Ptr;
1825
+ // Escape double quotes.
1826
+ replacement.append (" \\\" " );
1824
1827
}
1825
1828
}
1826
- replacement += StringRef (str);
1827
- replacement += ' "' ;
1829
+ assert (Ptr == CurPtr);
1830
+ replacement.append (OutputPtr, Ptr - 1 );
1831
+ replacement.push_back (' "' );
1832
+
1828
1833
diagnose (TokStart, diag::lex_single_quote_string)
1829
1834
.fixItReplaceChars (getSourceLoc (TokStart), getSourceLoc (CurPtr),
1830
1835
replacement);
0 commit comments