| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/gofiber/utils/v2/internal/unsafeconv" |
| 5 |
"github.com/gofiber/utils/v2/simd" |
| 6 |
"github.com/gofiber/utils/v2/swar" |
| 7 |
) |
| 8 |
|
| 9 |
// quoteLanes, backslashLanes, and tabLanes broadcast '"', '\\', and HTAB to every lane. |
| 10 |
const ( |
| 11 |
quoteLanes = uint64('"') * swar.Ones |
| 12 |
backslashLanes = uint64('\\') * swar.Ones |
| 13 |
tabLanes = uint64('\t') * swar.Ones |
| 14 |
) |
| 15 |
|
| 16 |
// The dispatch prologues below gate on simd.MinLen — the measured length |
| 17 |
// from which the AVX2 kernels beat these SWAR word loops — so the crossover |
| 18 |
// has a single source of truth in the simd package. The simd.Accelerated() |
| 19 |
// gate is a constant false off amd64, so the accelerated branches compile |
| 20 |
// away entirely there. |
| 21 |
|
| 22 |
// IsASCII reports whether s contains only ASCII bytes (no byte >= 0x80). |
| 23 |
// It ORs four words per iteration where possible (no index is needed, so |
| 24 |
// detection can be deferred to one test per 32 bytes), then two, then |
| 25 |
// word-wise; inputs of 8+ bytes finish with one overlapping word at n-8, |
| 26 |
// shorter ones byte-wise. |
| 27 |
// On amd64 CPUs with AVX2, inputs of 32+ bytes dispatch to package simd |
| 28 |
// instead. |
| 29 |
// |
| 30 |
// The unrolled loops pin their group of words with a reslice and load at |
| 31 |
// constant offsets inside it. swar.Load8's own reslice is bounds-checked |
| 32 |
// against the backing array, which the length-based loop condition does |
| 33 |
// not imply, so a variable index costs a compare and a branch per load; |
| 34 |
// pinning the window pays that once per group instead. This mirrors |
| 35 |
// simd.isASCIIGeneric — the same scan, kept in step so a retune of either |
| 36 |
// is a retune of both. |
| 37 |
func IsASCII[S byteSeq](s S) bool { |
| 38 |
n := len(s) |
| 39 |
if n >= simd.MinLen && simd.Accelerated() { |
| 40 |
return simd.IsASCII(unsafeconv.Bytes(s)) |
| 41 |
} |
| 42 |
if n >= 8 && n < 16 { |
| 43 |
return (swar.Load8(s, 0)|swar.Load8(s, n-8))&swar.HighBits == 0 |
| 44 |
} |
| 45 |
i := 0 |
| 46 |
for ; i+32 <= n; i += 32 { |
| 47 |
w := s[i : i+32] |
| 48 |
acc := swar.Load8(w, 0) | swar.Load8(w, 8) | swar.Load8(w, 16) | swar.Load8(w, 24) |
| 49 |
if acc&swar.HighBits != 0 { |
| 50 |
return false |
| 51 |
} |
| 52 |
} |
| 53 |
for ; i+16 <= n; i += 16 { |
| 54 |
w := s[i : i+16] |
| 55 |
if (swar.Load8(w, 0)|swar.Load8(w, 8))&swar.HighBits != 0 { |
| 56 |
return false |
| 57 |
} |
| 58 |
} |
| 59 |
for ; i+8 <= n; i += 8 { |
| 60 |
if swar.Load8(s, i)&swar.HighBits != 0 { |
| 61 |
return false |
| 62 |
} |
| 63 |
} |
| 64 |
if i == n { |
| 65 |
return true |
| 66 |
} |
| 67 |
if n >= 8 { |
| 68 |
return swar.Load8(s, n-8)&swar.HighBits == 0 |
| 69 |
} |
| 70 |
for ; i < n; i++ { |
| 71 |
if s[i] >= 0x80 { |
| 72 |
return false |
| 73 |
} |
| 74 |
} |
| 75 |
return true |
| 76 |
} |
| 77 |
|
| 78 |
// IndexNonQuotable returns the index of the first byte of s that cannot |
| 79 |
// appear verbatim inside an RFC 9110 quoted-string — that is, a byte |
| 80 |
// matching c == '\\' || c == '"' || (c < 0x20 && c != '\t') || c == 0x7f — |
| 81 |
// or -1 if every byte is quotable. qdtext is HTAB / SP / %x21 / %x23-5B / |
| 82 |
// %x5D-7E / obs-text, so HTAB and bytes >= 0x80 are quotable. Returning the |
| 83 |
// index (rather than a bool) lets callers copy the clean prefix and start |
| 84 |
// escaping exactly at the offending byte. Inputs of 8+ bytes finish with |
| 85 |
// one overlapping word at n-8, shorter ones byte-wise. |
| 86 |
func IndexNonQuotable[S byteSeq](s S) int { |
| 87 |
n := len(s) |
| 88 |
i := 0 |
| 89 |
// Two words per branch: the masks compute independently, and if only the |
| 90 |
// second word matched, its first set lane is still exact. |
| 91 |
for ; i+16 <= n; i += 16 { |
| 92 |
// Pinned window, as in IsASCII above: the two loads land at |
| 93 |
// constant offsets and skip their bounds checks. |
| 94 |
w := s[i : i+16] |
| 95 |
m0 := nonQuotableMask(swar.Load8(w, 0)) |
| 96 |
m1 := nonQuotableMask(swar.Load8(w, 8)) |
| 97 |
if m0|m1 != 0 { |
| 98 |
if m0 != 0 { |
| 99 |
return i + swar.FirstLane(m0) |
| 100 |
} |
| 101 |
return i + 8 + swar.FirstLane(m1) |
| 102 |
} |
| 103 |
} |
| 104 |
for ; i+8 <= n; i += 8 { |
| 105 |
if m := nonQuotableMask(swar.Load8(s, i)); m != 0 { |
| 106 |
return i + swar.FirstLane(m) |
| 107 |
} |
| 108 |
} |
| 109 |
if i == n { |
| 110 |
return -1 |
| 111 |
} |
| 112 |
if n >= 8 { |
| 113 |
if m := nonQuotableMask(swar.Load8(s, n-8)); m != 0 { |
| 114 |
return n - 8 + swar.FirstLane(m) |
| 115 |
} |
| 116 |
return -1 |
| 117 |
} |
| 118 |
for ; i < n; i++ { |
| 119 |
if c := s[i]; c == '\\' || c == '"' || (c < 0x20 && c != '\t') || c == 0x7f { |
| 120 |
return i |
| 121 |
} |
| 122 |
} |
| 123 |
return -1 |
| 124 |
} |
| 125 |
|
| 126 |
// nonQuotableMask marks the lanes of w holding bytes that need RFC 9110 |
| 127 |
// quoted-string escaping: controls other than HTAB (quotable qdtext), DEL, |
| 128 |
// '"', and '\\'. Like swar.ZeroLanes, the result is exact in and below the |
| 129 |
// first marked lane, which is all the first-match scan above consumes. |
| 130 |
func nonQuotableMask(w uint64) uint64 { |
| 131 |
return controlLanes(w)&otherLanes(w, tabLanes) | swar.ZeroLanes(w^quoteLanes) | swar.ZeroLanes(w^backslashLanes) |
| 132 |
} |