@@ -361,20 +361,15 @@ struct _NormalizedUTF16CodeUnitIterator: IteratorProtocol {
361
361
segmentBufferCount = normalizeFromSource ( )
362
362
}
363
363
364
- //exactly one of the buffers should have code units for us to return
365
- _internalInvariant ( ( segmentBufferCount == 0 )
366
- != ( ( overflowBuffer? . count ?? 0 ) == 0 ) )
364
+ guard segmentBufferIndex < segmentBufferCount else { return nil }
365
+
367
366
368
367
if segmentBufferIndex < segmentBufferCount {
369
- defer { segmentBufferIndex += 1 }
370
- if _slowPath ( segmentHeapBuffer != nil ) {
371
- return segmentHeapBuffer![ segmentBufferIndex]
372
- } else {
373
- return segmentBuffer [ segmentBufferIndex]
374
- }
375
- } else {
376
- return nil
368
+ defer { segmentBufferIndex += 1 }
369
+ if _slowPath ( segmentHeapBuffer != nil ) {
370
+ return segmentHeapBuffer![ segmentBufferIndex]
377
371
}
372
+ return segmentBuffer [ segmentBufferIndex]
378
373
}
379
374
380
375
mutating func normalizeFromSource( ) -> Int {
@@ -387,32 +382,40 @@ struct _NormalizedUTF16CodeUnitIterator: IteratorProtocol {
387
382
into: & segmentBuffer
388
383
) {
389
384
return count
390
- } else {
391
- return normalizeWithHeapBuffers ( filled)
392
385
}
393
- } else {
394
- return normalizeWithHeapBuffers ( )
386
+ return normalizeWithHeapBuffers ( filled)
395
387
}
388
+ return normalizeWithHeapBuffers ( )
396
389
}
397
390
391
+ //This handles normalization from an intermediate buffer to the heap segment
392
+ //buffer. This can get called in 3 situations:
393
+ //* We've already transitioned to heap buffers
394
+ //* We attempted to fill the pre-normal stack buffer but there was not enough
395
+ //. room, so we need to promote both and then attempt the fill again.
396
+ //* The fill for the stack buffer succeeded, but the normalization didn't. In
397
+ // this case, we want to first copy the contents of the stack buffer that
398
+ // we filled into the new heap buffer. The stackBufferCount
399
+ // parameter signals that we need to do this copy, thus skipping the fill
400
+ // that we would normally do before normalization.
398
401
mutating func normalizeWithHeapBuffers(
399
- _ copyFromNormalizationBuffer : Int ? = nil
402
+ _ stackBufferCount : Int ? = nil
400
403
) -> Int {
401
404
if segmentHeapBuffer == nil {
402
405
_sanityCheck ( normalizationHeapBuffer == nil )
403
- let extraInBuffer = copyFromNormalizationBuffer ?? 0
404
- let size = ( source. remaining + extraInBuffer )
406
+ let preFilledBufferCount = stackBufferCount ?? 0
407
+ let size = ( source. remaining + preFilledBufferCount )
405
408
* _Normalization. _maxNFCExpansionFactor
406
409
segmentHeapBuffer = Array ( repeating: 0 , count: size)
407
410
normalizationHeapBuffer = Array ( repeating: 0 , count: size)
408
- for i in 0 ..< extraInBuffer {
411
+ for i in 0 ..< preFilledBufferCount {
409
412
normalizationHeapBuffer![ i] = normalizationBuffer [ i]
410
413
}
411
414
}
412
415
413
416
guard let count = normalizationHeapBuffer!. withUnsafeMutableBufferPointer ( {
414
417
( normalizationHeapBufferPtr) -> Int ? in
415
- guard let filled = copyFromNormalizationBuffer ??
418
+ guard let filled = stackBufferCount ??
416
419
source. tryFill ( into: normalizationHeapBufferPtr)
417
420
else {
418
421
fatalError ( " Invariant broken, buffer should have space " )
@@ -636,22 +639,19 @@ extension _NormalizedUTF8CodeUnitIterator_2 {
636
639
let result = _lexicographicalCompare ( cu, otherCU)
637
640
if result == . equal {
638
641
continue
639
- } else {
640
- return result
641
642
}
642
- } else {
643
- //other returned nil, we are greater
644
- return . greater
643
+ return result
645
644
}
645
+ //other returned nil, we are greater
646
+ return . greater
646
647
}
647
648
648
649
//we ran out of code units, either we are equal, or only we ran out and
649
650
//other is greater
650
651
if let _ = mutableOther. next ( ) {
651
652
return . less
652
- } else {
653
- return . equal
654
653
}
654
+ return . equal
655
655
}
656
656
}
657
657
0 commit comments