Skip to content

Commit 1fff4b7

Browse files
authored
(139680932) NSRange to Range<AttributedString.Index> conversion should be relative to sliced boundaries (#1050)
1 parent eeb7ba3 commit 1fff4b7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Sources/FoundationEssentials/AttributedString/Conversion.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ extension Range where Bound == AttributedString.Index {
438438
guard range.location != NSNotFound else { return nil }
439439
guard range.location >= 0, range.length >= 0 else { return nil }
440440
let endOffset = range.location + range.length
441-
let bstr = string.__guts.string
441+
let bstrBounds = Range<BigString.Index>(uncheckedBounds: (string.startIndex._value, string.endIndex._value))
442+
let bstr = string.__guts.string[bstrBounds]
442443
guard endOffset <= bstr.utf16.count else { return nil }
443444

444445
let start = bstr.utf16.index(bstr.startIndex, offsetBy: range.location)

Tests/FoundationEssentialsTests/AttributedString/AttributedStringTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,14 @@ E {
23722372
}
23732373
}
23742374

2375+
func testNSRangeConversionOnSlice() throws {
2376+
let str = AttributedString("012345")
2377+
let slice = str[str.index(str.startIndex, offsetByCharacters: 3) ..< str.endIndex]
2378+
let nsRange = NSRange(location: 0, length: 2)
2379+
let range = try XCTUnwrap(Range(nsRange, in: slice))
2380+
XCTAssertEqual(String(slice[range].characters), "34")
2381+
}
2382+
23752383
#endif // FOUNDATION_FRAMEWORK
23762384

23772385
func testOOBRangeConversion() {

0 commit comments

Comments
 (0)