@@ -243,17 +243,20 @@ extension Unicode {
243
243
244
244
public protocol UnicodeDecoder {
245
245
associatedtype CodeUnit : UnsignedInteger , FixedWidthInteger
246
- associatedtype Buffer : Collection where Buffer. Iterator. Element == CodeUnit
246
+ associatedtype Buffer : Collection
247
+ where Buffer. Iterator. Element == CodeUnit
248
+ associatedtype EncodedScalar : Collection
249
+ where EncodedScalar. Iterator. Element == CodeUnit
247
250
248
251
init ( )
249
252
250
253
var buffer : Buffer { get }
251
254
252
255
mutating func parseOne< I : IteratorProtocol > (
253
256
_ input: inout I
254
- ) -> Unicode . ParseResult < Buffer > where I. Element == CodeUnit
257
+ ) -> Unicode . ParseResult < EncodedScalar > where I. Element == CodeUnit
255
258
256
- static func decodeOne( _ content: Buffer ) -> UnicodeScalar
259
+ static func decodeOne( _ content: EncodedScalar ) -> UnicodeScalar
257
260
}
258
261
259
262
extension UnicodeDecoder {
@@ -294,22 +297,23 @@ public protocol UnicodeEncoding {
294
297
}
295
298
296
299
297
- public protocol _UTF8Decoder : UnicodeDecoder {
300
+ public protocol _UTF8Decoder : UnicodeDecoder where Buffer == EncodedScalar {
298
301
func _parseNonASCII( ) -> ( isValid: Bool , bitCount: UInt8 )
299
302
var buffer : Buffer { get set }
300
303
}
301
304
302
305
extension _UTF8Decoder where Buffer == _UIntBuffer < UInt32 , UInt8 > {
303
306
public mutating func parseOne< I : IteratorProtocol > (
304
307
_ input: inout I
305
- ) -> Unicode . ParseResult < Buffer > where I. Element == Unicode . UTF8 . CodeUnit {
308
+ ) -> Unicode . ParseResult < EncodedScalar >
309
+ where I. Element == Unicode . UTF8 . CodeUnit {
306
310
307
311
// Bufferless ASCII fastpath.
308
312
if _fastPath ( buffer. isEmpty) {
309
313
guard let codeUnit = input. next ( ) else { return . emptyInput }
310
314
// ASCII, return immediately.
311
315
if codeUnit & 0x80 == 0 {
312
- return . valid( Buffer ( containing: codeUnit) )
316
+ return . valid( EncodedScalar ( containing: codeUnit) )
313
317
}
314
318
// Non-ASCII, proceed to buffering mode.
315
319
buffer. append ( codeUnit)
@@ -318,7 +322,7 @@ extension _UTF8Decoder where Buffer == _UIntBuffer<UInt32, UInt8> {
318
322
// to bufferless mode once we've exhausted it.
319
323
let codeUnit = UInt8 ( extendingOrTruncating: buffer. _storage)
320
324
buffer. remove ( at: buffer. startIndex)
321
- return . valid( Buffer ( containing: codeUnit) )
325
+ return . valid( EncodedScalar ( containing: codeUnit) )
322
326
}
323
327
// Buffering mode.
324
328
// Fill buffer back to 4 bytes (or as many as are left in the iterator).
@@ -355,11 +359,14 @@ extension _UTF8Decoder where Buffer == _UIntBuffer<UInt32, UInt8> {
355
359
extension Unicode . UTF8 : UnicodeEncoding {
356
360
public struct ForwardDecoder {
357
361
public typealias Buffer = _UIntBuffer < UInt32 , UInt8 >
362
+ public typealias EncodedScalar = Buffer
358
363
public init ( ) { buffer = Buffer ( ) }
359
364
public var buffer : Buffer
360
365
}
366
+
361
367
public struct ReverseDecoder {
362
368
public typealias Buffer = _UIntBuffer < UInt32 , UInt8 >
369
+ public typealias EncodedScalar = Buffer
363
370
public init ( ) { buffer = Buffer ( ) }
364
371
public var buffer : Buffer
365
372
}
@@ -368,9 +375,9 @@ extension Unicode.UTF8 : UnicodeEncoding {
368
375
extension UTF8 . ReverseDecoder : _UTF8Decoder {
369
376
public typealias CodeUnit = UInt8
370
377
371
- public static func decodeOne( _ encodedScalar : Buffer ) -> UnicodeScalar {
372
- let bits = encodedScalar . _storage
373
- switch encodedScalar . _bitCount {
378
+ public static func decodeOne( _ source : EncodedScalar ) -> UnicodeScalar {
379
+ let bits = source . _storage
380
+ switch source . _bitCount {
374
381
case 8 : return UnicodeScalar ( _unchecked: bits)
375
382
case 16 :
376
383
var value = bits & 0b0______________________11_1111
@@ -382,7 +389,7 @@ extension UTF8.ReverseDecoder : _UTF8Decoder {
382
389
value |= bits &>> 4 & 0b0_________1111_0000__0000_0000
383
390
return UnicodeScalar ( _unchecked: value)
384
391
default :
385
- _sanityCheck ( encodedScalar . _bitCount == 32 )
392
+ _sanityCheck ( source . _bitCount == 32 )
386
393
var value = bits & 0b0______________________11_1111
387
394
value |= bits &>> 2 & 0b0______________1111__1100_0000
388
395
value |= bits &>> 4 & 0b0_____11__1111_0000__0000_0000
@@ -515,9 +522,9 @@ extension Unicode.UTF8.ForwardDecoder : _UTF8Decoder {
515
522
return 1
516
523
}
517
524
518
- public static func decodeOne( _ encodedScalar : Buffer ) -> UnicodeScalar {
519
- let bits = encodedScalar . _storage
520
- switch encodedScalar . _bitCount {
525
+ public static func decodeOne( _ source : EncodedScalar ) -> UnicodeScalar {
526
+ let bits = source . _storage
527
+ switch source . _bitCount {
521
528
case 8 :
522
529
return UnicodeScalar ( _unchecked: bits)
523
530
case 16 :
@@ -530,7 +537,7 @@ extension Unicode.UTF8.ForwardDecoder : _UTF8Decoder {
530
537
value |= ( bits & 0b0________________________________0000_1111 ) &<< 12
531
538
return UnicodeScalar ( _unchecked: value)
532
539
default :
533
- _sanityCheck ( encodedScalar . count == 4 )
540
+ _sanityCheck ( source . count == 4 )
534
541
var value = ( bits & 0b0_11_1111__0000_0000__0000_0000__0000_0000 ) &>> 24
535
542
value |= ( bits & 0b0____________11_1111__0000_0000__0000_0000 ) &>> 10
536
543
value |= ( bits & 0b0_______________________11_1111__0000_0000 ) &<< 4
0 commit comments