aboutsummaryrefslogtreecommitdiff
path: root/src/strings
diff options
context:
space:
mode:
authorEric Pauley <eric@pauley.me>2018-04-29 00:15:03 -0400
committerBrad Fitzpatrick <bradfitz@golang.org>2018-05-01 18:52:19 +0000
commit9d11c63b64d1f29f43cfd7397614baa24c72a151 (patch)
treeb716a94ccd9561dcb5162b746138dc9077903d46 /src/strings
parenta5b80d469a1d07f77067075f9cbff80f5edc2323 (diff)
downloadgo-9d11c63b64d1f29f43cfd7397614baa24c72a151.tar.xz
bytes, strings: improve EqualFold fast version for ASCII
The existing implementation only considers the special ASCII case when the lower character is an upper case letter. This means that most ASCII comparisons use unicode.SimpleFold even when it is not necessary. benchmark old ns/op new ns/op delta BenchmarkEqualFold-8 450 390 -13.33% Change-Id: I735ca3c30fc0145c186d2a54f31fd39caab2c3fa Reviewed-on: https://go-review.googlesource.com/110018 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/strings')
-rw-r--r--src/strings/strings.go6
-rw-r--r--src/strings/strings_test.go12
2 files changed, 15 insertions, 3 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go
index b0a53fdefd..45345e0088 100644
--- a/src/strings/strings.go
+++ b/src/strings/strings.go
@@ -908,9 +908,9 @@ func EqualFold(s, t string) bool {
tr, sr = sr, tr
}
// Fast check for ASCII.
- if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
- // ASCII, and sr is upper case. tr must be lower case.
- if tr == sr+'a'-'A' {
+ if tr < utf8.RuneSelf {
+ // ASCII only, sr/tr must be upper/lower case
+ if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
continue
}
return false
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go
index d8fcb62a87..876f06c674 100644
--- a/src/strings/strings_test.go
+++ b/src/strings/strings_test.go
@@ -1379,6 +1379,8 @@ var EqualFoldTests = []struct {
{"abcdefghijK", "abcdefghij\u212A", true},
{"abcdefghijkz", "abcdefghij\u212Ay", false},
{"abcdefghijKz", "abcdefghij\u212Ay", false},
+ {"1", "2", false},
+ {"utf-8", "US-ASCII", false},
}
func TestEqualFold(t *testing.T) {
@@ -1392,6 +1394,16 @@ func TestEqualFold(t *testing.T) {
}
}
+func BenchmarkEqualFold(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, tt := range EqualFoldTests {
+ if out := EqualFold(tt.s, tt.t); out != tt.out {
+ b.Fatal("wrong result")
+ }
+ }
+ }
+}
+
var CountTests = []struct {
s, sep string
num int