mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
_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:
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) {
|
||||
|
||||
Reference in new issue
Block a user