| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/gofiber/utils/v2/internal/caseconv" |
| 5 |
"github.com/gofiber/utils/v2/swar" |
| 6 |
) |
| 7 |
|
| 8 |
type byteSeq interface { |
| 9 |
~string | ~[]byte |
| 10 |
} |
| 11 |
|
| 12 |
// load4 assembles s[i:i+4] into a little-endian uint32; the caller guarantees i+4 <= len(s). |
| 13 |
func load4[S byteSeq](s S, i int) uint32 { |
| 14 |
w := s[i : i+4] |
| 15 |
return uint32(w[0]) | uint32(w[1])<<8 | uint32(w[2])<<16 | uint32(w[3])<<24 |
| 16 |
} |
| 17 |
|
| 18 |
// EqualFold tests ascii strings or bytes for equality case-insensitively |
| 19 |
func EqualFold[S byteSeq](b, s S) bool { |
| 20 |
n := len(b) |
| 21 |
if n != len(s) { |
| 22 |
return false |
| 23 |
} |
| 24 |
|
| 25 |
// Compare 8 bytes per iteration; case-fold with SWAR only when the raw |
| 26 |
// words differ, so byte-identical input skips both folds entirely. |
| 27 |
i := 0 |
| 28 |
for ; i+8 <= n; i += 8 { |
| 29 |
x := swar.Load8(b, i) |
| 30 |
y := swar.Load8(s, i) |
| 31 |
if x != y && swar.ToUpperWord(x) != swar.ToUpperWord(y) { |
| 32 |
return false |
| 33 |
} |
| 34 |
} |
| 35 |
if i == n { |
| 36 |
return true |
| 37 |
} |
| 38 |
if n >= 8 { |
| 39 |
// Handle the tail with one overlapping word compare; re-checking |
| 40 |
// bytes that were already equal cannot change the outcome. |
| 41 |
x := swar.Load8(b, n-8) |
| 42 |
y := swar.Load8(s, n-8) |
| 43 |
return x == y || swar.ToUpperWord(x) == swar.ToUpperWord(y) |
| 44 |
} |
| 45 |
if n >= 4 { |
| 46 |
// 4..7 bytes: two overlapping 4-byte windows packed into one word |
| 47 |
// fold with a single ToUpperWord; checked after the word paths so |
| 48 |
// longer inputs pay nothing for it. |
| 49 |
x := uint64(load4(b, 0)) | uint64(load4(b, n-4))<<32 |
| 50 |
y := uint64(load4(s, 0)) | uint64(load4(s, n-4))<<32 |
| 51 |
return x == y || swar.ToUpperWord(x) == swar.ToUpperWord(y) |
| 52 |
} |
| 53 |
|
| 54 |
table := caseconv.ToUpperTable |
| 55 |
for ; i < n; i++ { |
| 56 |
if table[b[i]] != table[s[i]] { |
| 57 |
return false |
| 58 |
} |
| 59 |
} |
| 60 |
return true |
| 61 |
} |
| 62 |
|
| 63 |
// TrimLeft removes all leading occurrences of the byte cutset from s. |
| 64 |
// Unlike strings/bytes.TrimLeft, cutset is a single byte, not a set of characters. |
| 65 |
func TrimLeft[S byteSeq](s S, cutset byte) S { |
| 66 |
lenStr, start := len(s), 0 |
| 67 |
for start < lenStr && s[start] == cutset { |
| 68 |
start++ |
| 69 |
} |
| 70 |
return s[start:] |
| 71 |
} |
| 72 |
|
| 73 |
// Trim removes all leading and trailing occurrences of the byte cutset from s. |
| 74 |
// Unlike strings/bytes.Trim, cutset is a single byte, not a set of characters. |
| 75 |
func Trim[S byteSeq](s S, cutset byte) S { |
| 76 |
i, j := 0, len(s)-1 |
| 77 |
for ; i <= j; i++ { |
| 78 |
if s[i] != cutset { |
| 79 |
break |
| 80 |
} |
| 81 |
} |
| 82 |
for ; i < j; j-- { |
| 83 |
if s[j] != cutset { |
| 84 |
break |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return s[i : j+1] |
| 89 |
} |
| 90 |
|
| 91 |
// TrimRight removes all trailing occurrences of the byte cutset from s. |
| 92 |
// Unlike strings/bytes.TrimRight, cutset is a single byte, not a set of characters. |
| 93 |
func TrimRight[S byteSeq](s S, cutset byte) S { |
| 94 |
lenStr := len(s) |
| 95 |
for lenStr > 0 && s[lenStr-1] == cutset { |
| 96 |
lenStr-- |
| 97 |
} |
| 98 |
return s[:lenStr] |
| 99 |
} |
| 100 |
|
| 101 |
// TrimSpace removes leading and trailing whitespace from a string or byte slice. |
| 102 |
// This is an optimized version that's faster than strings/bytes.TrimSpace for ASCII strings. |
| 103 |
// It removes the following ASCII whitespace characters: space, tab, newline, carriage return, vertical tab, and form feed. |
| 104 |
func TrimSpace[S byteSeq](s S) S { |
| 105 |
n := len(s) |
| 106 |
if n == 0 { |
| 107 |
return s |
| 108 |
} |
| 109 |
|
| 110 |
i, j := 0, n-1 |
| 111 |
if !whitespaceTable[s[i]] && !whitespaceTable[s[j]] { |
| 112 |
return s |
| 113 |
} |
| 114 |
|
| 115 |
// Find first non-whitespace from start |
| 116 |
for ; i <= j && whitespaceTable[s[i]]; i++ { //nolint:revive // we want to check for multiple whitespace chars |
| 117 |
} |
| 118 |
|
| 119 |
// Find first non-whitespace from end |
| 120 |
for ; i < j && whitespaceTable[s[j]]; j-- { //nolint:revive // we want to check for multiple whitespace chars |
| 121 |
} |
| 122 |
|
| 123 |
return s[i : j+1] |
| 124 |
} |