Skip to content

Commit 32f452a

Browse files
committed
strings: faster equality check on simple cases
1 parent bea9b91 commit 32f452a

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/strings/strings.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,9 +1185,13 @@ func ReplaceAll(s, old, new string) string {
11851185
// are equal under simple Unicode case-folding, which is a more general
11861186
// form of case-insensitivity.
11871187
func EqualFold(s, t string) bool {
1188+
if len(s) != len(t) {
1189+
return false
1190+
}
1191+
11881192
// ASCII fast path
11891193
i := 0
1190-
for ; i < len(s) && i < len(t); i++ {
1194+
for ; i < len(s); i++ {
11911195
sr := s[i]
11921196
tr := t[i]
11931197
if sr|tr >= utf8.RuneSelf {
@@ -1205,22 +1209,15 @@ func EqualFold(s, t string) bool {
12051209
}
12061210
// ASCII only, sr/tr must be upper/lower case
12071211
if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
1208-
continue
1212+
return false
12091213
}
1210-
return false
12111214
}
1212-
// Check if we've exhausted both strings.
1213-
return len(s) == len(t)
1215+
return true
12141216

12151217
hasUnicode:
12161218
s = s[i:]
12171219
t = t[i:]
12181220
for _, sr := range s {
1219-
// If t is exhausted the strings are not equal.
1220-
if len(t) == 0 {
1221-
return false
1222-
}
1223-
12241221
// Extract first rune from second string.
12251222
var tr rune
12261223
if t[0] < utf8.RuneSelf {
@@ -1256,14 +1253,12 @@ hasUnicode:
12561253
for r != sr && r < tr {
12571254
r = unicode.SimpleFold(r)
12581255
}
1259-
if r == tr {
1260-
continue
1256+
if r != tr {
1257+
return false
12611258
}
1262-
return false
12631259
}
12641260

1265-
// First string is empty, so check if the second one is also empty.
1266-
return len(t) == 0
1261+
return true
12671262
}
12681263

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

0 commit comments

Comments
 (0)