Skip to content

Commit

Permalink
Refine logic for using verbatim strings
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Sep 21, 2023
1 parent ab42a64 commit 3c98bfe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CodeConverter/CSharp/LiteralConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ private static string Unquote(string quotedText)

public static bool IsWorthBeingAVerbatimString(string s1)
{
return s1.IndexOfAny(new[] {'\r', '\n', '\\'}) > -1;
return s1.IndexOfAny(new[] {'\r', '\n', '\\'}) > -1 &&
// Don't use verbatim except for \r\n within a larger string
s1.Replace("\r\n", "") is {Length: > 0} x && x.IndexOfAny(new[] { '\r', '\n' }) == -1;
}

/// <summary>
Expand Down
17 changes: 15 additions & 2 deletions Tests/CSharp/ExpressionTests/XmlExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,26 @@ private void TestMethod()
new XElement(""Author"", ""Garghentini, Davide""),
new XElement(""Title"", ""XML Developer's Guide""),
new XElement(""Price"", ""44.95""),
new XElement(""Description"", ""\r\n An in-depth look at creating applications\r\n with "", new XElement(""technology"", ""XML""), "". For\r\n "", new XElement(""audience"", ""beginners""), "" or\r\n "", new XElement(""audience"", ""advanced""), "" developers.\r\n "")
new XElement(""Description"", @""
An in-depth look at creating applications
with "", new XElement(""technology"", ""XML""), @"". For
"", new XElement(""audience"", ""beginners""), @"" or
"", new XElement(""audience"", ""advanced""), @"" developers.
"")
),
new XElement(""Book"", new XAttribute(""id"", ""bk331""),
new XElement(""Author"", ""Spencer, Phil""),
new XElement(""Title"", ""Developing Applications with Visual Basic .NET""),
new XElement(""Price"", ""45.95""),
new XElement(""Description"", ""\r\n Get the expert insights, practical code samples,\r\n and best practices you need\r\n to advance your expertise with "", new XElement(""technology"", ""Visual\r\n Basic .NET""), "".\r\n Learn how to create faster, more reliable applications\r\n based on professional,\r\n pragmatic guidance by today's top "", new XElement(""audience"", ""developers""), "".\r\n "")
new XElement(""Description"", @""
Get the expert insights, practical code samples,
and best practices you need
to advance your expertise with "", new XElement(""technology"", @""Visual
Basic .NET""), @"".
Learn how to create faster, more reliable applications
based on professional,
pragmatic guidance by today's top "", new XElement(""audience"", ""developers""), @"".
"")
)
)
Expand Down

0 comments on commit 3c98bfe

Please sign in to comment.