diff --git a/Sources/RopeModule/BString/Basics/_BString+Contents.swift b/Sources/RopeModule/BString/Basics/_BString+Contents.swift index 9bc335ec..04c8e545 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Contents.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Contents.swift @@ -273,7 +273,7 @@ extension _BString { precondition(offset >= 0 && offset <= utf8Count, "Index out of bounds") guard offset > 0, offset < utf8Count else { return Index(_utf8Offset: offset) } - let (path, chunk) = path(to: i, preferEnd: true) + var (path, chunk) = path(to: i, preferEnd: true) var base = baseIndex(with: i, at: path.chunk) if chunk.hasBreaks { let first = chunk.firstBreak @@ -290,12 +290,14 @@ extension _BString { } } - var it = self.rope.makeIterator(from: path.rope) - while it.stepBackward() { - base = base._advanceUTF8(by: -it.current.utf8Count) - if it.current.hasBreaks { break } + var i = path.rope + while i > self.rope.startIndex { + self.rope.formIndex(before: &i) + chunk = self.rope[i] + base = base._advanceUTF8(by: -chunk.utf8Count) + if chunk.hasBreaks { break } } - return base._advanceUTF8(by: it.current.string._utf8Offset(of: it.current.lastBreak)) + return base._advanceUTF8(by: chunk.lastBreak._utf8Offset) } func unicodeScalarIndex(roundingDown i: Index) -> Index { @@ -337,15 +339,23 @@ extension _BString { } var s = String(c) var base = base - var it = self.rope.makeIterator(from: start.rope) + var i = start.rope + var j = start.chunk while true { - base = base?._advanceUTF8(by: it.current.utf8Count) - guard it.stepForward() else { break } - let chunk = it.current + base = base?._advanceUTF8(by: rope[i].utf8Count) + rope.formIndex(after: &i) + guard i < rope.endIndex else { + j = "".endIndex + break + } + let chunk = rope[i] s.append(contentsOf: chunk.prefix) - if chunk.hasBreaks { break } + if chunk.hasBreaks { + j = rope[i].firstBreak + break + } } - return (Character(s), Path(it.index, it.current.firstBreak), base) + return (Character(s), Path(i, j), base) } subscript(utf8 index: Index) -> UInt8 { diff --git a/Sources/RopeModule/BString/Basics/_BString+Iterators.swift b/Sources/RopeModule/BString/Basics/_BString+Iterators.swift index e914fbef..0c31e6ad 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Iterators.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Iterators.swift @@ -146,21 +146,23 @@ extension _BString.UnicodeScalarIterator: IteratorProtocol { @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) extension _BString { internal struct CharacterIterator { - internal var _base: _Rope.Iterator + internal let _base: _BString + internal var _i: Rope.Index internal var _utf8BaseOffset: Int internal var _index: String.Index internal var _next: String.Index internal init(_ string: _BString) { + self._base = string + self._i = string.rope.startIndex self._utf8BaseOffset = 0 - self._base = string.rope.makeIterator() - guard !_base.isAtEnd else { + guard _i < string.rope.endIndex else { _index = "".startIndex _next = "".endIndex return } - let chunk = _base.current + let chunk = _base.rope[_i] assert(chunk.firstBreak == chunk.string.startIndex) self._index = chunk.firstBreak self._next = chunk.string[_index...].index(after: _index) @@ -170,19 +172,20 @@ extension _BString { _ string: _BString, from start: Index ) { + self._base = string self._utf8BaseOffset = start._utf8Offset if start == string.endIndex { - self._base = string.rope.makeIterator(from: string.rope.endIndex) + self._i = string.rope.endIndex self._index = "".startIndex self._next = "".endIndex return } let path = string.path(to: start, preferEnd: false).path - self._base = string.rope.makeIterator(from: path.rope) + self._i = path.rope self._utf8BaseOffset -= path.chunk._utf8Offset self._index = path.chunk - self._next = _base.current.wholeCharacters.index(after: path.chunk) + self._next = _base.rope[_i].wholeCharacters.index(after: path.chunk) } } @@ -204,53 +207,55 @@ extension _BString.CharacterIterator: IteratorProtocol { } internal var isAtStart: Bool { - _base.isAtStart && _index._utf8Offset == 0 + _i == _base.rope.startIndex && _index._utf8Offset == 0 } internal var current: Element { assert(!isAtEnd) - var r = _base.withCurrent { chunk -> (str: String, done: Bool) in - assert(_index >= chunk.firstBreak) - return (String(chunk.string[_index ..< _next]), _next < chunk.string.endIndex) + let chunk = _base.rope[_i] + var str = String(chunk.string[_index ..< _next]) + if _next < chunk.string.endIndex { return Character(str) } + + var i = _base.rope.index(after: _i) + while i < _base.rope.endIndex { + let chunk = _base.rope[i] + let b = chunk.firstBreak + str += chunk.string[.. Bool { guard !isAtEnd else { return false } - let chunk = _base.current + let chunk = _base.rope[_i] if _next < chunk.string.endIndex { _index = _next _next = chunk.wholeCharacters.index(after: _next) return true } - while true { - let c = _base.current.utf8Count - guard _base.stepForward() else { break } - _utf8BaseOffset += c - let chunk = _base.current - let i = chunk.firstBreak - if i < chunk.string.endIndex { - _index = i - _next = chunk.string[i...].index(after: i) + var baseOffset = _utf8BaseOffset + chunk.utf8Count + var i = _base.rope.index(after: _i) + while i < _base.rope.endIndex { + let chunk = _base.rope[i] + let b = chunk.firstBreak + if b < chunk.string.endIndex { + _i = i + _utf8BaseOffset = baseOffset + _index = b + _next = chunk.string[b...].index(after: b) return true } + baseOffset += chunk.utf8Count + _base.rope.formIndex(after: &i) } return false } mutating func stepBackward() -> Bool { if !isAtEnd { - let chunk = _base.current + let chunk = _base.rope[_i] let i = chunk.firstBreak if _index > i { _next = _index @@ -258,15 +263,21 @@ extension _BString.CharacterIterator: IteratorProtocol { return true } } - while _base.stepBackward() { - let chunk = _base.current - _utf8BaseOffset -= chunk.utf8Count + var i = _i + var baseOffset = _utf8BaseOffset + while i > _base.rope.startIndex { + _base.rope.formIndex(before: &i) + let chunk = _base.rope[i] + baseOffset -= chunk.utf8Count if chunk.hasBreaks { + _i = i + _utf8BaseOffset = baseOffset _next = chunk.string.endIndex _index = chunk.lastBreak return true } } + assert(_base.rope.isEmpty) return false } @@ -287,37 +298,38 @@ extension _BString.CharacterIterator { _utf8BaseOffset + _index._utf8Offset } + var index: _BString.Index { + _BString.Index(_utf8Offset: utf8Offset) + } + /// The UTF-8 offset range of the current character, measured from the start of the string. var utf8Range: Range { assert(!isAtEnd) let start = utf8Offset var end = _utf8BaseOffset - if _next < _base.current.string.endIndex { + var chunk = _base.rope[_i] + if _next < chunk.string.endIndex { end += _next._utf8Offset return Range(uncheckedBounds: (start, end)) } - var it = _base - while true { - end += it.current.utf8Count - guard it.stepForward() else { break } - let chunk = it.current - let i = chunk.firstBreak - if i < chunk.string.endIndex { - end += i._utf8Offset + end += chunk.utf8Count + var i = _base.rope.index(after: _i) + while i < _base.rope.endIndex { + chunk = _base.rope[i] + let b = chunk.firstBreak + if b < chunk.string.endIndex { + end += b._utf8Offset break } + end += chunk.utf8Count + _base.rope.formIndex(after: &i) } return Range(uncheckedBounds: (start, end)) } - var index: _BString.Index { - let utf8Base = _base.rope.offset(of: _base.index, in: _BString.UTF8Metric()) - return _BString.Index(_utf8Offset: utf8Base + _index._utf8Offset) - } - func isBelow(_ path: _BString.Path) -> Bool { - if self._base.index < path.rope { return true } - guard self._base.index == path.rope else { return false } + if _i < path.rope { return true } + guard _i == path.rope else { return false } return self._index < path.chunk } } diff --git a/Sources/RopeModule/BString/Operations/_BString+Initializers.swift b/Sources/RopeModule/BString/Operations/_BString+Initializers.swift index 7e16118a..9f98ce64 100644 --- a/Sources/RopeModule/BString/Operations/_BString+Initializers.swift +++ b/Sources/RopeModule/BString/Operations/_BString+Initializers.swift @@ -118,10 +118,10 @@ extension String { } self += start.chunk.string[start.path.chunk...] - - var it = big.rope.makeIterator(from: start.path.rope) - while it.stepForward(), it.index < end.path.rope { - self += it.current.string + var i = big.rope.index(after: start.path.rope) + while i < end.path.rope { + self += big.rope[i].string + big.rope.formIndex(after: &i) } self += end.chunk.string[.. rope.startIndex { + rope.formIndex(before: &i) + if rope[i].hasBreaks { break } } - precondition(it.index < end.rope) - let prev = Path(it.index, it.current.lastBreak) + precondition(i < end.rope) + let prev = Path(i, rope[i].lastBreak) // Collect grapheme breaking state. - var state = _CharacterRecognizer(partialCharacter: it.current.suffix) - while it.stepForward(), it.index < end.rope { - state.consumePartialCharacter(it.current.string[...]) + var state = _CharacterRecognizer(partialCharacter: rope[i].suffix) + rope.formIndex(after: &i) + while i < end.rope { + state.consumePartialCharacter(rope[i].string[...]) + rope.formIndex(after: &i) } - state.consumePartialCharacter(it.current.string[.. 0 { - var it = makeIterator(from: endIndex) - while it.stepBackward() { - if it.current.hasBreaks { break } - if it.index == index { break } + var i = endIndex + while i > startIndex { + formIndex(before: &i) + if self[i].hasBreaks { break } + if i == index { break } } - if it.index > index || it.current.lastBreak > i { - new = it.current.immediateLastBreakState! - while it.stepForward() { - new.consumePartialCharacter(it.current.string[...]) + if i > index || self[i].lastBreak > stringIndex { + new = self[i].immediateLastBreakState! + formIndex(after: &i) + while i < endIndex { + new.consumePartialCharacter(self[i].string[...]) + formIndex(after: &i) } old = new return } } - var it = makeIterator(from: index) - let suffix = it.current.string.unicodeScalars[i...].dropFirst() + + var i = index + let suffix = self[i].string.unicodeScalars[stringIndex...].dropFirst() new.consumePartialCharacter(suffix) - while it.stepForward() { - new.consumePartialCharacter(it.current.string[...]) + formIndex(after: &i) + while i < endIndex { + new.consumePartialCharacter(self[i].string[...]) + formIndex(after: &i) } old = new } diff --git a/Sources/RopeModule/Rope/Conformances/_Rope+Sequence.swift b/Sources/RopeModule/Rope/Conformances/_Rope+Sequence.swift index 6fa10817..58600513 100644 --- a/Sources/RopeModule/Rope/Conformances/_Rope+Sequence.swift +++ b/Sources/RopeModule/Rope/Conformances/_Rope+Sequence.swift @@ -29,53 +29,10 @@ extension _Rope: Sequence { self.rope.ensureLeaf(in: &index) } - var isAtEnd: Bool { - index._leaf == nil - } - - var isAtStart: Bool { - index == rope.startIndex - } - - var current: Element { - guard let leaf = index._leaf else { - preconditionFailure("Cannot access current element in iterator at end") - } - return leaf.read { $0.children[index._path[0]].value } - } - - func withCurrent(_ body: (Element) -> R) -> R { - guard let leaf = index._leaf else { - preconditionFailure("Cannot access current element in iterator at end") - } - return leaf.read { body($0.children[index._path[0]].value) } - } - - mutating func stepForward() -> Bool { - let end = rope.endPath - guard index._path < end else { return false } - let next = rope.index(after: index) - guard next._path < end else { return false } - index = next - return true - } - - mutating func stepBackward() -> Bool { - guard index._path > rope.startPath else { return false } - rope.formIndex(before: &index) - return true - } - - mutating func stepToEnd() { - index = rope.endIndex - } - mutating func next() -> Element? { - guard !isAtEnd else { return nil } - let item = self.current - if !stepForward() { - stepToEnd() - } + guard let leaf = index._leaf else { return nil } + let item = leaf.read { $0.children[index._path[0]].value } + rope.formIndex(after: &index) return item } }