Skip to content

[stdlib] Test if two ascii string pointers are equal before memcmp #10018

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
17 changes: 14 additions & 3 deletions stdlib/public/core/StringComparable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ extension String {
/// - Precondition: Both `self` and `rhs` are ASCII strings.
public // @testable
func _compareASCII(_ rhs: String) -> Int {
var compare = Int(extendingOrTruncating: _swift_stdlib_memcmp(
self._core.startASCII, rhs._core.startASCII,
Swift.min(self._core.count, rhs._core.count)))
var compare: Int

if self._core.startASCII == rhs._core.startASCII {
compare = 0
}
else {
compare = Int(extendingOrTruncating: _swift_stdlib_memcmp(
self._core.startASCII, rhs._core.startASCII,
Swift.min(self._core.count, rhs._core.count)))
}

if compare == 0 {
compare = self._core.count - rhs._core.count
}
Expand Down Expand Up @@ -130,6 +138,9 @@ extension String : Equatable {
if lhs._core.count != rhs._core.count {
return false
}
if lhs._core.startASCII == rhs._core.startASCII {
return true
}
return _swift_stdlib_memcmp(
lhs._core.startASCII, rhs._core.startASCII,
rhs._core.count) == (0 as CInt)
Expand Down