Skip to content

Commit

Permalink
Merge pull request #59 from swift-xcode/escaping
Browse files Browse the repository at this point in the history
fix escaping of ""
  • Loading branch information
yonaskolb authored Aug 28, 2017
2 parents 0b752d8 + 29c508f commit eea7fbb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/CommentedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ struct CommentedString {

// escape quotes
var range: Range<String.Index>?
if escaped.isQuoted {
if escaped.isQuoted && escaped.characters.count > 1 {
range = escaped.index(after: escaped.startIndex)..<escaped.index(before: escaped.endIndex)
}
escaped = escaped.replacingOccurrences(of: "([^\\\\])(\")", with: "$1\\\\$2", options: .regularExpression, range: range)
escaped = escaped.replacingOccurrences(of: "\"", with: "\\\"", range: range).replacingOccurrences(of: "\\\\\"", with: "\\\"")

if !escaped.isQuoted && escaped.rangeOfCharacter(from: CommentedString.invalidCharacters) != nil {
escaped = escaped.quoted
Expand Down
8 changes: 7 additions & 1 deletion Tests/xcodeprojTests/PBXProjWriterSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ class PBXProjWriterSpecs: XCTestCase {
"a\na".quoted: "a\(escapedNewline)a".quoted,
"a\(escapedNewline)a": "a\(escapedNewline)a".quoted,
"a\(escapedNewline)a".quoted: "a\(escapedNewline)a".quoted,
"\"": escapedQuote.quoted,
"\"\"": "\"\"",
"\"\"\"\"": "\(escapedQuote)\(escapedQuote)".quoted,
"a=\"\"": "a=\(escapedQuote)\(escapedQuote)".quoted,
]

for (initial, expected) in values {
let escapedString = CommentedString(initial).validString
XCTAssertEqual(escapedString, expected)
if escapedString != expected {
XCTFail("Escaped strings are not equal:\ninitial: \(initial)\nexpect: \(expected)\nescaped: \(escapedString) ")
}
}
}

Expand Down

0 comments on commit eea7fbb

Please sign in to comment.