Skip to content

Commit 1a9ba3b

Browse files
author
Lance Parker
committed
Honor ranges in the opaque path
1 parent f757334 commit 1a9ba3b

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

stdlib/public/core/NormalizedCodeUnitIterator.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ struct _NormalizedCodeUnitIterator: IteratorProtocol {
3131
source = _UnmanagedStringSource(unmanagedString, start: startIndex)
3232
}
3333

34-
init(_ guts: _StringGuts, startIndex: Int = 0) {
35-
source = _StringGutsSource(guts, start: startIndex)
34+
init(_ guts: _StringGuts, _ range: Range<Int>, startIndex: Int = 0) {
35+
source = _StringGutsSource(guts, range, start: startIndex)
3636
}
3737

3838
mutating func compare(with other: _NormalizedCodeUnitIterator) -> _Ordering {
@@ -137,21 +137,23 @@ struct _NormalizedCodeUnitIterator: IteratorProtocol {
137137

138138
struct _StringGutsSource: _SegmentSource {
139139
var remaining: Int {
140-
return guts.count - index
140+
return range.count - index
141141
}
142142
var guts: _StringGuts
143143
var index: Int
144+
var range: Range<Int>
144145

145-
init(_ guts: _StringGuts, start: Int = 0) {
146+
init(_ guts: _StringGuts, _ range: Range<Int>, start: Int = 0) {
146147
self.guts = guts
147-
index = start
148+
self.range = range
149+
index = range.lowerBound + start
148150
}
149151

150152
mutating func tryFill(buffer: UnsafeMutableBufferPointer<UInt16>) -> Int? {
151153
var bufferIndex = 0
152154
let originalIndex = index
153155
repeat {
154-
guard index < guts.count else {
156+
guard index < range.count else {
155157
break
156158
}
157159

stdlib/public/core/StringComparison.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ extension _UnmanagedString where CodeUnit : FixedWidthInteger & UnsignedInteger
209209
}
210210

211211
extension _UnmanagedOpaqueString {
212-
internal func _findDiffIdx(_ other: _StringGuts
212+
internal func _findDiffIdx(_ other: _StringGuts, _ otherRange: Range<Int>
213213
) -> Int {
214-
let count = Swift.min(self.count, other.count)
214+
let count = Swift.min(self.count, otherRange.count)
215215
for idx in 0..<count {
216-
guard self[idx] == other[idx] else {
216+
guard self[idx] == other[idx + otherRange.lowerBound] else {
217217
return idx
218218
}
219219
}
@@ -552,13 +552,13 @@ extension _UnmanagedOpaqueString {
552552
let selfCount = selfRange.count
553553
let otherCount = otherRange.count
554554
let count = Swift.min(selfCount, otherCount)
555-
let idx = self._findDiffIdx(other)
555+
let idx = self[selfRange]._findDiffIdx(other, otherRange)
556556
if idx == count {
557557
return _lexicographicalCompare(selfCount, otherCount)
558558
}
559559

560560
let selfCU = self[idx]
561-
let otherCU = other[idx]
561+
let otherCU = other[idx + otherRange.lowerBound]
562562

563563
//
564564
// Fast path: if one is ASCII, we can often compare the code units directly.
@@ -587,7 +587,7 @@ extension _UnmanagedOpaqueString {
587587
}
588588

589589
return self._compareOpaquePathological(
590-
other, startingFrom: Swift.max(0, idx-1))
590+
other, otherRange, startingFrom: Swift.max(0, idx-1))
591591
}
592592

593593
if selfIsASCII && selfIsSingleSegmentScalar
@@ -600,19 +600,20 @@ extension _UnmanagedOpaqueString {
600600
}
601601

602602
return self._compareOpaquePathological(
603-
other, startingFrom: Swift.max(0, idx-1)
603+
other, otherRange, startingFrom: Swift.max(0, idx-1)
604604
)
605605
}
606606

607607
@inline(never)
608608
func _compareOpaquePathological(
609-
_ other: _StringGuts, startingFrom: Int
609+
_ other: _StringGuts, _ otherRange: Range<Int>,
610+
startingFrom: Int
610611
) -> _Ordering {
611612
// Compare by pulling in a segment at a time, normalizing then comparing
612613
// individual code units
613614
var selfIterator = _NormalizedCodeUnitIterator(self, startIndex: startingFrom)
614615
return selfIterator.compare(with:
615-
_NormalizedCodeUnitIterator(other, startIndex: startingFrom)
616+
_NormalizedCodeUnitIterator(other, otherRange, startIndex: startingFrom)
616617
)
617618
}
618619
}

0 commit comments

Comments
 (0)