Skip to content

Escape backslashes properly when converting to the DSL #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2022

Conversation

DaveEwing
Copy link
Contributor

Fix an issue where we weren't properly escaping backslashes in quote strings when converting to the DSL.

@DaveEwing
Copy link
Contributor Author

@swift-ci please test

@@ -578,7 +578,7 @@ extension PrettyPrinter {
extension String {
// TODO: Escaping?
fileprivate var _quoted: String {
"\"\(self._replacing("\"", with: "\\\""))\""
"\"\(self._replacing("\\", with: "\\\\")._replacing("\"", with: "\\\""))\""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"\"\(self._replacing("\\", with: "\\\\")._replacing("\"", with: "\\\""))\""
"\"\(self._replacing(#"\"#, with: #"\\"#)._replacing(#"""#, with: #"\""#))\""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can emit it as a raw string if it has any backslashes or quotes inside it. We could do a quick check to count the number of hashes prior to a backslash and emit a raw string with one additional one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about counting hashes, but it seemed like it wasn't worth the effort. But I'm all for using raw strings in your suggested change. I was mostly preserving the way it had been written.

func testQuoting() throws {
try testConversion(#"\\\"a\""#, #"""
Regex {
"\\\"a\""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right conversion? The input is raw, so IIUC it has 3 full backslashes, then a quote then a full backslash and another quote.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the test. The \s in \" in the regex to convert is ignored when parsing, but not necessary.

@DaveEwing
Copy link
Contributor Author

@swift-ci please test

@DaveEwing DaveEwing merged commit 935e137 into main May 28, 2022
@DaveEwing DaveEwing deleted the dewing/93676693_Quote_backslash branch May 28, 2022 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants