Skip to content

Commit f9aa787

Browse files
author
Lance Parker
committed
Moar feedback
1 parent 12bf297 commit f9aa787

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

stdlib/public/core/NormalizedCodeUnitIterator.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -361,20 +361,15 @@ struct _NormalizedUTF16CodeUnitIterator: IteratorProtocol {
361361
segmentBufferCount = normalizeFromSource()
362362
}
363363

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+
367366

368367
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]
377371
}
372+
return segmentBuffer[segmentBufferIndex]
378373
}
379374

380375
mutating func normalizeFromSource() -> Int {
@@ -387,32 +382,40 @@ struct _NormalizedUTF16CodeUnitIterator: IteratorProtocol {
387382
into: &segmentBuffer
388383
) {
389384
return count
390-
} else {
391-
return normalizeWithHeapBuffers(filled)
392385
}
393-
} else {
394-
return normalizeWithHeapBuffers()
386+
return normalizeWithHeapBuffers(filled)
395387
}
388+
return normalizeWithHeapBuffers()
396389
}
397390

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.
398401
mutating func normalizeWithHeapBuffers(
399-
_ copyFromNormalizationBuffer: Int? = nil
402+
_ stackBufferCount: Int? = nil
400403
) -> Int {
401404
if segmentHeapBuffer == nil {
402405
_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)
405408
* _Normalization._maxNFCExpansionFactor
406409
segmentHeapBuffer = Array(repeating: 0, count: size)
407410
normalizationHeapBuffer = Array(repeating:0, count: size)
408-
for i in 0..<extraInBuffer {
411+
for i in 0..<preFilledBufferCount {
409412
normalizationHeapBuffer![i] = normalizationBuffer[i]
410413
}
411414
}
412415

413416
guard let count = normalizationHeapBuffer!.withUnsafeMutableBufferPointer({
414417
(normalizationHeapBufferPtr) -> Int? in
415-
guard let filled = copyFromNormalizationBuffer ??
418+
guard let filled = stackBufferCount ??
416419
source.tryFill(into: normalizationHeapBufferPtr)
417420
else {
418421
fatalError("Invariant broken, buffer should have space")
@@ -636,22 +639,19 @@ extension _NormalizedUTF8CodeUnitIterator_2 {
636639
let result = _lexicographicalCompare(cu, otherCU)
637640
if result == .equal {
638641
continue
639-
} else {
640-
return result
641642
}
642-
} else {
643-
//other returned nil, we are greater
644-
return .greater
643+
return result
645644
}
645+
//other returned nil, we are greater
646+
return .greater
646647
}
647648

648649
//we ran out of code units, either we are equal, or only we ran out and
649650
//other is greater
650651
if let _ = mutableOther.next() {
651652
return .less
652-
} else {
653-
return .equal
654653
}
654+
return .equal
655655
}
656656
}
657657

0 commit comments

Comments
 (0)