| 1 |
//go:build purego || !amd64 |
| 2 |
// +build purego !amd64 |
| 3 |
|
| 4 |
package ascii |
| 5 |
|
| 6 |
// EqualFoldString is a version of strings.EqualFold designed to work on ASCII |
| 7 |
// input instead of UTF-8. |
| 8 |
// |
| 9 |
// When the program has guarantees that the input is composed of ASCII |
| 10 |
// characters only, it allows for greater optimizations. |
| 11 |
func EqualFoldString(a, b string) bool { |
| 12 |
if len(a) != len(b) { |
| 13 |
return false |
| 14 |
} |
| 15 |
|
| 16 |
var cmp byte |
| 17 |
|
| 18 |
for len(a) >= 8 { |
| 19 |
cmp |= lowerCase[a[0]] ^ lowerCase[b[0]] |
| 20 |
cmp |= lowerCase[a[1]] ^ lowerCase[b[1]] |
| 21 |
cmp |= lowerCase[a[2]] ^ lowerCase[b[2]] |
| 22 |
cmp |= lowerCase[a[3]] ^ lowerCase[b[3]] |
| 23 |
cmp |= lowerCase[a[4]] ^ lowerCase[b[4]] |
| 24 |
cmp |= lowerCase[a[5]] ^ lowerCase[b[5]] |
| 25 |
cmp |= lowerCase[a[6]] ^ lowerCase[b[6]] |
| 26 |
cmp |= lowerCase[a[7]] ^ lowerCase[b[7]] |
| 27 |
|
| 28 |
if cmp != 0 { |
| 29 |
return false |
| 30 |
} |
| 31 |
|
| 32 |
a = a[8:] |
| 33 |
b = b[8:] |
| 34 |
} |
| 35 |
|
| 36 |
switch len(a) { |
| 37 |
case 7: |
| 38 |
cmp |= lowerCase[a[6]] ^ lowerCase[b[6]] |
| 39 |
fallthrough |
| 40 |
case 6: |
| 41 |
cmp |= lowerCase[a[5]] ^ lowerCase[b[5]] |
| 42 |
fallthrough |
| 43 |
case 5: |
| 44 |
cmp |= lowerCase[a[4]] ^ lowerCase[b[4]] |
| 45 |
fallthrough |
| 46 |
case 4: |
| 47 |
cmp |= lowerCase[a[3]] ^ lowerCase[b[3]] |
| 48 |
fallthrough |
| 49 |
case 3: |
| 50 |
cmp |= lowerCase[a[2]] ^ lowerCase[b[2]] |
| 51 |
fallthrough |
| 52 |
case 2: |
| 53 |
cmp |= lowerCase[a[1]] ^ lowerCase[b[1]] |
| 54 |
fallthrough |
| 55 |
case 1: |
| 56 |
cmp |= lowerCase[a[0]] ^ lowerCase[b[0]] |
| 57 |
} |
| 58 |
|
| 59 |
return cmp == 0 |
| 60 |
} |