diff --git a/Sources/RopeModule/BString/Basics/_BString+Contents.swift b/Sources/RopeModule/BString/Basics/_BString+Contents.swift index 008b973f..2bdb2701 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Contents.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Contents.swift @@ -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) diff --git a/Sources/RopeModule/BString/Basics/_BString+Index.swift b/Sources/RopeModule/BString/Basics/_BString+Index.swift index 8dca907a..1c9add4a 100644 --- a/Sources/RopeModule/BString/Basics/_BString+Index.swift +++ b/Sources/RopeModule/BString/Basics/_BString+Index.swift @@ -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) { diff --git a/Tests/RopeModuleTests/TestBString.swift b/Tests/RopeModuleTests/TestBString.swift index 59d6d371..50737dd7 100644 --- a/Tests/RopeModuleTests/TestBString.swift +++ b/Tests/RopeModuleTests/TestBString.swift @@ -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