Skip to content

Commit b8231ea

Browse files
authored
Merge pull request #744 from sshamaia/nsstringprefix-branch
2 parents fe3fe61 + 44bbb33 commit b8231ea

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

Foundation/NSString.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -1432,18 +1432,25 @@ extension String {
14321432
let range = CFRangeMake(0, CFStringGetLength(cfstring))
14331433
let opts = CFStringCompareFlags(
14341434
kCFCompareAnchored | kCFCompareNonliteral)
1435-
1436-
return CFStringFindWithOptions(cfstring, prefix._cfObject,
1435+
if prefix.isEmpty {
1436+
return true
1437+
} else {
1438+
return CFStringFindWithOptions(cfstring, prefix._cfObject,
14371439
range, opts, nil)
1440+
}
14381441
}
14391442

14401443
public func hasSuffix(_ suffix: String) -> Bool {
14411444
let cfstring = self._cfObject
14421445
let range = CFRangeMake(0, CFStringGetLength(cfstring))
14431446
let opts = CFStringCompareFlags(
14441447
kCFCompareAnchored | kCFCompareBackwards | kCFCompareNonliteral)
1445-
return CFStringFindWithOptions(cfstring, suffix._cfObject,
1448+
if suffix.isEmpty {
1449+
return true
1450+
} else {
1451+
return CFStringFindWithOptions(cfstring, suffix._cfObject,
14461452
range, opts, nil)
1453+
}
14471454
}
14481455
}
14491456
#endif

TestFoundation/TestNSString.swift

+18-8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class TestNSString : XCTestCase {
9090
("test_deletingPathExtension", test_deletingPathExtension),
9191
("test_ExternalRepresentation", test_ExternalRepresentation),
9292
("test_mutableStringConstructor", test_mutableStringConstructor),
93+
("test_emptyStringPrefixAndSuffix",test_emptyStringPrefixAndSuffix),
9394
("test_PrefixSuffix", test_PrefixSuffix),
9495
("test_utf16StringRangeCount", test_StringUTF16ViewIndexStrideableRange),
9596
("test_reflection", { _ in test_reflection }),
@@ -1056,6 +1057,12 @@ class TestNSString : XCTestCase {
10561057
XCTAssertEqual(outContentsEndIndex, twoLines.index(twoLines.startIndex, offsetBy: 11))
10571058
XCTAssertEqual(outEndIndex, twoLines.index(twoLines.startIndex, offsetBy: 12))
10581059
}
1060+
1061+
func test_emptyStringPrefixAndSuffix() {
1062+
let testString = "hello"
1063+
XCTAssertTrue(testString.hasPrefix(""))
1064+
XCTAssertTrue(testString.hasSuffix(""))
1065+
}
10591066
}
10601067

10611068
struct ComparisonTest {
@@ -1086,7 +1093,8 @@ let comparisonTests = [
10861093
// ASCII cases
10871094
ComparisonTest("t", "tt"),
10881095
ComparisonTest("t", "Tt"),
1089-
ComparisonTest("\u{0}", ""),
1096+
ComparisonTest("\u{0}", "",
1097+
reason: "https://bugs.swift.org/browse/SR-332"),
10901098
ComparisonTest("\u{0}", "\u{0}",
10911099
reason: "https://bugs.swift.org/browse/SR-332"),
10921100
ComparisonTest("\r\n", "t"),
@@ -1183,17 +1191,21 @@ enum Stack: Swift.Error {
11831191
}
11841192

11851193
func checkHasPrefixHasSuffix(_ lhs: String, _ rhs: String, _ stack: [UInt]) -> Int {
1186-
if lhs == "" {
1194+
if (lhs == "" && rhs == "") {
11871195
var failures = 0
1188-
failures += lhs.hasPrefix(rhs) ? 1 : 0
1189-
failures += lhs.hasSuffix(rhs) ? 1 : 0
1196+
failures += lhs.hasPrefix(rhs) ? 0: 1
1197+
failures += lhs.hasSuffix(rhs) ? 0: 1
11901198
return failures
1191-
}
1192-
if rhs == "" {
1199+
} else if lhs == "" {
11931200
var failures = 0
11941201
failures += lhs.hasPrefix(rhs) ? 1 : 0
11951202
failures += lhs.hasSuffix(rhs) ? 1 : 0
11961203
return failures
1204+
} else if rhs == "" {
1205+
var failures = 0
1206+
failures += lhs.hasPrefix(rhs) ? 0 : 1
1207+
failures += lhs.hasSuffix(rhs) ? 0 : 1
1208+
return failures
11971209
}
11981210

11991211
// To determine the expected results, compare grapheme clusters,
@@ -1228,7 +1240,6 @@ func checkHasPrefixHasSuffix(_ lhs: String, _ rhs: String, _ stack: [UInt]) -> I
12281240

12291241
extension TestNSString {
12301242
func test_PrefixSuffix() {
1231-
#if !_runtime(_ObjC)
12321243
for test in comparisonTests {
12331244
var failures = 0
12341245
failures += checkHasPrefixHasSuffix(test.lhs, test.rhs, [test.loc, #line])
@@ -1249,7 +1260,6 @@ extension TestNSString {
12491260
}
12501261
XCTAssert(test.xfail == fail, "Unexpected \(test.xfail ?"success":"failure"): \(test.loc)")
12511262
}
1252-
#endif
12531263
}
12541264
}
12551265

0 commit comments

Comments
 (0)