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-10456] String index(_:offsetBy:limitedBy:) don't return nil as expected #52856

Open
swift-ci opened this issue Apr 11, 2019 · 8 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. documentation standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10456
Radar None
Original Reporter 1442960012 (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 10.2,Swift 5

Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug, Documentation
Assignee amartini (JIRA)
Priority Medium

md5: 316746d417790f24988e58e3a206990a

Issue Description:

let s = "Swift"

    **if** **let** i = s.index(s.startIndex, offsetBy: 5, limitedBy: s.endIndex){

        print(s\[i\]) //will execute and Fatal error: String index is out of bounds

    }

}

@belkadan
Copy link
Contributor

The limitedBy: parameter sets the last valid value, not the first invalid value. The docs should probably clarify that. @natecook1000, @milseman?

@swift-ci
Copy link
Collaborator Author

Comment by Fon (JIRA)

It's really a documentation problem? Please see the code below.
-------------~~~~Documentation~~~~-------------
let s = "Swift"
if let i = s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex) {
print(s[i])
}
// Prints "t"

let j = s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex)
print(j)
// Prints "nil"
-------------~~~~Example~~~~-------------
let i = s.index(s.startIndex, offsetBy: 5,limitedBy: s.endIndex)
printℹ
//Crashed because String index is out of bounds. This should print "nil".

@belkadan
Copy link
Contributor

No, i == s.endIndex in your example, which is the expected behavior. That's a valid index to form, but not to use as a subscript.

@swift-ci
Copy link
Collaborator Author

Comment by Fon (JIRA)

let s = "Swift"
let startIndex=s.startIndex
var endIndex=s.endIndex

//These are valid.
if var i=s.index(s.startIndex, offsetBy: 5, limitedBy: s.endIndex){
   print(s.formIndex(&i, offsetBy: 0, limitedBy:s.endIndex))//prints true
}
print(s.formIndex(&endIndex, offsetBy: 0, limitedBy: s.endIndex))//prints true

print(s[startIndex])//prints "S"
print(s[..<endIndex])//prints "Swift"
print(s[s.endIndex])//Crashed error String index is out of bounds.

let name = "Marie Curie"
let firstSpace = name.firstIndex(of: " ") ?? name.endIndex
let firstName = name[..<firstSpace]
// firstName == "Marie"

I got it. It's a designed operation. I can use i only by s[..<endIndex]. But why don't allow to subscript a string by endIndex.

@belkadan
Copy link
Contributor

endIndex represents a boundary position for a Collection. It turns out this representation is more useful than, say, lastIndex, because it allows you to represent empty Collections as Collections where startIndex == endIndex. For a 5-element Array, for example, the endIndex would be 5, and the valid indexes are 0, 1, 2, 3, 4. For a 0-element Array, the endIndex is 0.

@swift-ci
Copy link
Collaborator Author

Comment by Alexander Ignition (JIRA)

@belkadan can we update the documentation so that it is not misleading ?

for example

let numbers = [10, 20, 30, 40, 50]
if let i = numbers.index(numbers.startIndex,
                         offsetBy: 4,
                         limitedBy: numbers.index(before: numbers.endIndex)) {
    print(numbers[i])
}

@belkadan
Copy link
Contributor

I'm not at Apple anymore, so I'm not sure who's handling documentation these days. @krilnon?

@krilnon
Copy link
Member

krilnon commented Mar 13, 2020

That’d be amartini (JIRA User).

@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. documentation standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants