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-337] hasPrefix and hasSuffix not implemented in Swift #42959

Closed
alblue opened this issue Dec 22, 2015 · 4 comments
Closed

[SR-337] hasPrefix and hasSuffix not implemented in Swift #42959

alblue opened this issue Dec 22, 2015 · 4 comments

Comments

@alblue
Copy link
Contributor

alblue commented Dec 22, 2015

Previous ID SR-337
Radar None
Original Reporter @alblue
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s
Labels Improvement
Assignee None
Priority Medium

md5: 7139ff1a5de95a9c013b0ae4cb94233c

Issue Description:

In stdlib/public/core/StringLegacy.swift there is a fixme to implement hasPrefix and hasSuffix without Objective-C

// FIXME: Implement hasPrefix and hasSuffix without objc
// rdar://problem/18878343

I'd like to give this a go.

@alblue
Copy link
Contributor Author

alblue commented Dec 22, 2015

I've implemented this with:

extension String {
func hasPrefix(other:String) -> Bool {
let otherCharacters = other.characters
if otherCharacters.isEmpty {
return false
}
let otherStart = otherCharacters.startIndex
let otherEnd = otherCharacters.endIndex

let selfCharacters = self.characters
let selfStart = selfCharacters.startIndex
let selfEnd = selfCharacters.endIndex

var s = selfStart
var o = otherStart

while ( s < selfEnd && o < otherEnd ) {
if selfCharacters[s] != otherCharacters[o] {
return false
}
s = s.successor()
o = o.successor()
}
return o == otherEnd
}
func hasSuffix(other:String) -> Bool {
let otherCharacters = other.characters
if otherCharacters.isEmpty {
return false
}
let otherStart = otherCharacters.startIndex
let otherEnd = otherCharacters.endIndex

let selfCharacters = self.characters
if selfCharacters.isEmpty {
return false
}
let selfStart = selfCharacters.startIndex
let selfEnd = selfCharacters.endIndex

var s = selfEnd
var o = otherEnd

repeat {
s = s.predecessor()
o = o.predecessor()
if selfCharacters[s] != otherCharacters[o] {
return false
}
} while ( s > selfStart && o > otherStart )
return o == otherStart
}
}

Which file should it go into? The existing hasPrefix/hasSuffix on OSX in stdlib/public/core/StringLegacy.swift but I don't know if that's the right place to put it.

@alblue
Copy link
Contributor Author

alblue commented Dec 22, 2015

Other potential pulls implementing the same features:

#440
#630

@alblue
Copy link
Contributor Author

alblue commented Dec 22, 2015

Created pull request for the above at #737

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

Fixed in f9b3e14.

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

No branches or pull requests

2 participants