Skip to content

NSRange to Range<AttributedString.Index> conversion should be relative to sliced boundaries #1050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ extension Range where Bound == AttributedString.Index {
guard range.location != NSNotFound else { return nil }
guard range.location >= 0, range.length >= 0 else { return nil }
let endOffset = range.location + range.length
let bstr = string.__guts.string
let bstrBounds = Range<BigString.Index>(uncheckedBounds: (string.startIndex._value, string.endIndex._value))
let bstr = string.__guts.string[bstrBounds]
guard endOffset <= bstr.utf16.count else { return nil }

let start = bstr.utf16.index(bstr.startIndex, offsetBy: range.location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,14 @@ E {
}
}

func testNSRangeConversionOnSlice() throws {
let str = AttributedString("012345")
let slice = str[str.index(str.startIndex, offsetByCharacters: 3) ..< str.endIndex]
let nsRange = NSRange(location: 0, length: 2)
let range = try XCTUnwrap(Range(nsRange, in: slice))
XCTAssertEqual(String(slice[range].characters), "34")
}

#endif // FOUNDATION_FRAMEWORK

func testOOBRangeConversion() {
Expand Down