| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/gofiber/utils/v2/swar" |
| 5 |
) |
| 6 |
|
| 7 |
// noControlExemption is never a control lane, so exempting it is a no-op. |
| 8 |
const noControlExemption = 0x80 |
| 9 |
|
| 10 |
// IndexControl returns the index of the first ASCII control byte in s (below |
| 11 |
// 0x20 or DEL, the RFC 5234 CTL set), or -1. HTAB counts as a control byte; |
| 12 |
// bytes >= 0x80 never match, unlike unicode.IsControl's C1 range. |
| 13 |
func IndexControl[S byteSeq](s S) int { |
| 14 |
return scanControl(s, noControlExemption) |
| 15 |
} |
| 16 |
|
| 17 |
// IndexControlExceptTab is IndexControl with HTAB permitted: the bytes an |
| 18 |
// RFC 9110 field value may not contain. |
| 19 |
func IndexControlExceptTab[S byteSeq](s S) int { |
| 20 |
return scanControl(s, '\t') |
| 21 |
} |
| 22 |
|
| 23 |
// scanControl is the word scan behind both. Words holding no control byte, |
| 24 |
// the common case, skip the exemption test entirely. |
| 25 |
func scanControl[S byteSeq](s S, exempt byte) int { |
| 26 |
n := len(s) |
| 27 |
exemptLanes := swar.Broadcast(exempt) |
| 28 |
i := 0 |
| 29 |
for ; i+16 <= n; i += 16 { |
| 30 |
w := s[i : i+16] |
| 31 |
w0, w1 := swar.Load8(w, 0), swar.Load8(w, 8) |
| 32 |
m0, m1 := controlLanes(w0), controlLanes(w1) |
| 33 |
if m0|m1 != 0 { |
| 34 |
if m0 &= otherLanes(w0, exemptLanes); m0 != 0 { |
| 35 |
return i + swar.FirstLane(m0) |
| 36 |
} |
| 37 |
if m1 &= otherLanes(w1, exemptLanes); m1 != 0 { |
| 38 |
return i + 8 + swar.FirstLane(m1) |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
for ; i+8 <= n; i += 8 { |
| 43 |
w := swar.Load8(s, i) |
| 44 |
if m := controlLanes(w); m != 0 { |
| 45 |
if m &= otherLanes(w, exemptLanes); m != 0 { |
| 46 |
return i + swar.FirstLane(m) |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
if i == n { |
| 51 |
return -1 |
| 52 |
} |
| 53 |
if n >= 8 { |
| 54 |
w := swar.Load8(s, n-8) |
| 55 |
if m := controlLanes(w); m != 0 { |
| 56 |
if m &= otherLanes(w, exemptLanes); m != 0 { |
| 57 |
return n - 8 + swar.FirstLane(m) |
| 58 |
} |
| 59 |
} |
| 60 |
return -1 |
| 61 |
} |
| 62 |
for ; i < n; i++ { |
| 63 |
if c := s[i]; (c < 0x20 || c == 0x7f) && c != exempt { |
| 64 |
return i |
| 65 |
} |
| 66 |
} |
| 67 |
return -1 |
| 68 |
} |
| 69 |
|
| 70 |
// controlLanes flags the lanes of w below 0x20 or equal to DEL, exactly per |
| 71 |
// lane; the biased lanes stay below 0xE0, so no carries. |
| 72 |
func controlLanes(w uint64) uint64 { |
| 73 |
b := w & swar.LowSeven |
| 74 |
return (^(b + (0x80-0x20)*swar.Ones) | (b + swar.Ones)) &^ w & swar.HighBits |
| 75 |
} |
| 76 |
|
| 77 |
// otherLanes flags the lanes of w that differ from the byte broadcast in |
| 78 |
// lanes, exactly per lane: x is zero only in the equal lanes, and only there |
| 79 |
// is the high bit of ((x | HighBits) - Ones) | x clear (swar.MatchByteMask's test). |
| 80 |
func otherLanes(w, lanes uint64) uint64 { |
| 81 |
x := w ^ lanes |
| 82 |
return ((x | swar.HighBits) - swar.Ones) | x |
| 83 |
} |