Assorted bug fixes and refinements

This commit is contained in:
Karoy Lorentey committed 2023-03-20 13:12:08 -07:00
1 parent 96aebb90b7
commit 283d4071bc
7 files changed
+141 -40

No files matched your search

@@ -36,10 +36,9 @@ extension _BString {
to end: Index,
in metric: some _StringMetric
) -> Int {
guard !isEmpty else {
precondition(start == end && start == endIndex, "Invalid index")
return 0
}
precondition(start <= endIndex && end <= endIndex, "Invalid index")
guard start != end else { return 0 }
assert(!isEmpty)
let a = path(to: Swift.min(start, end), preferEnd: false)
let b = path(to: Swift.max(start, end), preferEnd: true)
if a.path.rope == b.path.rope {
@@ -320,12 +319,22 @@ extension _BString {
return chunk.string.utf8[path.chunk]
}
subscript(utf8 offset: Int) -> UInt8 {
let (path, chunk) = _path(startIndex, offsetBy: offset, in: UTF8Metric())
return chunk.string.utf8[path.chunk]
}
subscript(utf16 index: Index) -> UInt16 {
precondition(index < endIndex, "Index out of bounds")
let (path, chunk) = path(to: index, preferEnd: false)
return chunk.string.utf16[path.chunk]
}
subscript(utf16 offset: Int) -> UInt16 {
let (path, chunk) = _path(startIndex, offsetBy: offset, in: UTF16Metric())
return chunk.string.utf16[path.chunk]
}
subscript(character index: Index) -> Character {
precondition(index < endIndex, "Index out of bounds")
let (path, chunk) = path(to: index, preferEnd: false)
@@ -135,11 +135,13 @@ extension _BString.UnicodeScalarIterator: IteratorProtocol {
extension _BString {
internal struct CharacterIterator {
internal var _utf8BaseOffset: Int
internal var _base: _Rope<Chunk>.Iterator
internal var _index: String.Index
internal var _next: String.Index
internal init(_base: _Rope<Chunk>.Iterator) {
internal init(_base: _Rope<Chunk>.Iterator, utf8BaseOffset: Int) {
self._utf8BaseOffset = utf8BaseOffset
self._base = _base
guard !_base.isAtEnd else {
_index = "".startIndex
@@ -152,7 +154,8 @@ extension _BString {
self._next = chunk.string[_index...].index(after: _index)
}
internal init(_base: _Rope<Chunk>.Iterator, _ i: String.Index) {
internal init(_base: _Rope<Chunk>.Iterator, utf8BaseOffset: Int, _ i: String.Index) {
self._utf8BaseOffset = utf8BaseOffset
self._base = _base
self._index = i
assert(i >= _base.current.firstBreak)
@@ -165,16 +168,21 @@ extension _BString {
}
internal func makeCharacterIterator() -> CharacterIterator {
CharacterIterator(_base: rope.makeIterator())
CharacterIterator(_base: rope.makeIterator(), utf8BaseOffset: 0)
}
internal func makeCharacterIterator(from index: Index) -> CharacterIterator {
if index == endIndex {
return CharacterIterator(_base: rope.makeIterator(from: rope.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, path.chunk)
return CharacterIterator(
_base: base,
utf8BaseOffset: index._utf8Offset - path.chunk._utf8Offset,
path.chunk)
}
}
@@ -215,7 +223,10 @@ extension _BString.CharacterIterator: IteratorProtocol {
_next = chunk.wholeCharacters.index(after: _next)
return true
}
while _base.stepForward() {
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 {
@@ -239,6 +250,7 @@ extension _BString.CharacterIterator: IteratorProtocol {
}
while _base.stepBackward() {
let chunk = _base.current
_utf8BaseOffset -= chunk.utf8Count
if chunk.hasBreaks {
_next = chunk.string.endIndex
_index = chunk.lastBreak
@@ -259,8 +271,42 @@ extension _BString.CharacterIterator: IteratorProtocol {
}
extension _BString.CharacterIterator {
// The UTF-8 offset of the current position, from the start of the string.
var utf8Offset: Int {
_utf8BaseOffset + _index._utf8Offset
}
/// The UTF-8 offset range of the current character, measured from the start of the string.
var utf8Range: Range<Int> {
assert(!isAtEnd)
let start = utf8Offset
var end = _utf8BaseOffset
if _next < _base.current.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
break
}
}
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 }
return self._index < path.chunk
}
}
@@ -37,6 +37,38 @@ extension _BString {
return true
}
internal static func characterIsEqual(
_ left: Self,
in leftRange: Range<Index>,
to right: Self,
in rightRange: Range<Index>
) -> 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), leftRange == rightRange { return true }
// FIXME: Even if we keep doing characterwise comparisons, we should skip over shared subtrees.
var it1 = left.makeCharacterIterator(from: leftRange.lowerBound)
var it2 = right.makeCharacterIterator(from: rightRange.upperBound)
let leftEnd = left.path(to: leftRange.upperBound, preferEnd: true).path
let rightEnd = right.path(to: rightRange.upperBound, preferEnd: true).path
var a: Character? = nil
var b: Character? = nil
while it1.isBelow(leftEnd), it2.isBelow(rightEnd) {
a = it1.next()
b = it2.next()
assert(a != nil && b != nil)
guard a == b else { return false }
}
return !it1.isBelow(leftEnd) && !it2.isBelow(rightEnd)
}
internal func characterIsLess(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
@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
extension Range<_BString.Index> {
internal var _isEmptyUTF8: Bool {
lowerBound._utf8Offset == upperBound._utf8Offset
}
}
@@ -77,6 +77,14 @@ extension _BString {
extension String {
internal init(_from big: _BString) {
guard !big.isEmpty else {
self.init()
return
}
if big.rope.isSingleton {
self = big.rope[big.rope.startIndex].string
return
}
self.init()
self.reserveCapacity(big.utf8Count)
for chunk in big.rope {
@@ -86,11 +94,17 @@ extension String {
internal init(_from big: _BString, in range: Range<_BString.Index>) {
self.init()
guard !range._isEmptyUTF8 else {
let estimatedUTF8Size = range.upperBound._utf8Offset - range.lowerBound._utf8Offset
guard estimatedUTF8Size > 0 else {
// We can safely ignore UTF-16 offsets, as we're rounding down to scalar boundaries
// anyway.
return
}
// Note: if the range isn't scalar aligned, then this may be slightly under- or overestimating
// the actual size of the result. (Underestimations can be problematic due to String's
// exponential resizing. If those become an issue, consider adding +3 here.)
self.reserveCapacity(estimatedUTF8Size)
let start = big.path(to: range.lowerBound, preferEnd: false)
let end = big.path(to: range.upperBound, preferEnd: true)