From 451c9079c7803cc311ca2fb95961a7b38f0b2d53 Mon Sep 17 00:00:00 2001 From: David Ewing Date: Tue, 24 May 2022 12:24:18 -0400 Subject: [PATCH 1/3] Fix an issue where we weren't properly escaping backslashes in quoted strings when converting to the DSL. --- Sources/_StringProcessing/PrintAsPattern.swift | 2 +- Tests/RegexTests/RenderDSLTests.swift | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/_StringProcessing/PrintAsPattern.swift b/Sources/_StringProcessing/PrintAsPattern.swift index 3058a3956..4c31e39a8 100644 --- a/Sources/_StringProcessing/PrintAsPattern.swift +++ b/Sources/_StringProcessing/PrintAsPattern.swift @@ -578,7 +578,7 @@ extension PrettyPrinter { extension String { // TODO: Escaping? fileprivate var _quoted: String { - "\"\(self._replacing("\"", with: "\\\""))\"" + "\"\(self._replacing("\\", with: "\\\\")._replacing("\"", with: "\\\""))\"" } } diff --git a/Tests/RegexTests/RenderDSLTests.swift b/Tests/RegexTests/RenderDSLTests.swift index 4d244e2cc..cd230d87f 100644 --- a/Tests/RegexTests/RenderDSLTests.swift +++ b/Tests/RegexTests/RenderDSLTests.swift @@ -114,4 +114,12 @@ extension RenderDSLTests { """) } } + + func testQuoting() throws { + try testConversion(#"\\\"a\""#, #""" + Regex { + "\\\"a\"" + } + """#) + } } From 4c7f5954ca2653bf3f094e45c01c4466b289ca4b Mon Sep 17 00:00:00 2001 From: David Ewing Date: Wed, 25 May 2022 16:30:15 -0400 Subject: [PATCH 2/3] From review: use raw strings for find/replace when quoting a string. --- Sources/_StringProcessing/PrintAsPattern.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_StringProcessing/PrintAsPattern.swift b/Sources/_StringProcessing/PrintAsPattern.swift index 4c31e39a8..2abcf49b5 100644 --- a/Sources/_StringProcessing/PrintAsPattern.swift +++ b/Sources/_StringProcessing/PrintAsPattern.swift @@ -578,7 +578,7 @@ extension PrettyPrinter { extension String { // TODO: Escaping? fileprivate var _quoted: String { - "\"\(self._replacing("\\", with: "\\\\")._replacing("\"", with: "\\\""))\"" + "\"\(self._replacing(#"\"#, with: #"\\"#)._replacing(#"""#, with: #"\""#))\"" } } From 21b30cbe71d80de83dcfeb6a4d3df36b261312c1 Mon Sep 17 00:00:00 2001 From: David Ewing Date: Wed, 25 May 2022 16:32:22 -0400 Subject: [PATCH 3/3] From review: remove an extra `\` from the regex pattern. (The regex parser ignores it in front of a `"`, but it's confusing.) --- Tests/RegexTests/RenderDSLTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/RegexTests/RenderDSLTests.swift b/Tests/RegexTests/RenderDSLTests.swift index cd230d87f..016b32f4a 100644 --- a/Tests/RegexTests/RenderDSLTests.swift +++ b/Tests/RegexTests/RenderDSLTests.swift @@ -116,7 +116,7 @@ extension RenderDSLTests { } func testQuoting() throws { - try testConversion(#"\\\"a\""#, #""" + try testConversion(#"\\"a""#, #""" Regex { "\\\"a\"" }