Skip to content
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
11 changes: 11 additions & 0 deletions stdlib/public/core/StringComparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ private func _stringCompareFastUTF8(
bothNFC: Bool
) -> Bool {
if _fastPath(bothNFC) {
/*
If we know both Strings are NFC *and* we're just checking
equality, then we can early-out without looking at the contents
if the UTF8 counts are different (without the NFC req, equal
characters can have different counts). It might be nicer to do
this in _binaryCompare, but we have the information about what
operation we're trying to do at this level.
*/
if expecting == .equal && utf8Left.count != utf8Right.count {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another opportunity is to sink it into binaryCompare, since that could short circuit too for equality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But really, equity is way way way more common than comparison, so I'm in favor of this check and/or a more specialized code path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wanted to sink it into _binaryCompare too, but that has to produce < = >, so it has to inspect up to the first mismatch. This seems like a good fix.

return false
}
let cmp = _binaryCompare(utf8Left, utf8Right)
return _lexicographicalCompare(cmp, 0, expecting: expecting)
}
Expand Down