diff --git a/Sources/RopeModule/BString/Basics/_BString+Contents.swift b/Sources/RopeModule/BString/Basics/_BString+Contents.swift index 71837692..9bc335ec 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Contents.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Contents.swift @@ -175,7 +175,8 @@ extension _BString { offsetBy distance: Int, in metric: some _StringMetric ) -> Index { - var (path, chunk) = path(to: i, preferEnd: false) + precondition(i <= endIndex, "Index out of bounds") + var (path, chunk) = path(to: i, preferEnd: i == endIndex || distance < 0) let base = baseIndex(with: i, at: path.chunk) var d = distance let r = metric.formIndex(&path.chunk, offsetBy: &d, in: chunk) @@ -268,7 +269,7 @@ extension _BString { @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BString { func characterIndex(roundingDown i: Index) -> Index { - let offset = i._utf8Offset - 1 + let offset = i._utf8Offset precondition(offset >= 0 && offset <= utf8Count, "Index out of bounds") guard offset > 0, offset < utf8Count else { return Index(_utf8Offset: offset) } diff --git a/Sources/RopeModule/BString/Basics/_BString+Iterators.swift b/Sources/RopeModule/BString/Basics/_BString+Iterators.swift index ab0863e1..e914fbef 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Iterators.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Iterators.swift @@ -146,14 +146,15 @@ extension _BString.UnicodeScalarIterator: IteratorProtocol { @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BString { internal struct CharacterIterator { - internal var _utf8BaseOffset: Int internal var _base: _Rope.Iterator + + internal var _utf8BaseOffset: Int internal var _index: String.Index internal var _next: String.Index - internal init(_base: _Rope.Iterator, utf8BaseOffset: Int) { - self._utf8BaseOffset = utf8BaseOffset - self._base = _base + internal init(_ string: _BString) { + self._utf8BaseOffset = 0 + self._base = string.rope.makeIterator() guard !_base.isAtEnd else { _index = "".startIndex _next = "".endIndex @@ -165,35 +166,32 @@ extension _BString { self._next = chunk.string[_index...].index(after: _index) } - internal init(_base: _Rope.Iterator, utf8BaseOffset: Int, _ i: String.Index) { - self._utf8BaseOffset = utf8BaseOffset - self._base = _base - self._index = i - assert(i >= _base.current.firstBreak) - guard !_base.isAtEnd, i < _base.current.string.endIndex else { - self._next = i + internal init( + _ string: _BString, + from start: Index + ) { + self._utf8BaseOffset = start._utf8Offset + + if start == string.endIndex { + self._base = string.rope.makeIterator(from: string.rope.endIndex) + self._index = "".startIndex + self._next = "".endIndex return } - self._next = _base.current.wholeCharacters.index(after: i) + let path = string.path(to: start, preferEnd: false).path + self._base = string.rope.makeIterator(from: path.rope) + self._utf8BaseOffset -= path.chunk._utf8Offset + self._index = path.chunk + self._next = _base.current.wholeCharacters.index(after: path.chunk) } } internal func makeCharacterIterator() -> CharacterIterator { - CharacterIterator(_base: rope.makeIterator(), utf8BaseOffset: 0) + CharacterIterator(self) } internal func makeCharacterIterator(from index: Index) -> CharacterIterator { - if index == endIndex { - return CharacterIterator( - _base: rope.makeIterator(from: rope.endIndex), - utf8BaseOffset: index._utf8Offset) - } - let (path, _) = self.path(to: index, preferEnd: false) - let base = rope.makeIterator(from: path.rope) - return CharacterIterator( - _base: base, - utf8BaseOffset: index._utf8Offset - path.chunk._utf8Offset, - path.chunk) + CharacterIterator(self, from: index) } } diff --git a/Sources/RopeModule/BString/Conformances/_BString+Comparisons.swift b/Sources/RopeModule/BString/Conformances/_BString+Comparisons.swift index 4fac1847..3ab28258 100644 --- a/Sources/RopeModule/BString/Conformances/_BString+Comparisons.swift +++ b/Sources/RopeModule/BString/Conformances/_BString+Comparisons.swift @@ -58,6 +58,8 @@ extension _BString { var it1 = left.makeCharacterIterator(from: leftRange.lowerBound) var it2 = right.makeCharacterIterator(from: rightRange.upperBound) + if leftRange._isEmptyUTF8 { return rightRange._isEmptyUTF8 } + let leftEnd = left.path(to: leftRange.upperBound, preferEnd: true).path let rightEnd = right.path(to: rightRange.upperBound, preferEnd: true).path diff --git a/Sources/RopeModule/BString/Conformances/_BString+Hashing.swift b/Sources/RopeModule/BString/Conformances/_BString+Hashing.swift index 81192eae..6871c8fb 100644 --- a/Sources/RopeModule/BString/Conformances/_BString+Hashing.swift +++ b/Sources/RopeModule/BString/Conformances/_BString+Hashing.swift @@ -27,6 +27,20 @@ extension _BString { hasher.combine(0xFF as UInt8) } + internal func hashCharacters(into hasher: inout Hasher, from range: Range) { + var it = self.makeCharacterIterator(from: range.lowerBound) + let endOffset = range.upperBound._utf8Offset + while let character = it.next() { + var s = String(character) + if it.utf8Offset > endOffset { + let i = s.unicodeScalars._index(roundingDown: s._utf8Index(at: it.utf8Offset - endOffset)) + s.removeSubrange(i...) + } + s._withNFCCodeUnits { hasher.combine($0) } + } + hasher.combine(0xFF as UInt8) + } + /// Feed the UTF-8 encoding of `self` into hasher, with a terminating byte. internal func hashUTF8(into hasher: inout Hasher) { for chunk in self.rope {