@@ -143,7 +143,7 @@ private ExpressionSyntax ConvertInitializer(VariableDeclaratorSyntax declarator)
143143 internal ExpressionSyntax GetLiteralExpression ( object value , string textForUser = null )
144144 {
145145 if ( value is string valueTextForCompiler ) {
146- textForUser = GetStringTextForUser ( textForUser , valueTextForCompiler ) ;
146+ textForUser = GetQuotedStringTextForUser ( textForUser , valueTextForCompiler ) ;
147147 return SyntaxFactory . LiteralExpression ( Microsoft . CodeAnalysis . CSharp . SyntaxKind . StringLiteralExpression ,
148148 SyntaxFactory . Literal ( textForUser , valueTextForCompiler ) ) ;
149149 }
@@ -194,25 +194,15 @@ internal ExpressionSyntax GetLiteralExpression(object value, string textForUser
194194 throw new ArgumentOutOfRangeException ( nameof ( value ) , value , null ) ;
195195 }
196196
197- internal string GetStringTextForUser ( string textForUser , string valueTextForCompiler )
197+ internal string GetQuotedStringTextForUser ( string textForUser , string valueTextForCompiler )
198198 {
199+ var sourceUnquotedTextForUser = Unquote ( textForUser ) ;
199200 var worthBeingAVerbatimString = IsWorthBeingAVerbatimString ( valueTextForCompiler ) ;
200- if ( worthBeingAVerbatimString )
201- {
202- var valueWithReplacements = EscapeQuotes ( textForUser , valueTextForCompiler , true ) ;
203- return $ "@\" { valueWithReplacements } \" ";
204- }
201+ var destQuotedTextForUser =
202+ $ "\" { EscapeQuotes ( sourceUnquotedTextForUser , valueTextForCompiler , worthBeingAVerbatimString ) } \" ";
203+
204+ return worthBeingAVerbatimString ? "@" + destQuotedTextForUser : destQuotedTextForUser ;
205205
206- string unquotedTextForUser = Unquote ( textForUser ) ;
207- return "\" " + EscapeQuotes ( unquotedTextForUser , valueTextForCompiler , false ) + "\" " ;
208- }
209-
210- private static string Unquote ( string quotedText )
211- {
212- int firstQuoteIndex = quotedText . IndexOf ( "\" " ) ;
213- int lastQuoteIndex = quotedText . LastIndexOf ( "\" " ) ;
214- var unquoted = quotedText . Substring ( firstQuoteIndex + 1 , lastQuoteIndex - firstQuoteIndex - 1 ) ;
215- return unquoted ;
216206 }
217207
218208 internal string EscapeQuotes ( string unquotedTextForUser , string valueTextForCompiler , bool isVerbatimString )
@@ -224,6 +214,14 @@ internal string EscapeQuotes(string unquotedTextForUser, string valueTextForComp
224214 }
225215 }
226216
217+ private static string Unquote ( string quotedText )
218+ {
219+ int firstQuoteIndex = quotedText . IndexOf ( "\" " ) ;
220+ int lastQuoteIndex = quotedText . LastIndexOf ( "\" " ) ;
221+ var unquoted = quotedText . Substring ( firstQuoteIndex + 1 , lastQuoteIndex - firstQuoteIndex - 1 ) ;
222+ return unquoted ;
223+ }
224+
227225 public bool IsWorthBeingAVerbatimString ( string s1 )
228226 {
229227 return s1 . IndexOfAny ( new [ ] { '\r ' , '\n ' , '\\ ' } ) > - 1 ;
0 commit comments