Debian Codegraph @

byteseq.go

raw
1 package utils
2
3 import (
4 "math/bits"
5
6 "github.com/gofiber/utils/v2/internal/caseconv"
7 "github.com/gofiber/utils/v2/swar"
8 )
9
10 type byteSeq interface {
11 ~string | ~[]byte
12 }
13
14 // load4 assembles s[i:i+4] into a little-endian uint32; the caller guarantees i+4 <= len(s).
15 func load4[S byteSeq](s S, i int) uint32 {
16 w := s[i : i+4]
17 return uint32(w[0]) | uint32(w[1])<<8 | uint32(w[2])<<16 | uint32(w[3])<<24
18 }
19
20 // EqualFold tests ascii strings or bytes for equality case-insensitively
21 func EqualFold[S byteSeq](b, s S) bool {
22 n := len(b)
23 if n != len(s) {
24 return false
25 }
26
27 // Compare 8 bytes per iteration; case-fold with SWAR only when the raw
28 // words differ, so byte-identical input skips both folds entirely.
29 i := 0
30 for ; i+8 <= n; i += 8 {
31 x := swar.Load8(b, i)
32 y := swar.Load8(s, i)
33 if x != y && swar.ToUpperWord(x) != swar.ToUpperWord(y) {
34 return false
35 }
36 }
37 if i == n {
38 return true
39 }
40 if n >= 8 {
41 // Handle the tail with one overlapping word compare; re-checking
42 // bytes that were already equal cannot change the outcome.
43 x := swar.Load8(b, n-8)
44 y := swar.Load8(s, n-8)
45 return x == y || swar.ToUpperWord(x) == swar.ToUpperWord(y)
46 }
47 if n >= 4 {
48 // 4..7 bytes: two overlapping 4-byte windows packed into one word
49 // fold with a single ToUpperWord; checked after the word paths so
50 // longer inputs pay nothing for it.
51 x := uint64(load4(b, 0)) | uint64(load4(b, n-4))<<32
52 y := uint64(load4(s, 0)) | uint64(load4(s, n-4))<<32
53 return x == y || swar.ToUpperWord(x) == swar.ToUpperWord(y)
54 }
55
56 table := caseconv.ToUpperTable
57 for ; i < n; i++ {
58 if table[b[i]] != table[s[i]] {
59 return false
60 }
61 }
62 return true
63 }
64
65 // HashFold returns a 64-bit hash of s in which ASCII letters are folded to one
66 // case: strings that EqualFold reports equal hash equal. It is the hashing
67 // half of a case-insensitive lookup table whose other half is EqualFold, for
68 // keys such as header or field names, and reads s a word at a time without
69 // writing a folded copy of it anywhere. As in EqualFold, only 'A'..'Z' and
70 // 'a'..'z' fold; every other byte, including those >= 0x80, hashes as is.
71 //
72 // It is not a cryptographic hash, and it takes no seed, so it offers no
73 // protection against collisions planned in advance: use it to index keys of
74 // the program's own, such as the fields of a struct, and not to store keys an
75 // attacker picks. Its values may change between releases; do not persist them.
76 func HashFold[S byteSeq](s S) uint64 {
77 n := len(s)
78 if n < 8 {
79 // A key shorter than a word, as most are, packs into one: from four
80 // bytes up as two overlapping halves, below that as its first,
81 // middle and last byte, which cover one to three bytes between them.
82 // Keys of different lengths can pack alike, "a" and "aa" say, so the
83 // length goes into the multiplier, where no byte of the key reaches.
84 var w uint64
85 switch {
86 case n >= 4:
87 w = uint64(load4(s, 0)) | uint64(load4(s, n-4))<<32
88 case n > 0:
89 w = uint64(s[0]) | uint64(s[n>>1])<<8 | uint64(s[n-1])<<16
90 }
91 return hashFoldMix(swar.ToLowerWord(w)^hashFoldK0, hashFoldK1^uint64(n)<<56)
92 }
93 // Longer keys go as wyhash does: sixteen bytes per multiply, then the
94 // last sixteen, overlapping what came before when fewer are left, and a
95 // final mix that takes in the length.
96 h := hashFoldK2
97 for i := 0; n-i > 16; i += 16 {
98 h = hashFoldMix(swar.ToLowerWord(swar.Load8(s, i))^hashFoldK1, swar.ToLowerWord(swar.Load8(s, i+8))^h)
99 }
100 hi, lo := bits.Mul64(
101 swar.ToLowerWord(swar.Load8(s, max(n-16, 0)))^hashFoldK1,
102 swar.ToLowerWord(swar.Load8(s, n-8))^h,
103 )
104 return hashFoldMix(lo^hashFoldK0^uint64(n), hi^hashFoldK1)
105 }
106
107 // The constants of HashFold, wyhash's.
108 const (
109 hashFoldK0 uint64 = 0xa0761d6478bd642f
110 hashFoldK1 uint64 = 0xe7037ed1a0b428db
111 hashFoldK2 uint64 = 0x8ebc6af09c88c6e3
112 )
113
114 // hashFoldMix folds the 128-bit product of a and b into 64 bits, the mixing
115 // step of the wyhash family: a single widening multiply spreads every bit of
116 // a and b across the result.
117 func hashFoldMix(a, b uint64) uint64 {
118 hi, lo := bits.Mul64(a, b)
119 return hi ^ lo
120 }
121
122 // TrimLeft removes all leading occurrences of the byte cutset from s.
123 // Unlike strings/bytes.TrimLeft, cutset is a single byte, not a set of characters.
124 func TrimLeft[S byteSeq](s S, cutset byte) S {
125 lenStr, start := len(s), 0
126 for start < lenStr && s[start] == cutset {
127 start++
128 }
129 return s[start:]
130 }
131
132 // Trim removes all leading and trailing occurrences of the byte cutset from s.
133 // Unlike strings/bytes.Trim, cutset is a single byte, not a set of characters.
134 func Trim[S byteSeq](s S, cutset byte) S {
135 i, j := 0, len(s)-1
136 for ; i <= j; i++ {
137 if s[i] != cutset {
138 break
139 }
140 }
141 for ; i < j; j-- {
142 if s[j] != cutset {
143 break
144 }
145 }
146
147 return s[i : j+1]
148 }
149
150 // TrimRight removes all trailing occurrences of the byte cutset from s.
151 // Unlike strings/bytes.TrimRight, cutset is a single byte, not a set of characters.
152 func TrimRight[S byteSeq](s S, cutset byte) S {
153 lenStr := len(s)
154 for lenStr > 0 && s[lenStr-1] == cutset {
155 lenStr--
156 }
157 return s[:lenStr]
158 }
159
160 // TrimSpace removes leading and trailing whitespace from a string or byte slice.
161 // This is an optimized version that's faster than strings/bytes.TrimSpace for ASCII strings.
162 // It removes the following ASCII whitespace characters: space, tab, newline, carriage return, vertical tab, and form feed.
163 func TrimSpace[S byteSeq](s S) S {
164 n := len(s)
165 if n == 0 {
166 return s
167 }
168
169 i, j := 0, n-1
170 if !whitespaceTable[s[i]] && !whitespaceTable[s[j]] {
171 return s
172 }
173
174 // Find first non-whitespace from start
175 for ; i <= j && whitespaceTable[s[i]]; i++ { //nolint:revive // we want to check for multiple whitespace chars
176 }
177
178 // Find first non-whitespace from end
179 for ; i < j && whitespaceTable[s[j]]; j-- { //nolint:revive // we want to check for multiple whitespace chars
180 }
181
182 return s[i : j+1]
183 }