Skip to content

Commit 207c5b9

Browse files
committed
Remove unused code and address code review
1 parent b046c76 commit 207c5b9

File tree

1 file changed

+37
-111
lines changed

1 file changed

+37
-111
lines changed

Sources/Foundation/JSONSerialization.swift

+37-111
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ open class JSONSerialization : NSObject {
187187
let newPtr = ptr[advanceBy..<ptr.count]
188188
let json = String(bytes: newPtr, encoding: encoding)!
189189
return try json.utf8.withContiguousStorageIfAvailable { (utf8) -> JSONValue in
190-
try JSONParser().parse(bytes: utf8)
190+
var parser = JSONParser(bytes: utf8)
191+
return try parser.parse()
191192
}!
192193
}
193194

@@ -198,13 +199,15 @@ open class JSONSerialization : NSObject {
198199

199200
// if utf8 all is good
200201
if encoding == .utf8 {
201-
return try JSONParser().parse(bytes: ptr)
202+
var parser = JSONParser(bytes: ptr)
203+
return try parser.parse()
202204
}
203205

204206
#warning("@Fabian: pretty sure we should throw an error here, if this is invalid data")
205207
let json = String(bytes: ptr, encoding: encoding!)!
206208
return try json.utf8.withContiguousStorageIfAvailable { (utf8) -> JSONValue in
207-
try JSONParser().parse(bytes: utf8)
209+
var parser = JSONParser(bytes: utf8)
210+
return try parser.parse()
208211
}!
209212
}
210213

