Skip to content
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

strings: improve equality check on simple cases #70195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 10 additions & 15 deletions src/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,9 +1185,13 @@ func ReplaceAll(s, old, new string) string {
// are equal under simple Unicode case-folding, which is a more general
// form of case-insensitivity.
func EqualFold(s, t string) bool {
if len(s) != len(t) {
return false
}

// ASCII fast path
i := 0
for ; i < len(s) && i < len(t); i++ {
for ; i < len(s); i++ {
sr := s[i]
tr := t[i]
if sr|tr >= utf8.RuneSelf {
Expand All @@ -1205,22 +1209,15 @@ func EqualFold(s, t string) bool {
}
// ASCII only, sr/tr must be upper/lower case
if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
continue
return false
}
return false
}
// Check if we've exhausted both strings.
return len(s) == len(t)
return true

hasUnicode:
s = s[i:]
t = t[i:]
for _, sr := range s {
// If t is exhausted the strings are not equal.
if len(t) == 0 {
return false
}

// Extract first rune from second string.
var tr rune
if t[0] < utf8.RuneSelf {
Expand Down Expand Up @@ -1256,14 +1253,12 @@ hasUnicode:
for r != sr && r < tr {
r = unicode.SimpleFold(r)
}
if r == tr {
continue
if r != tr {
return false
}
return false
}

// First string is empty, so check if the second one is also empty.
return len(t) == 0
return true
}

// Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
Expand Down