Fix JSON pretty printer string escaping

This commit is contained in:
Shadowfacts 2020-08-12 19:17:14 -04:00
parent f7310471bf
commit c96e093c72
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 17 additions and 1 deletions

View File

@ -89,7 +89,23 @@ class JSONPrettyPrinter {
}
private func escape(_ str: String) -> String {
return str.replacingOccurrences(of: "\"", with: "\\\"")
var str = str
var index = str.startIndex
while index < str.endIndex {
let c = str[index]
if c == "\\" || c == "\"" {
str.replaceSubrange(index..<str.index(after: index), with: "\\\(c)")
index = str.index(after: index)
} else if c == "\n" {
str.replaceSubrange(index..<str.index(after: index), with: "\\n")
index = str.index(after: index)
} else if c == "\r" {
str.replaceSubrange(index..<str.index(after: index), with: "\\r")
index = str.index(after: index)
}
index = str.index(after: index)
}
return str
}
}