Skip to content
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

Parse escaped chars #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
24 changes: 23 additions & 1 deletion Source/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,29 @@ open class JSON {
}
}

let parsedString = String(jsonString[startingIndex ..< index])
var parsedString = String(jsonString[startingIndex ..< index])
Copy link
Owner

Choose a reason for hiding this comment

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

This could stay as a constant and be given a different name and built upon, e.g.
let initialParsedString = String()
let parsedString = initialParsedString.someOperation()

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure if this is useful. I mean how would you implement this for the hexEncodedUnicodeCharRegex parsedString? Feel free to push your solution on this branch.

.replacingOccurrences(of: "\\\\", with: "\\", range: nil)
.replacingOccurrences(of: "\\n", with: "\n", range: nil)
.replacingOccurrences(of: "\\t", with: "\t", range: nil)
.replacingOccurrences(of: "\\r", with: "\r", range: nil)
.replacingOccurrences(of: "\\\"", with: "\"", range: nil)
if let regex = try? NSRegularExpression(pattern: "\\\\u[0-9A-Fa-f]{4}") {
schorschii marked this conversation as resolved.
Show resolved Hide resolved
let parsedStringNs = parsedString as NSString
for match in regex.matches(in: parsedString, range: NSMakeRange(0, parsedStringNs.length)) {
let matchString = parsedStringNs.substring(with: match.range) as String
let matchStringHex = matchString.suffix(4)
var replaceData = Data()
if let byte = UInt8(matchStringHex.prefix(2), radix: 16), byte > 0 {
replaceData.append(contentsOf: [byte])
}
if let byte = UInt8(matchStringHex.suffix(2), radix: 16), byte > 0 {
replaceData.append(contentsOf: [byte])
}
if let replacementString = String(data: replaceData, encoding: .isoLatin1) {
parsedString = parsedString.replacingOccurrences(of: matchString, with: replacementString)
}
}
}

guard index != jsonString.endIndex else {
index = startingIndex
Expand Down
10 changes: 7 additions & 3 deletions Tests/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ class StringTests: TisanderTest {
XCTAssertEqual(json[0] as? String, "\\a")
}

func testEscapedQuoteString() {
func testEscapedCharsString() {
let input = """
["\\\""]
["\\n", "\\t", "\\r", "\\\"", "\\u00fc"]
"""
var json: Value
do { json = try JSON.parse(string: input) } catch let e { XCTFail((e as? SerializationError)?.rawValue ?? "Unknown exception"); return }

XCTAssertEqual(json[0] as? String, "\\\"")
XCTAssertEqual(json[0] as? String, "\n")
XCTAssertEqual(json[1] as? String, "\t")
XCTAssertEqual(json[2] as? String, "\r")
XCTAssertEqual(json[3] as? String, "\"")
XCTAssertEqual(json[4] as? String, "ü")
}

// func testEscapedSlashString() {
Expand Down