From abf5dfd260209dc550a826d745a2a03bde6ddcab Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 10 Mar 2023 18:55:27 -0800 Subject: [PATCH] _BSubstring: Conform to Comparable --- .../Conformances/_BString+Comparable.swift | 13 ++------ .../BString/Views/_BSubstring.swift | 30 +++++++++++++++++++ .../BigString/BigString+CharacterView.swift | 2 +- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Sources/RopeModule/BString/Conformances/_BString+Comparable.swift b/Sources/RopeModule/BString/Conformances/_BString+Comparable.swift index cb2a67a5..565e43ce 100644 --- a/Sources/RopeModule/BString/Conformances/_BString+Comparable.swift +++ b/Sources/RopeModule/BString/Conformances/_BString+Comparable.swift @@ -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 diff --git a/Sources/RopeModule/BString/Views/_BSubstring.swift b/Sources/RopeModule/BString/Views/_BSubstring.swift index 3c9e2402..b8ae1c7a 100644 --- a/Sources/RopeModule/BString/Views/_BSubstring.swift +++ b/Sources/RopeModule/BString/Views/_BSubstring.swift @@ -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 diff --git a/Sources/RopeModule/BigString/BigString+CharacterView.swift b/Sources/RopeModule/BigString/BigString+CharacterView.swift index d51acb8c..b6ec0eb8 100644 --- a/Sources/RopeModule/BigString/BigString+CharacterView.swift +++ b/Sources/RopeModule/BigString/BigString+CharacterView.swift @@ -60,7 +60,7 @@ extension BigString: Equatable { @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension BigString: Comparable { public static func < (left: Self, right: Self) -> Bool { - left._guts.characterwiseIsLess(than: right._guts) + left._guts < right._guts } }