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

[SR-8416] Fatal error: cannot increment beyond endIndex on String.distance when specific unicode characters are present #50941

Open
swift-ci opened this issue Jul 31, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8416
Radar None
Original Reporter gmogames (JIRA User)
Type Bug

Attachment: Download

Environment

High Sierra 10.13.5
Macbook Pro 15-inch, 2017, 2.9Ghz intel Core i7, 16GB
XCode 9.4.1 (9F2000)
Swift 4

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: cdfbee80384d05d90e87af13186bb96b

Issue Description:

Trying to get the distance between indices of a string where the index is on specific unicode character causes the throw of a Fatal Error: cannot increment beyond endIndex.

The characters I was able to reproduce this issue are (without the quotes):

" ͡" or "͜"

Attached a playground with reproducible example, but this is the code:

let text = "funny face:  ͡ ͜ ͡"
let words = text.components(separatedBy: .whitespacesAndNewlines)
var bookmark = text.startIndex
for word in words {
    let wordSize = word.count
    let remainingRange = Range(bookmark..<text.endIndex)    
    if let matchRange: Range = text.range(of: word, options: .literal, range: remainingRange) {
        let wordIndex = text.distance(from: text.startIndex, to: matchRange.lowerBound) **** CRASHES HERE ****        
        guard let newIndex = text.index(text.startIndex, offsetBy: wordIndex + wordSize, limitedBy: text.endIndex) else { break }
        bookmark = newIndex
    }   
    guard let newIndex = text.index(remainingRange.lowerBound, offsetBy: wordSize, limitedBy: text.endIndex) else { break }

    bookmark = newIndex
}
@belkadan
Copy link
Contributor

cc @milseman

@swift-ci
Copy link
Collaborator Author

Comment by Guilherme Mogames (JIRA)

FYI: if using .utf16 for the Strings, then it works fine, but it took me a while to find this out. So I'm not sure if this is the expected behavior when not using .utf16 on the strings.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants