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