_BString: Fix character index rounding; use it when accessing character data

Oops, `_BString.subscript(character:)` failed to round down to a character boundary before trying to access the addressed character. It also resolved the index with `preferEnd` set to true, which isn’t what we actually want.
This commit is contained in:
Karoy Lorentey committed 2023-03-20 13:12:12 -07:00
1 parent e0f8305a99
commit eda72e344f
3 files changed
+70 -9

No files matched your search

@@ -330,7 +330,7 @@ extension _BString {
guard offset > 0 else { return resolve(i, preferEnd: false)._knownCharacterAligned() }
guard offset < utf8Count else { return resolve(i, preferEnd: true)._knownCharacterAligned() }
let i = resolve(i, preferEnd: true)
let i = resolve(i, preferEnd: false)
guard !i._isKnownCharacterAligned else { return i }
var ri = i._rope!
@@ -430,12 +430,13 @@ extension _BString {
@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension _BString {
func _character(at start: Index) -> (character: Character, end: Index) {
let start = characterIndex(roundingDown: start)
precondition(start._utf8Offset < utf8Count, "Index out of bounds")
let start = resolve(start, preferEnd: false)
var ri = start._rope!
var ci = start._chunkIndex
var chunk = rope[ri]
let char = chunk.string[ci]
let char = chunk.wholeCharacters[ci]
let endOffset = start._utf8ChunkOffset + char.utf8.count
if endOffset < chunk.utf8Count {
let endStringIndex = chunk.string._utf8Index(at: endOffset)
@@ -56,10 +56,10 @@ extension _BString.Index {
internal static var _utf16TrailingSurrogateBits: UInt64 { 0x400 }
@inline(__always)
internal static var _characterAlignmentBits: UInt64 { 0x300 }
internal static var _characterAlignmentBit: UInt64 { 0x200 }
@inline(__always)
internal static var _scalarAlignmentBits: UInt64 { 0x100 }
internal static var _scalarAlignmentBit: UInt64 { 0x100 }
internal var _utf8Offset: Int {
Int(truncatingIfNeeded: _rawBits &>> 11)
@@ -125,22 +125,22 @@ extension _BString.Index {
internal func _knownScalarAligned() -> Self {
var copy = self
copy._flags = Self._scalarAlignmentBits
copy._flags = Self._scalarAlignmentBit
return copy
}
internal func _knownCharacterAligned() -> Self {
var copy = self
copy._flags = Self._characterAlignmentBits
copy._flags = Self._characterAlignmentBit | Self._scalarAlignmentBit
return copy
}
internal var _isKnownScalarAligned: Bool {
_rawBits & Self._scalarAlignmentBits != 0
_rawBits & Self._scalarAlignmentBit != 0
}
internal var _isKnownCharacterAligned: Bool {
_rawBits & Self._characterAlignmentBits != 0
_rawBits & Self._characterAlignmentBit != 0
}
internal init(_utf8Offset: Int) {
+60
View File
@@ -433,5 +433,65 @@ class TestBString: XCTestCase {
checkCharacterIndices(flat, big)
}
}
func testCharacterIndexRounding() {
let ref = sampleString
let str = _BString(ref)
func check(
_ indices: some Sequence<_BString.Index>,
file: StaticString = #file,
line: UInt = #line
) {
var current = str.startIndex
var next = str.characterIndex(after: current)
for i in indices {
if i >= next {
current = next
next = str.characterIndex(after: current)
}
let j = str.characterIndex(roundingDown: i)
XCTAssertEqual(j, current, "i: \(i)", file: file, line: line)
XCTAssertEqual(str[character: i], str[character: j], "i: \(i)", file: file, line: line)
}
XCTAssertEqual(next, str.endIndex, "end", file: file, line: line)
}
check(str.utf8.indices)
check(str.utf16.indices)
check(str.unicodeScalars.indices)
check(str.characters.indices)
XCTAssertEqual(str.characterIndex(roundingDown: str.endIndex), str.endIndex)
}
func testUnicodeScalarIndexRounding() {
let ref = sampleString
let str = _BString(ref)
func check(
_ indices: some Sequence<_BString.Index>,
file: StaticString = #file,
line: UInt = #line
) {
var current = str.startIndex
var next = str.unicodeScalarIndex(after: current)
for i in indices {
while i >= next {
current = next
next = str.unicodeScalarIndex(after: current)
}
let j = str.unicodeScalarIndex(roundingDown: i)
XCTAssertEqual(j, current, "\(i)", file: file, line: line)
XCTAssertEqual(str[unicodeScalar: i], str[unicodeScalar: j], "i: \(i)", file: file, line: line)
}
}
check(str.utf8.indices)
check(str.utf16.indices)
check(str.unicodeScalars.indices)
check(str.characters.indices)
XCTAssertEqual(str.unicodeScalarIndex(roundingDown: str.endIndex), str.endIndex)
}
}
#endif