_BSubstring: Conform to Comparable

This commit is contained in:
Karoy Lorentey committed 2023-03-20 13:12:12 -07:00
1 parent 1182398068
commit abf5dfd260
3 files changed
+34 -11

No files matched your search

@@ -14,22 +14,15 @@
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString: Comparable {
internal static func < (left: Self, right: Self) -> Bool {
left.characterwiseIsLess(than: right)
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString {
internal func characterwiseIsLess(than other: Self) -> Bool {
// FIXME: Implement properly normalized comparisons & hashing.
// This is somewhat tricky as we shouldn't just normalize individual pieces of the string
// split up on random Character boundaries -- Unicode does not promise that
// norm(a + c) == norm(a) + norm(b) in this case.
// To do this properly, we'll probably need to expose new stdlib entry points. :-/
if self.isIdentical(to: other) { return false }
if left.isIdentical(to: right) { return false }
// FIXME: Even if we keep doing characterwise comparisons, we should skip over shared subtrees.
var it1 = self.makeCharacterIterator()
var it2 = other.makeCharacterIterator()
var it1 = left.makeIterator()
var it2 = right.makeIterator()
while true {
switch (it1.next(), it2.next()) {
case (nil, nil): return false
@@ -70,6 +70,11 @@ extension _BSubstring: Equatable {
internal static func ==(left: Self, right: Self) -> Bool {
_BString.characterwiseIsEqual(left._base, in: left._bounds, to: right._base, in: right._bounds)
}
internal func isIdentical(to other: Self) -> Bool {
guard self._base.isIdentical(to: other._base) else { return false }
return self._bounds == other._bounds
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
@@ -79,6 +84,31 @@ extension _BSubstring: Hashable {
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring: Comparable {
internal static func < (left: Self, right: Self) -> Bool {
// FIXME: Implement properly normalized comparisons & hashing.
// This is somewhat tricky as we shouldn't just normalize individual pieces of the string
// split up on random Character boundaries -- Unicode does not promise that
// norm(a + c) == norm(a) + norm(b) in this case.
// To do this properly, we'll probably need to expose new stdlib entry points. :-/
if left.isIdentical(to: right) { return false }
// FIXME: Even if we keep doing characterwise comparisons, we should skip over shared subtrees.
var it1 = left.makeIterator()
var it2 = right.makeIterator()
while true {
switch (it1.next(), it2.next()) {
case (nil, nil): return false
case (nil, .some): return true
case (.some, nil): return false
case let (a?, b?):
if a == b { continue }
return a < b
}
}
}
}
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BSubstring: Sequence {
typealias Element = Character