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