From 3d8c29eaea9743ac7bd22022a878b320f4eb4396 Mon Sep 17 00:00:00 2001 From: David Smith Date: Mon, 29 Nov 2021 12:09:57 -0800 Subject: [PATCH] Early out for unequal NFC counts in String == --- stdlib/public/core/StringComparison.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/stdlib/public/core/StringComparison.swift b/stdlib/public/core/StringComparison.swift index fb00567768bff..f68b17afdbb50 100644 --- a/stdlib/public/core/StringComparison.swift +++ b/stdlib/public/core/StringComparison.swift @@ -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 { + return false + } let cmp = _binaryCompare(utf8Left, utf8Right) return _lexicographicalCompare(cmp, 0, expecting: expecting) }