Skip to content

Commit 47942b0

Browse files
committed
Test if two ascii string pointers are equal before memcmp
1 parent ad700c5 commit 47942b0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

stdlib/public/core/StringComparable.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ extension String {
5353
/// - Precondition: Both `self` and `rhs` are ASCII strings.
5454
public // @testable
5555
func _compareASCII(_ rhs: String) -> Int {
56-
var compare = Int(extendingOrTruncating: _swift_stdlib_memcmp(
57-
self._core.startASCII, rhs._core.startASCII,
58-
Swift.min(self._core.count, rhs._core.count)))
56+
var compare: Int
57+
58+
if self._core.startASCII == rhs._core.startASCII {
59+
compare = 0
60+
}
61+
else {
62+
compare = Int(extendingOrTruncating: _swift_stdlib_memcmp(
63+
self._core.startASCII, rhs._core.startASCII,
64+
Swift.min(self._core.count, rhs._core.count)))
65+
}
66+
5967
if compare == 0 {
6068
compare = self._core.count - rhs._core.count
6169
}
@@ -130,6 +138,9 @@ extension String : Equatable {
130138
if lhs._core.count != rhs._core.count {
131139
return false
132140
}
141+
if lhs._core.startASCII == rhs._core.startASCII {
142+
return true
143+
}
133144
return _swift_stdlib_memcmp(
134145
lhs._core.startASCII, rhs._core.startASCII,
135146
rhs._core.count) == (0 as CInt)

0 commit comments

Comments
 (0)