@@ -16,58 +16,6 @@ extension _Normalization {
16
16
internal typealias _SegmentOutputBuffer = _FixedArray16 < UInt16 >
17
17
}
18
18
19
- extension Unicode . Scalar {
20
- // Normalization boundary - a place in a string where everything left of the
21
- // boundary can be normalized independently from everything right of the
22
- // boundary. The concatenation of each result is the same as if the entire
23
- // string had been normalized as a whole.
24
- //
25
- // Normalization segment - a sequence of code units between two normalization
26
- // boundaries (without any boundaries in the middle). Note that normalization
27
- // segments can, as a process of normalization, expand, contract, and even
28
- // produce new sub-segments.
29
-
30
- // Whether this scalar value always has a normalization boundary before it.
31
- internal var _hasNormalizationBoundaryBefore : Bool {
32
- _internalInvariant ( Int32 ( exactly: self . value) != nil , " top bit shouldn't be set " )
33
- let value = Int32 ( bitPattern: self . value)
34
- return 0 != __swift_stdlib_unorm2_hasBoundaryBefore (
35
- _Normalization. _nfcNormalizer, value)
36
- }
37
- internal var _isNFCQCYes : Bool {
38
- return __swift_stdlib_u_getIntPropertyValue (
39
- Builtin . reinterpretCast ( value) , __swift_stdlib_UCHAR_NFC_QUICK_CHECK
40
- ) == 1
41
- }
42
- }
43
-
44
- internal func _tryNormalize(
45
- _ input: UnsafeBufferPointer < UInt16 > ,
46
- into outputBuffer:
47
- UnsafeMutablePointer < _Normalization . _SegmentOutputBuffer >
48
- ) -> Int ? {
49
- return _tryNormalize ( input, into: _castOutputBuffer ( outputBuffer) )
50
- }
51
- internal func _tryNormalize(
52
- _ input: UnsafeBufferPointer < UInt16 > ,
53
- into outputBuffer: UnsafeMutableBufferPointer < UInt16 >
54
- ) -> Int ? {
55
- var err = __swift_stdlib_U_ZERO_ERROR
56
- let count = __swift_stdlib_unorm2_normalize (
57
- _Normalization. _nfcNormalizer,
58
- input. baseAddress. _unsafelyUnwrappedUnchecked,
59
- numericCast ( input. count) ,
60
- outputBuffer. baseAddress. _unsafelyUnwrappedUnchecked,
61
- numericCast ( outputBuffer. count) ,
62
- & err
63
- )
64
- guard err. isSuccess else {
65
- // The output buffer needs to grow
66
- return nil
67
- }
68
- return numericCast ( count)
69
- }
70
-
71
19
//
72
20
// Pointer casting helpers
73
21
//
@@ -131,9 +79,11 @@ extension UnsafeBufferPointer where Element == UInt8 {
131
79
if index == 0 || index == count {
132
80
return true
133
81
}
134
-
135
82
assert ( !_isContinuation( self [ _unchecked: index] ) )
136
83
84
+ // Sub-300 latiny fast-path
85
+ if self [ _unchecked: index] < 0xCC { return true }
86
+
137
87
let cu = _decodeScalar ( self , startingAt: index) . 0
138
88
return cu. _hasNormalizationBoundaryBefore
139
89
}
@@ -191,34 +141,6 @@ internal struct _NormalizedUTF8CodeUnitIterator: IteratorProtocol {
191
141
192
142
return utf8Buffer [ bufferIndex]
193
143
}
194
-
195
- internal mutating func compare(
196
- with other: _NormalizedUTF8CodeUnitIterator
197
- ) -> _StringComparisonResult {
198
- var mutableOther = other
199
-
200
- for cu in self {
201
- if let otherCU = mutableOther. next ( ) {
202
- let result = _lexicographicalCompare ( cu, otherCU)
203
- if result == . equal {
204
- continue
205
- } else {
206
- return result
207
- }
208
- } else {
209
- //other returned nil, we are greater
210
- return . greater
211
- }
212
- }
213
-
214
- //we ran out of code units, either we are equal, or only we ran out and
215
- //other is greater
216
- if let _ = mutableOther. next ( ) {
217
- return . less
218
- } else {
219
- return . equal
220
- }
221
- }
222
144
}
223
145
224
146
extension _NormalizedUTF8CodeUnitIterator : Sequence { }
@@ -513,16 +435,6 @@ extension _NormalizedUTF8CodeUnitIterator_2 {
513
435
@inline ( __always)
514
436
@_effects ( releasenone)
515
437
private mutating func fastPathFill( ) -> ( numRead: Int , numWritten: Int ) {
516
- // Quick check if a scalar is NFC and a segment starter
517
- @inline ( __always) func isNFCStarter( _ scalar: Unicode . Scalar ) -> Bool {
518
- // Fast-path: All scalars up through U+02FF are NFC and have boundaries
519
- // before them
520
- if scalar. value < 0x300 { return true }
521
-
522
- // Otherwise, consult the properties
523
- return scalar. _hasNormalizationBoundaryBefore && scalar. _isNFCQCYes
524
- }
525
-
526
438
// TODO: Additional fast-path: All CCC-ascending NFC_QC segments are NFC
527
439
// TODO: Just freakin do normalization and don't bother with ICU
528
440
var outputCount = 0
@@ -540,7 +452,7 @@ extension _NormalizedUTF8CodeUnitIterator_2 {
540
452
541
453
if _slowPath (
542
454
!utf8. hasNormalizationBoundary ( before: inputCount &+ len)
543
- || !isNFCStarter ( scalar)
455
+ || !scalar. _isNFCStarter
544
456
) {
545
457
break
546
458
}
@@ -566,7 +478,7 @@ extension _NormalizedUTF8CodeUnitIterator_2 {
566
478
if _slowPath (
567
479
!gutsSlice. foreignHasNormalizationBoundary (
568
480
before: startIdx. encoded ( offsetBy: len) )
569
- || !isNFCStarter ( scalar)
481
+ || !scalar. _isNFCStarter
570
482
) {
571
483
break
572
484
}
@@ -627,29 +539,22 @@ extension _NormalizedUTF8CodeUnitIterator_2 {
627
539
628
540
@_effects ( readonly)
629
541
internal mutating func compare(
630
- with other: _NormalizedUTF8CodeUnitIterator_2
631
- ) -> _StringComparisonResult {
632
- var iter = self
542
+ with other: _NormalizedUTF8CodeUnitIterator_2 ,
543
+ expecting : _StringComparisonResult
544
+ ) -> Bool {
633
545
var mutableOther = other
634
546
635
- while let cu = iter. next ( ) {
636
- if let otherCU = mutableOther. next ( ) {
637
- let result = _lexicographicalCompare ( cu, otherCU)
638
- if result == . equal {
639
- continue
640
- }
641
- return result
547
+ for cu in self {
548
+ guard let otherCU = mutableOther. next ( ) else {
549
+ // We have more code units, therefore we are greater
550
+ return false
642
551
}
643
- //other returned nil, we are greater
644
- return . greater
552
+ if cu == otherCU { continue }
553
+ return expecting == . less ? cu < otherCU : false
645
554
}
646
555
647
- //we ran out of code units, either we are equal, or only we ran out and
648
- //other is greater
649
- if let _ = mutableOther. next ( ) {
650
- return . less
651
- }
652
- return . equal
556
+ // We have exhausted our code units. We are less if there's more remaining
557
+ return mutableOther. next ( ) == nil ? expecting == . equal : expecting == . less
653
558
}
654
559
}
655
560
0 commit comments