| 1 | package swar_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/gofiber/utils/v2/swar" |
| 7 | ) |
| 8 | |
| 9 | // ExampleZeroLanes shows the canonical first-match scan: broadcast the |
| 10 | // needle once, scan full words, and finish 8+ byte inputs with one |
| 11 | // overlapping word at len(s)-8. The overlap is safe because lanes before |
| 12 | // the loop's exit index are known non-matching, so the first set lane |
| 13 | // always falls into the new bytes. |
| 14 | func ExampleZeroLanes() { |
| 15 | indexByte := func(s string, c byte) int { |
| 16 | n := len(s) |
| 17 | if n < swar.WordLen { |
| 18 | for i := range n { |
| 19 | if s[i] == c { |
| 20 | return i |
| 21 | } |
| 22 | } |
| 23 | return -1 |
| 24 | } |
| 25 | needle := swar.Broadcast(c) |
| 26 | i := 0 |
| 27 | for ; i+swar.WordLen <= n; i += swar.WordLen { |
| 28 | if m := swar.ZeroLanes(swar.Load8(s, i) ^ needle); m != 0 { |
| 29 | return i + swar.FirstLane(m) |
| 30 | } |
| 31 | } |
| 32 | if i == n { |
| 33 | return -1 |
| 34 | } |
| 35 | if m := swar.ZeroLanes(swar.Load8(s, n-swar.WordLen) ^ needle); m != 0 { |
| 36 | return n - swar.WordLen + swar.FirstLane(m) |
| 37 | } |
| 38 | return -1 |
| 39 | } |
| 40 | |
| 41 | fmt.Println(indexByte("cache-control: no-store", ':')) //nolint:errcheck // example output |
| 42 | fmt.Println(indexByte("etag", ':')) //nolint:errcheck // example output |
| 43 | // Output: |
| 44 | // 13 |
| 45 | // -1 |
| 46 | } |
| 47 | |
| 48 | // ExampleZeroLanes_blockScan shows the multi-word variant of the |
| 49 | // first-match scan: compute each word's mask independently, test their OR |
| 50 | // once per block, and resolve the lower word first. Combining is safe for |
| 51 | // first-match scans because a ZeroLanes mask is zero iff its word has no |
| 52 | // match and its lowest set lane is exact — so when only the second word |
| 53 | // matched, its first set lane is still the true first match. Exact masks |
| 54 | // (MatchByteMask, MatchRangeMask) may be combined the same way, block width |
| 55 | // is free to grow (four words per branch is common for long inputs), and |
| 56 | // the resolve ladder extends one `if` per extra word. |
| 57 | func ExampleZeroLanes_blockScan() { |
| 58 | indexByte := func(s string, c byte) int { |
| 59 | needle := swar.Broadcast(c) |
| 60 | i := 0 |
| 61 | for ; i+16 <= len(s); i += 16 { |
| 62 | m0 := swar.ZeroLanes(swar.Load8(s, i) ^ needle) |
| 63 | m1 := swar.ZeroLanes(swar.Load8(s, i+8) ^ needle) |
| 64 | if m0|m1 != 0 { |
| 65 | if m0 != 0 { |
| 66 | return i + swar.FirstLane(m0) |
| 67 | } |
| 68 | return i + 8 + swar.FirstLane(m1) |
| 69 | } |
| 70 | } |
| 71 | for ; i+8 <= len(s); i += 8 { |
| 72 | if m := swar.ZeroLanes(swar.Load8(s, i) ^ needle); m != 0 { |
| 73 | return i + swar.FirstLane(m) |
| 74 | } |
| 75 | } |
| 76 | // A scalar tail keeps the example focused on block combining; see |
| 77 | // ExampleZeroLanes for the overlapping-word finish. |
| 78 | for ; i < len(s); i++ { |
| 79 | if s[i] == c { |
| 80 | return i |
| 81 | } |
| 82 | } |
| 83 | return -1 |
| 84 | } |
| 85 | |
| 86 | fmt.Println(indexByte("cache-control: public, max-age=604800, immutable", '=')) //nolint:errcheck // example output |
| 87 | fmt.Println(indexByte("cache-control: public", '=')) //nolint:errcheck // example output |
| 88 | // Output: |
| 89 | // 30 |
| 90 | // -1 |
| 91 | } |
| 92 | |
| 93 | // ExampleMatchRangeMask classifies one word of input: which lanes hold an |
| 94 | // ASCII digit, and where the first and last digit sit. |
| 95 | func ExampleMatchRangeMask() { |
| 96 | w := swar.Load8("Fiber123", 0) |
| 97 | digits := swar.MatchRangeMask(w, '0', '9') |
| 98 | fmt.Println(swar.FirstLane(digits), swar.LastLane(digits)) //nolint:errcheck // example output |
| 99 | // Output: 5 7 |
| 100 | } |
| 101 | |
| 102 | // ExampleLoad8 shows the little-endian lane order: the byte at the lowest |
| 103 | // index becomes lane 0, the least significant byte of the word. |
| 104 | func ExampleLoad8() { |
| 105 | w := swar.Load8([]byte("ABCDEFGH"), 0) |
| 106 | fmt.Printf("lane0=%c lane7=%c\n", byte(w), byte(w>>56)) //nolint:errcheck // example output |
| 107 | // Output: lane0=A lane7=H |
| 108 | } |