@@ -306,11 +309,9 @@ open class JSONSerialization : NSObject {
306309

307310
//MARK: - Encoding Detection
308311

309-
internal extension JSONSerialization {
310-
312+
private extension JSONSerialization {
311313
/// Detect the encoding format of the NSData contents
312314
static func detectEncoding(_ bytes: UnsafeRawBufferPointer) -> String.Encoding {
313-
314315
if bytes.count >= 4 {
315316
switch (bytes[0], bytes[1], bytes[2], bytes[3]) {
316317
case (0, 0, 0, _):
@@ -615,27 +616,16 @@ private struct JSONWriter {
615616
}
616617

617618
//MARK: - JSONDeserializer
618-
struct JSONParser {
619-
init() {}
620-
621-
@inlinable
622-
func parse<Bytes: Collection>(bytes: Bytes) throws
623-
-> JSONValue where Bytes.Element == UInt8
624-
{
625-
var impl = JSONParserImpl(bytes: bytes)
626-
return try impl.parse()
627-
}
628-
}
629-
630-
@usableFromInline struct JSONParserImpl {
631-
@usableFromInline var reader: DocumentReader
632-
@usableFromInline var depth: Int = 0
633-
634-
@inlinable init<Bytes: Collection>(bytes: Bytes) where Bytes.Element == UInt8 {
635-
self.reader = DocumentReader(bytes: bytes)
619+
private struct JSONParser {
620+
621+
var reader: DocumentReader
622+
var depth: Int = 0
623+
624+
init<Bytes: Collection>(bytes: Bytes) where Bytes.Element == UInt8 {
625+
self.reader = DocumentReader(bytes: [UInt8](bytes))
636626
}
637627

638-
@usableFromInline mutating func parse() throws -> JSONValue {
628+
mutating func parse() throws -> JSONValue {
639629
let value = try parseValue()
640630
#if DEBUG
641631
defer {
@@ -1093,30 +1083,25 @@ struct JSONParser {
10931083
}
10941084
}
10951085

1096-
extension JSONParserImpl {
1086+
private extension JSONParser {
10971087

1098-
@usableFromInline struct DocumentReader {
1099-
@usableFromInline let array: [UInt8]
1100-
@usableFromInline let count: Int
1088+
struct DocumentReader {
1089+
let array: [UInt8]
1090+
let count: Int
11011091

1102-
@usableFromInline /* private(set) */ var index: Int = -1
1103-
@usableFromInline /* private(set) */ var value: UInt8?
1104-
1105-
@inlinable init<Bytes: Collection>(bytes: Bytes) where Bytes.Element == UInt8 {
1106-
if let array = bytes as? [UInt8] {
1107-
self.array = array
1108-
} else {
1109-
self.array = Array(bytes)
1110-
}
1092+
private(set) var index: Int = -1
1093+
private(set) var value: UInt8?
11111094

1095+
init(bytes: [UInt8]) {
1096+
self.array = bytes
11121097
self.count = self.array.count
11131098
}
11141099

1115-
@inlinable subscript(bounds: Range<Int>) -> ArraySlice<UInt8> {
1100+
subscript(bounds: Range<Int>) -> ArraySlice<UInt8> {
11161101
self.array[bounds]
11171102
}
11181103

1119-
@inlinable mutating func read() -> (UInt8, Int)? {
1104+
mutating func read() -> (UInt8, Int)? {
11201105
guard self.index < self.count - 1 else {
11211106
self.value = nil
11221107
self.index = self.array.endIndex
@@ -1129,17 +1114,17 @@ extension JSONParserImpl {
11291114
return (self.value!, self.index)
11301115
}
11311116

1132-
@inlinable func remainingBytes(from index: Int) -> ArraySlice<UInt8> {
1117+
func remainingBytes(from index: Int) -> ArraySlice<UInt8> {
11331118
self.array.suffix(from: index)
11341119
}
11351120

1136-
@usableFromInline enum EscapedSequenceError: Swift.Error {
1121+
enum EscapedSequenceError: Swift.Error {
11371122
case expectedLowSurrogateUTF8SequenceAfterHighSurrogate(index: Int)
11381123
case unexpectedEscapedCharacter(ascii: UInt8, index: Int)
11391124
case couldNotCreateUnicodeScalarFromUInt32(index: Int, unicodeScalarValue: UInt32)
11401125
}
11411126

1142-
@inlinable mutating func readUTF8StringTillNextUnescapedQuote() throws -> String {
1127+
mutating func readUTF8StringTillNextUnescapedQuote() throws -> String {
11431128
precondition(self.value == UInt8(ascii: "\""), "Expected to have read a quote character last")
11441129
var stringStartIndex = self.index + 1
11451130
var output: String?
@@ -1199,15 +1184,15 @@ extension JSONParserImpl {
11991184
// can be removed as soon https://bugs.swift.org/browse/SR-12126 and
12001185
// https://bugs.swift.org/browse/SR-12125 has landed.
12011186
// Thanks @weissi for making my code fast!
1202-
@inlinable func makeStringFast<Bytes: Collection>(_ bytes: Bytes) -> String where Bytes.Element == UInt8 {
1187+
func makeStringFast<Bytes: Collection>(_ bytes: Bytes) -> String where Bytes.Element == UInt8 {
12031188
if let string = bytes.withContiguousStorageIfAvailable({ String(decoding: $0, as: Unicode.UTF8.self) }) {
12041189
return string
12051190
} else {
12061191
return String(decoding: bytes, as: Unicode.UTF8.self)
12071192
}
12081193
}
12091194

1210-
@inlinable mutating func parseEscapeSequence() throws -> (String, Int) {
1195+
mutating func parseEscapeSequence() throws -> (String, Int) {
12111196
guard let (byte, index) = read() else {
12121197
throw JSONError.unexpectedEndOfFile
12131198
}
@@ -1229,7 +1214,7 @@ extension JSONParserImpl {
12291214
}
12301215
}
12311216

1232-
@inlinable mutating func parseUnicodeSequence() throws -> (Unicode.Scalar, Int) {
1217+
mutating func parseUnicodeSequence() throws -> (Unicode.Scalar, Int) {
12331218
// we build this for utf8 only for now.
12341219
let bitPattern = try parseUnicodeHexSequence()
12351220

@@ -1275,7 +1260,7 @@ extension JSONParserImpl {
12751260
return (unicode, self.index)
12761261
}
12771262

1278-
@inlinable mutating func parseUnicodeHexSequence() throws -> UInt16 {
1263+
mutating func parseUnicodeHexSequence() throws -> UInt16 {
12791264
// As stated in RFC-8259 an escaped unicode character is 4 HEXDIGITs long
12801265
// https://tools.ietf.org/html/rfc8259#section-7
12811266
guard let (firstHex, startIndex) = read(),
@@ -1302,7 +1287,7 @@ extension JSONParserImpl {
13021287
return bitPattern
13031288
}
13041289

1305-
@inlinable static func hexAsciiTo4Bits(_ ascii: UInt8) -> UInt8? {
1290+
static func hexAsciiTo4Bits(_ ascii: UInt8) -> UInt8? {
13061291
switch ascii {
13071292
case 48 ... 57:
13081293
return ascii - 48
@@ -1320,8 +1305,7 @@ extension JSONParserImpl {
13201305
}
13211306

13221307
// MARK:
1323-
@usableFromInline
1324-
enum JSONError: Swift.Error, Equatable {
1308+
private enum JSONError: Swift.Error, Equatable {
13251309
case unexpectedCharacter(ascii: UInt8, characterIndex: Int)
13261310
case unexpectedEndOfFile
13271311
case tooManyNestedArraysOrDictionaries(characterIndex: Int)
@@ -1335,8 +1319,7 @@ enum JSONError: Swift.Error, Equatable {
13351319
case singleFragmentFoundButNotAllowed
13361320
}
13371321

1338-
@usableFromInline
1339-
enum JSONValue {
1322+
private enum JSONValue {
13401323
case string(String)
13411324
case number(String)
13421325
case bool(Bool)
@@ -1346,7 +1329,7 @@ enum JSONValue {
13461329
case object([String: JSONValue])
13471330
}
13481331

1349-
extension JSONValue {
1332+
private extension JSONValue {
13501333
var isValue: Bool {
13511334
switch self {
13521335
case .array, .object:
@@ -1385,64 +1368,7 @@ extension JSONValue {
13851368
}
13861369
}
13871370

1388-
// MARK: - Protocol conformances -
1389-
1390-
// MARK: Equatable
1391-
1392-
extension JSONValue: Equatable {
1393-
@usableFromInline
1394-
static func == (lhs: JSONValue, rhs: JSONValue) -> Bool {
1395-
switch (lhs, rhs) {
1396-
case (.null, .null):
1397-
return true
1398-
case (.bool(let lhs), .bool(let rhs)):
1399-
return lhs == rhs
1400-
case (.number(let lhs), .number(let rhs)):
1401-
return lhs == rhs
1402-
case (.string(let lhs), .string(let rhs)):
1403-
return lhs == rhs
1404-
case (.array(let lhs), .array(let rhs)):
1405-
guard lhs.count == rhs.count else {
1406-
return false
1407-
}
1408-
1409-
var lhsiterator = lhs.makeIterator()
1410-
var rhsiterator = rhs.makeIterator()
1411-
1412-
while let lhs = lhsiterator.next(), let rhs = rhsiterator.next() {
1413-
if lhs == rhs {
1414-
continue
1415-
}
1416-
return false
1417-
}
1418-
1419-
return true
1420-
case (.object(let lhs), .object(let rhs)):
1421-
guard lhs.count == rhs.count else {
1422-
return false
1423-
}
1424-
1425-
var lhsiterator = lhs.makeIterator()
1426-
1427-
while let (lhskey, lhsvalue) = lhsiterator.next() {
1428-
guard let rhsvalue = rhs[lhskey] else {
1429-
return false
1430-
}
1431-
1432-
if lhsvalue == rhsvalue {
1433-
continue
1434-
}
1435-
return false
1436-
}
1437-
1438-
return true
1439-
default:
1440-
return false
1441-
}
1442-
}
1443-
}
1444-
1445-
extension JSONValue {
1371+
private extension JSONValue {
14461372
func toObjcRepresentation(options: JSONSerialization.ReadingOptions) throws -> Any {
14471373
switch self {
14481374
case .array(let values):

0 commit comments

Comments
 (0)