1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 caseMask = ^byte(0x20) // Mask to ignore case in ASCII.
15 smallLongEss = '\u017f'
18 // foldFunc returns one of four different case folding equivalence
19 // functions, from most general (and slow) to fastest:
21 // 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8
22 // 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')
23 // 3) asciiEqualFold, no special, but includes non-letters (including _)
24 // 4) simpleLetterEqualFold, no specials, no non-letters.
26 // The letters S and K are special because they map to 3 runes, not just 2:
27 // * S maps to s and to U+017F 'ſ' Latin small letter long s
28 // * k maps to K and to U+212A 'K' Kelvin sign
29 // See https://play.golang.org/p/tTxjOc0OGo
31 // The returned function is specialized for matching against s and
32 // should only be given s. It's not curried for performance reasons.
33 func foldFunc(s []byte) func(s, t []byte) bool {
35 special := false // special letter
37 if b >= utf8.RuneSelf {
38 return bytes.EqualFold
41 if upper < 'A' || upper > 'Z' {
43 } else if upper == 'K' || upper == 'S' {
44 // See above for why these letters are special.
54 return simpleLetterEqualFold
57 // equalFoldRight is a specialization of bytes.EqualFold when s is
58 // known to be all ASCII (including punctuation), but contains an 's',
59 // 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.
60 // See comments on foldFunc.
61 func equalFoldRight(s, t []byte) bool {
62 for _, sb := range s {
67 if tb < utf8.RuneSelf {
69 sbUpper := sb & caseMask
70 if 'A' <= sbUpper && sbUpper <= 'Z' {
71 if sbUpper != tb&caseMask {
81 // sb is ASCII and t is not. t must be either kelvin
82 // sign or long s; sb must be s, S, k, or K.
83 tr, size := utf8.DecodeRune(t)
86 if tr != smallLongEss {
105 // asciiEqualFold is a specialization of bytes.EqualFold for use when
106 // s is all ASCII (but may contain non-letters) and contains no
107 // special-folding letters.
108 // See comments on foldFunc.
109 func asciiEqualFold(s, t []byte) bool {
110 if len(s) != len(t) {
113 for i, sb := range s {
118 if ('a' <= sb && sb <= 'z') || ('A' <= sb && sb <= 'Z') {
119 if sb&caseMask != tb&caseMask {
129 // simpleLetterEqualFold is a specialization of bytes.EqualFold for
130 // use when s is all ASCII letters (no underscores, etc) and also
131 // doesn't contain 'k', 'K', 's', or 'S'.
132 // See comments on foldFunc.
133 func simpleLetterEqualFold(s, t []byte) bool {
134 if len(s) != len(t) {
137 for i, b := range s {
138 if b&caseMask != t[i]&caseMask {