Skip to content

[String] Fast paths for hasPrefix and hasSuffix #865

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
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
24 changes: 12 additions & 12 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1428,29 +1428,29 @@ extension String : _NSBridgeable, _CFBridgeable {
#if !(os(OSX) || os(iOS))
extension String {
public func hasPrefix(_ prefix: String) -> Bool {
if prefix.isEmpty {
return true
}

let cfstring = self._cfObject
let range = CFRangeMake(0, CFStringGetLength(cfstring))
let opts = CFStringCompareFlags(
kCFCompareAnchored | kCFCompareNonliteral)
if prefix.isEmpty {
return true
} else {
return CFStringFindWithOptions(cfstring, prefix._cfObject,
range, opts, nil)
}
return CFStringFindWithOptions(cfstring, prefix._cfObject,
range, opts, nil)
}

public func hasSuffix(_ suffix: String) -> Bool {
if suffix.isEmpty {
return true
}

let cfstring = self._cfObject
let range = CFRangeMake(0, CFStringGetLength(cfstring))
let opts = CFStringCompareFlags(
kCFCompareAnchored | kCFCompareBackwards | kCFCompareNonliteral)
if suffix.isEmpty {
return true
} else {
return CFStringFindWithOptions(cfstring, suffix._cfObject,
range, opts, nil)
}
return CFStringFindWithOptions(cfstring, suffix._cfObject,
range, opts, nil)
}
}
#endif
Expand Down