| 1 | package utilsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gofiber/utils/v2/internal/unsafeconvscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/" |
| 5 | "github.com/gofiber/utils/v2/simdscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/" |
| 6 | "github.com/gofiber/utils/v2/swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/" |
| 7 | ) |
| 8 | |
| 9 | // quoteLanes and backslashLanes broadcast '"' and '\\' to every lane. |
| 10 | const ( |
| 11 | quoteLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/quoteLanes. = uint64('"') * swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Onesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Ones. |
| 12 | backslashLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/backslashLanes. = uint64('\\') * swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Onesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/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 IsASCIIscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/IsASCII().[S byteSeqscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/byteSeq#](s S) bool { |
| 37 | n := len(s) |
| 38 | if n >= simdscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/.MinLenscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/MinLen. && simdscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/.Acceleratedscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/Accelerated().() { |
| 39 | return simdscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/.IsASCIIscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/simd`/IsASCII().(unsafeconvscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/.Bytesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/Bytes().(s)) |
| 40 | } |
| 41 | if n >= 8 && n < 16 { |
| 42 | return (swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, 0)|swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, n-8))&swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.HighBitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/HighBits. == 0 |
| 43 | } |
| 44 | i := 0 |
| 45 | for ; i+32 <= n; i += 32 { |
| 46 | w := s[i : i+32] |
| 47 | acc := swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 0) | swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 8) | swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 16) | swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 24) |
| 48 | if acc&swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.HighBitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/HighBits. != 0 { |
| 49 | return false |
| 50 | } |
| 51 | } |
| 52 | for ; i+16 <= n; i += 16 { |
| 53 | w := s[i : i+16] |
| 54 | if (swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 0)|swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 8))&swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.HighBitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/HighBits. != 0 { |
| 55 | return false |
| 56 | } |
| 57 | } |
| 58 | for ; i+8 <= n; i += 8 { |
| 59 | if swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, i)&swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.HighBitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/HighBits. != 0 { |
| 60 | return false |
| 61 | } |
| 62 | } |
| 63 | if i == n { |
| 64 | return true |
| 65 | } |
| 66 | if n >= 8 { |
| 67 | return swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, n-8)&swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.HighBitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/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 IndexNonQuotablescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/IndexNonQuotable().[S byteSeqscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/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 := nonQuotableMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/nonQuotableMask().(swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 0)) |
| 95 | m1 := nonQuotableMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/nonQuotableMask().(swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(w, 8)) |
| 96 | if m0|m1 != 0 { |
| 97 | if m0 != 0 { |
| 98 | return i + swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/FirstLane().(m0) |
| 99 | } |
| 100 | return i + 8 + swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/FirstLane().(m1) |
| 101 | } |
| 102 | } |
| 103 | for ; i+8 <= n; i += 8 { |
| 104 | if m := nonQuotableMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/nonQuotableMask().(swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, i)); m != 0 { |
| 105 | return i + swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/FirstLane().(m) |
| 106 | } |
| 107 | } |
| 108 | if i == n { |
| 109 | return -1 |
| 110 | } |
| 111 | if n >= 8 { |
| 112 | if m := nonQuotableMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/nonQuotableMask().(swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, n-8)); m != 0 { |
| 113 | return n - 8 + swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/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 nonQuotableMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/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 & swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.LowSevenscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/LowSeven.) + swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Onesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Ones.) & swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.LowSevenscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/LowSeven. |
| 136 | ctl := ^(t + (0x80-0x21)*swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.Onesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Ones.) &^ w & swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.HighBitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/HighBits. &^ swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.MatchByteMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/MatchByteMask().(w, '\t') |
| 137 | return ctl | swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.ZeroLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/ZeroLanes().(w^quoteLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/quoteLanes.) | swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/.ZeroLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/ZeroLanes().(w^backslashLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/backslashLanes.) |
| 138 | } |