@@ -339,10 +339,10 @@ extension JSONParser {
339
339
self . moveReaderIndex ( forwardBy: copy + 1 )
340
340
guard var result = output else {
341
341
// if we don't have an output string we create a new string
342
- return String ( decoding : self [ stringStartIndex ..< stringStartIndex + copy] , as : Unicode . UTF8 . self )
342
+ return try Self . makeString ( self [ stringStartIndex ..< stringStartIndex + copy] )
343
343
}
344
344
// if we have an output string we append
345
- result += String ( decoding : self [ stringStartIndex ..< stringStartIndex + copy] , as : Unicode . UTF8 . self )
345
+ result += try Self . makeString ( self [ stringStartIndex ..< stringStartIndex + copy] )
346
346
return result
347
347
348
348
case 0 ... 31 :
@@ -352,15 +352,15 @@ extension JSONParser {
352
352
// through U+001F).
353
353
var string = output ?? " "
354
354
let errorIndex = self . readerIndex + copy
355
- string += self . makeStringFast ( self . array [ stringStartIndex ... errorIndex] )
355
+ string += try Self . makeString ( self . array [ stringStartIndex ... errorIndex] )
356
356
throw JSONError . unescapedControlCharacterInString ( ascii: byte, in: string, index: errorIndex)
357
357
358
358
case UInt8 ( ascii: " \\ " ) :
359
359
self . moveReaderIndex ( forwardBy: copy)
360
360
if output != nil {
361
- output! += self . makeStringFast ( self . array [ stringStartIndex ..< stringStartIndex + copy] )
361
+ output! += try Self . makeString ( self . array [ stringStartIndex ..< stringStartIndex + copy] )
362
362
} else {
363
- output = self . makeStringFast ( self . array [ stringStartIndex ..< stringStartIndex + copy] )
363
+ output = try Self . makeString ( self . array [ stringStartIndex ..< stringStartIndex + copy] )
364
364
}
365
365
366
366
let escapedStartIndex = self . readerIndex
@@ -371,13 +371,13 @@ extension JSONParser {
371
371
stringStartIndex = self . readerIndex
372
372
copy = 0
373
373
} catch EscapedSequenceError . unexpectedEscapedCharacter( let ascii, let failureIndex) {
374
- output! += makeStringFast ( array [ escapedStartIndex ..< self . readerIndex] )
374
+ output! += try Self . makeString ( array [ escapedStartIndex ..< self . readerIndex] )
375
375
throw JSONError . unexpectedEscapedCharacter ( ascii: ascii, in: output!, index: failureIndex)
376
376
} catch EscapedSequenceError . expectedLowSurrogateUTF8SequenceAfterHighSurrogate( let failureIndex) {
377
- output! += makeStringFast ( array [ escapedStartIndex ..< self . readerIndex] )
377
+ output! += try Self . makeString ( array [ escapedStartIndex ..< self . readerIndex] )
378
378
throw JSONError . expectedLowSurrogateUTF8SequenceAfterHighSurrogate ( in: output!, index: failureIndex)
379
379
} catch EscapedSequenceError . couldNotCreateUnicodeScalarFromUInt32( let failureIndex, let unicodeScalarValue) {
380
- output! += makeStringFast ( array [ escapedStartIndex ..< self . readerIndex] )
380
+ output! += try Self . makeString ( array [ escapedStartIndex ..< self . readerIndex] )
381
381
throw JSONError . couldNotCreateUnicodeScalarFromUInt32 (
382
382
in: output!, index: failureIndex, unicodeScalarValue: unicodeScalarValue
383
383
)
@@ -392,15 +392,11 @@ extension JSONParser {
392
392
throw JSONError . unexpectedEndOfFile
393
393
}
394
394
395
- // can be removed as soon https://bugs.swift.org/browse/SR-12126 and
396
- // https://bugs.swift.org/browse/SR-12125 has landed.
397
- // Thanks @weissi for making my code fast!
398
- private func makeStringFast< Bytes: Collection > ( _ bytes: Bytes ) -> String where Bytes. Element == UInt8 {
399
- if let string = bytes. withContiguousStorageIfAvailable ( { String ( decoding: $0, as: Unicode . UTF8. self) } ) {
400
- return string
401
- } else {
402
- return String ( decoding: bytes, as: Unicode . UTF8. self)
395
+ private static func makeString< Bytes: Collection > ( _ bytes: Bytes ) throws -> String where Bytes. Element == UInt8 {
396
+ guard let str = String ( bytes: bytes, encoding: . utf8) else {
397
+ throw JSONError . cannotConvertInputDataToUTF8
403
398
}
399
+ return str
404
400
}
405
401
406
402
private mutating func parseEscapeSequence( ) throws -> String {
@@ -606,7 +602,7 @@ extension JSONParser {
606
602
let numberStartIndex = self . readerIndex
607
603
self . moveReaderIndex ( forwardBy: numberchars)
608
604
609
- return self . makeStringFast ( self [ numberStartIndex ..< self . readerIndex] )
605
+ return String ( decoding : self [ numberStartIndex ..< self . readerIndex] , as : Unicode . UTF8 . self )
610
606
default :
611
607
throw JSONError . unexpectedCharacter ( ascii: byte, characterIndex: readerIndex + numberchars)
612
608
}
0 commit comments