Skip to content

Commit 1ac7952

Browse files
committed
Fixing double quote handling in verbatim strings, re: #99
1 parent fc9c321 commit 1ac7952

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

‎ReadableExpressions.UnitTests/WhenTranslatingConstants.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ public void ShouldTranslateAVerbatimStringWithAnEscapedTabCharacter()
6363
translated.ShouldBe(@"""hello\\tthere!""");
6464
}
6565

66+
// See https://.com/agileobjects/ReadableExpressions/issues/99
67+
[Fact]
68+
public void ShouldTranslateAVerbatimStringWithDoubleQuotes()
69+
{
70+
const string VERBATIM = @"
71+
12
72+
""1""2
73+
";
74+
var stringConstant = Constant(VERBATIM, typeof(string));
75+
76+
var translated = stringConstant.ToReadableString();
77+
78+
translated.ShouldBe(@"@""
79+
12
80+
""""1""""2
81+
""");
82+
83+
}
84+
6685
[Fact]
6786
public void ShouldTranslateABoolean()
6887
{
@@ -532,7 +551,7 @@ public void ShouldTranslateAStringArrayICollectionConstant()
532551
var arrayConstant = Constant(new[] { "Five", "Five", "Five" }, typeof(ICollection<string>));
533552

534553
var translated = arrayConstant.ToReadableString();
535-
554+
536555
translated.ShouldBe("(ICollection<string>)new[] { \"Five\", \"Five\", \"Five\" }");
537556
}
538557

‎ReadableExpressions/Translations/ConstantTranslation.cs

+24-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Initialisations;
1515
using NetStandardPolyfills;
1616
using static System.Convert;
17+
using static System.Environment;
1718
using static System.Globalization.CultureInfo;
1819
#if NET35
1920
using static Microsoft.Scripting.Ast.ExpressionType;
@@ -160,12 +161,14 @@ private static bool TryTranslateFromTypeCode(
160161
return true;
161162

162163
case NetStandardTypeCode.String:
163-
var stringValue = ((string)constant.Value)
164-
.Replace(@"\", @"\\")
165-
.Replace("\0", @"\0")
166-
.Replace(@"""", @"\""");
167-
164+
var stringValue = GetStringConstant(constant, out var isVerbatim);
168165
stringValue = "\"" + stringValue + "\"";
166+
167+
if (isVerbatim)
168+
{
169+
stringValue = "@" + stringValue;
170+
}
171+
169172
translation = FixedValueTranslation(stringValue, typeof(string), Text, context);
170173
return true;
171174
}
@@ -213,6 +216,13 @@ private static ITranslation GetDoubleTranslation(
213216
return FixedValueTranslation(stringValue + "d", constant.Type, Numeric, context);
214217
}
215218

219+
private static ITranslation GetLongTranslation(
220+
ConstantExpression constant,
221+
ITranslationContext context)
222+
{
223+
return FixedValueTranslation((long)constant.Value + "L", constant.Type, Numeric, context);
224+
}
225+
216226
private static ITranslation GetFloatTranslation(
217227
ConstantExpression constant,
218228
ITranslationContext context)
@@ -226,11 +236,17 @@ private static ITranslation GetFloatTranslation(
226236
return FixedValueTranslation(stringValue + "f", constant.Type, Numeric, context);
227237
}
228238

229-
private static ITranslation GetLongTranslation(
239+
private static string GetStringConstant(
230240
ConstantExpression constant,
231-
ITranslationContext context)
241+
out bool isVerbatim)
232242
{
233-
return FixedValueTranslation((long)constant.Value + "L", constant.Type, Numeric, context);
243+
var stringValue = (string)constant.Value;
244+
isVerbatim = stringValue.Contains(NewLine);
245+
246+
return stringValue
247+
.Replace(@"\", @"\\")
248+
.Replace("\0", @"\0")
249+
.Replace(@"""", isVerbatim ? @"""""" : @"\""");
234250
}
235251

236252
private static bool TryGetTypeTranslation(

0 commit comments

Comments
 (0)