Skip to content
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

Out of bounds, case crash. #10

Closed
orangeince opened this issue Apr 30, 2019 · 0 comments
Closed

Out of bounds, case crash. #10

orangeince opened this issue Apr 30, 2019 · 0 comments

Comments

@orangeince
Copy link
Contributor

Problem

App crashed when the text of UILabel is come from network data parse.May be caused by
string encoding. I simulated the scene with SwiftPlayground.

let key = ""
let text = String(data: "明天开始".data(using: .unicode)!, encoding: .unicode)!
let ranges = text.lowercased().ranges(of: key)
print("matchedRanges: \(ranges)")
// matchedRanges: [Range(Swift.String.Index(_rawBits: 196608)..<Swift.String.Index(_rawBits: 393216))]

print("originText.endIndex: \(text.endIndex)")
// originText.endIndex: Index(_rawBits: 262144)

print("lowercased.endIndex: \(text.lowercased().endIndex)")
// lowercased.endIndex: Index(_rawBits: 786432)

The upperBound of matched range is greater than the originText.endIndex.

Solution

Remove the lowercased option before match range and use CompareOptions.caseInsensitive as the default value of ranges method.

I am happy to submit PR if you allow it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant