-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
… strings when converting to the DSL.
@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: "\\\""))\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"\"\(self._replacing("\\", with: "\\\\")._replacing("\"", with: "\\\""))\"" | |
"\"\(self._replacing(#"\"#, with: #"\\"#)._replacing(#"""#, with: #"\""#))\"" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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\"" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…arser ignores it in front of a `"`, but it's confusing.)
@swift-ci please test |
Fix an issue where we weren't properly escaping backslashes in quote strings when converting to the DSL.