Skip to content

Commit eb1b37b

Browse files
committed
[String] Fast paths for hasPrefix and hasSuffix
1 parent 01ffe57 commit eb1b37b

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Foundation/NSString.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,29 +1428,29 @@ extension String : _NSBridgeable, _CFBridgeable {
14281428
#if !(os(OSX) || os(iOS))
14291429
extension String {
14301430
public func hasPrefix(_ prefix: String) -> Bool {
1431+
if prefix.isEmpty {
1432+
return true
1433+
}
1434+
14311435
let cfstring = self._cfObject
14321436
let range = CFRangeMake(0, CFStringGetLength(cfstring))
14331437
let opts = CFStringCompareFlags(
14341438
kCFCompareAnchored | kCFCompareNonliteral)
1435-
if prefix.isEmpty {
1436-
return true
1437-
} else {
1438-
return CFStringFindWithOptions(cfstring, prefix._cfObject,
1439-
range, opts, nil)
1440-
}
1439+
return CFStringFindWithOptions(cfstring, prefix._cfObject,
1440+
range, opts, nil)
14411441
}
14421442

14431443
public func hasSuffix(_ suffix: String) -> Bool {
1444+
if suffix.isEmpty {
1445+
return true
1446+
}
1447+
14441448
let cfstring = self._cfObject
14451449
let range = CFRangeMake(0, CFStringGetLength(cfstring))
14461450
let opts = CFStringCompareFlags(
14471451
kCFCompareAnchored | kCFCompareBackwards | kCFCompareNonliteral)
1448-
if suffix.isEmpty {
1449-
return true
1450-
} else {
1451-
return CFStringFindWithOptions(cfstring, suffix._cfObject,
1452-
range, opts, nil)
1453-
}
1452+
return CFStringFindWithOptions(cfstring, suffix._cfObject,
1453+
range, opts, nil)
14541454
}
14551455
}
14561456
#endif

0 commit comments

Comments
 (0)