@@ -187,7 +187,8 @@ open class JSONSerialization : NSObject {
187
187
let newPtr = ptr [ advanceBy..< ptr. count]
188
188
let json = String ( bytes: newPtr, encoding: encoding) !
189
189
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 ( )
191
192
} !
192
193
}
193
194
@@ -198,13 +199,15 @@ open class JSONSerialization : NSObject {
198
199
199
200
// if utf8 all is good
200
201
if encoding == . utf8 {
201
- return try JSONParser ( ) . parse ( bytes: ptr)
202
+ var parser = JSONParser ( bytes: ptr)
203
+ return try parser. parse ( )
202
204
}
203
205
204
206
#warning("@Fabian: pretty sure we should throw an error here, if this is invalid data")
205
207
let json = String ( bytes: ptr, encoding: encoding!) !
206
208
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 ( )
208
211
} !
209
212
}
210
213
@@ -306,11 +309,9 @@ open class JSONSerialization : NSObject {
306
309
307
310
//MARK: - Encoding Detection
308
311
309
- internal extension JSONSerialization {
310
-
312
+ private extension JSONSerialization {
311
313
/// Detect the encoding format of the NSData contents
312
314
static func detectEncoding( _ bytes: UnsafeRawBufferPointer ) -> String . Encoding {
313
-
314
315
if bytes. count >= 4 {
315
316
switch ( bytes [ 0 ] , bytes [ 1 ] , bytes [ 2 ] , bytes [ 3 ] ) {
316
317
case ( 0 , 0 , 0 , _) :
@@ -615,27 +616,16 @@ private struct JSONWriter {
615
616
}
616
617
617
618
//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) )
636
626
}
637
627
638
- @ usableFromInline mutating func parse( ) throws -> JSONValue {
628
+ mutating func parse( ) throws -> JSONValue {
639
629
let value = try parseValue ( )
640
630
#if DEBUG
641
631
defer {
@@ -1093,30 +1083,25 @@ struct JSONParser {
1093
1083
}
1094
1084
}
1095
1085
1096
- extension JSONParserImpl {
1086
+ private extension JSONParser {
1097
1087
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
1101
1091
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 ?
1111
1094
1095
+ init ( bytes: [ UInt8 ] ) {
1096
+ self . array = bytes
1112
1097
self . count = self . array. count
1113
1098
}
1114
1099
1115
- @ inlinable subscript( bounds: Range < Int > ) -> ArraySlice < UInt8 > {
1100
+ subscript( bounds: Range < Int > ) -> ArraySlice < UInt8 > {
1116
1101
self . array [ bounds]
1117
1102
}
1118
1103
1119
- @ inlinable mutating func read( ) -> ( UInt8 , Int ) ? {
1104
+ mutating func read( ) -> ( UInt8 , Int ) ? {
1120
1105
guard self . index < self . count - 1 else {
1121
1106
self . value = nil
1122
1107
self . index = self . array. endIndex
@@ -1129,17 +1114,17 @@ extension JSONParserImpl {
1129
1114
return ( self . value!, self . index)
1130
1115
}
1131
1116
1132
- @ inlinable func remainingBytes( from index: Int ) -> ArraySlice < UInt8 > {
1117
+ func remainingBytes( from index: Int ) -> ArraySlice < UInt8 > {
1133
1118
self . array. suffix ( from: index)
1134
1119
}
1135
1120
1136
- @ usableFromInline enum EscapedSequenceError : Swift . Error {
1121
+ enum EscapedSequenceError : Swift . Error {
1137
1122
case expectedLowSurrogateUTF8SequenceAfterHighSurrogate( index: Int )
1138
1123
case unexpectedEscapedCharacter( ascii: UInt8 , index: Int )
1139
1124
case couldNotCreateUnicodeScalarFromUInt32( index: Int , unicodeScalarValue: UInt32 )
1140
1125
}
1141
1126
1142
- @ inlinable mutating func readUTF8StringTillNextUnescapedQuote( ) throws -> String {
1127
+ mutating func readUTF8StringTillNextUnescapedQuote( ) throws -> String {
1143
1128
precondition ( self . value == UInt8 ( ascii: " \" " ) , " Expected to have read a quote character last " )
1144
1129
var stringStartIndex = self . index + 1
1145
1130
var output : String ?
@@ -1199,15 +1184,15 @@ extension JSONParserImpl {
1199
1184
// can be removed as soon https://bugs.swift.org/browse/SR-12126 and
1200
1185
// https://bugs.swift.org/browse/SR-12125 has landed.
1201
1186
// 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 {
1203
1188
if let string = bytes. withContiguousStorageIfAvailable ( { String ( decoding: $0, as: Unicode . UTF8. self) } ) {
1204
1189
return string
1205
1190
} else {
1206
1191
return String ( decoding: bytes, as: Unicode . UTF8. self)
1207
1192
}
1208
1193
}
1209
1194
1210
- @ inlinable mutating func parseEscapeSequence( ) throws -> ( String , Int ) {
1195
+ mutating func parseEscapeSequence( ) throws -> ( String , Int ) {
1211
1196
guard let ( byte, index) = read ( ) else {
1212
1197
throw JSONError . unexpectedEndOfFile
1213
1198
}
@@ -1229,7 +1214,7 @@ extension JSONParserImpl {
1229
1214
}
1230
1215
}
1231
1216
1232
- @ inlinable mutating func parseUnicodeSequence( ) throws -> ( Unicode . Scalar , Int ) {
1217
+ mutating func parseUnicodeSequence( ) throws -> ( Unicode . Scalar , Int ) {
1233
1218
// we build this for utf8 only for now.
1234
1219
let bitPattern = try parseUnicodeHexSequence ( )
1235
1220
@@ -1275,7 +1260,7 @@ extension JSONParserImpl {
1275
1260
return ( unicode, self . index)
1276
1261
}
1277
1262
1278
- @ inlinable mutating func parseUnicodeHexSequence( ) throws -> UInt16 {
1263
+ mutating func parseUnicodeHexSequence( ) throws -> UInt16 {
1279
1264
// As stated in RFC-8259 an escaped unicode character is 4 HEXDIGITs long
1280
1265
// https://tools.ietf.org/html/rfc8259#section-7
1281
1266
guard let ( firstHex, startIndex) = read ( ) ,
@@ -1302,7 +1287,7 @@ extension JSONParserImpl {
1302
1287
return bitPattern
1303
1288
}
1304
1289
1305
- @ inlinable static func hexAsciiTo4Bits( _ ascii: UInt8 ) -> UInt8 ? {
1290
+ static func hexAsciiTo4Bits( _ ascii: UInt8 ) -> UInt8 ? {
1306
1291
switch ascii {
1307
1292
case 48 ... 57 :
1308
1293
return ascii - 48
@@ -1320,8 +1305,7 @@ extension JSONParserImpl {
1320
1305
}
1321
1306
1322
1307
// MARK:
1323
- @usableFromInline
1324
- enum JSONError : Swift . Error , Equatable {
1308
+ private enum JSONError : Swift . Error , Equatable {
1325
1309
case unexpectedCharacter( ascii: UInt8 , characterIndex: Int )
1326
1310
case unexpectedEndOfFile
1327
1311
case tooManyNestedArraysOrDictionaries( characterIndex: Int )
@@ -1335,8 +1319,7 @@ enum JSONError: Swift.Error, Equatable {
1335
1319
case singleFragmentFoundButNotAllowed
1336
1320
}
1337
1321
1338
- @usableFromInline
1339
- enum JSONValue {
1322
+ private enum JSONValue {
1340
1323
case string( String )
1341
1324
case number( String )
1342
1325
case bool( Bool )
@@ -1346,7 +1329,7 @@ enum JSONValue {
1346
1329
case object( [ String : JSONValue ] )
1347
1330
}
1348
1331
1349
- extension JSONValue {
1332
+ private extension JSONValue {
1350
1333
var isValue : Bool {
1351
1334
switch self {
1352
1335
case . array, . object:
@@ -1385,64 +1368,7 @@ extension JSONValue {
1385
1368
}
1386
1369
}
1387
1370
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 {
1446
1372
func toObjcRepresentation( options: JSONSerialization . ReadingOptions ) throws -> Any {
1447
1373
switch self {
1448
1374
case . array( let values) :
0 commit comments