Fix JSON pretty printer string escaping
This commit is contained in:
parent
f7310471bf
commit
c96e093c72
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue