| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
func cutSamples() []string { |
| 12 |
return []string{ |
| 13 |
"", |
| 14 |
";", |
| 15 |
";;", |
| 16 |
"a", |
| 17 |
"a;", |
| 18 |
";a", |
| 19 |
"a;b", |
| 20 |
"a;b;c", |
| 21 |
"text/html; charset=utf-8", |
| 22 |
"host:8080", |
| 23 |
"[::1]:8080", |
| 24 |
"no separator here", |
| 25 |
"trailing;;", |
| 26 |
"\x00;\xff", |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func Test_CutByte(t *testing.T) { |
| 31 |
t.Parallel() |
| 32 |
for _, s := range cutSamples() { |
| 33 |
for _, sep := range []byte{';', ':', 'a', '\x00'} { |
| 34 |
wantBefore, wantAfter, wantFound := strings.Cut(s, string(sep)) |
| 35 |
before, after, found := CutByte(s, sep) |
| 36 |
require.Equal(t, wantFound, found, "CutByte(%q, %q)", s, sep) |
| 37 |
require.Equal(t, wantBefore, before, "CutByte(%q, %q)", s, sep) |
| 38 |
require.Equal(t, wantAfter, after, "CutByte(%q, %q)", s, sep) |
| 39 |
|
| 40 |
bb, ba, bf := CutByte([]byte(s), sep) |
| 41 |
wb, wa, wf := bytes.Cut([]byte(s), []byte{sep}) |
| 42 |
require.Equal(t, wf, bf, "CutByte(%q, %q) bytes", s, sep) |
| 43 |
require.Equal(t, wb, bb, "CutByte(%q, %q) bytes", s, sep) |
| 44 |
require.Equal(t, wa, ba, "CutByte(%q, %q) bytes", s, sep) |
| 45 |
if !found { |
| 46 |
// Like bytes.Cut, the after part of a miss is nil. |
| 47 |
require.Nil(t, ba) |
| 48 |
} |
| 49 |
|
| 50 |
// LastCutByte splits at the last separator. |
| 51 |
lb, la, lf := LastCutByte(s, sep) |
| 52 |
if i := strings.LastIndexByte(s, sep); i >= 0 { |
| 53 |
require.True(t, lf) |
| 54 |
require.Equal(t, s[:i], lb) |
| 55 |
require.Equal(t, s[i+1:], la) |
| 56 |
} else { |
| 57 |
require.False(t, lf) |
| 58 |
require.Equal(t, s, lb) |
| 59 |
require.Empty(t, la) |
| 60 |
} |
| 61 |
lbb, lba, lbf := LastCutByte([]byte(s), sep) |
| 62 |
require.Equal(t, lf, lbf) |
| 63 |
require.Equal(t, lb, string(lbb)) |
| 64 |
require.Equal(t, la, string(lba)) |
| 65 |
if !lf { |
| 66 |
require.Nil(t, lba) |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// The results alias the input: no copies are made. |
| 72 |
in := []byte("key=value") |
| 73 |
k, v, ok := CutByte(in, '=') |
| 74 |
require.True(t, ok) |
| 75 |
require.Same(t, &in[0], &k[0]) |
| 76 |
require.Same(t, &in[4], &v[0]) |
| 77 |
} |
| 78 |
|
| 79 |
func Benchmark_CutByte(b *testing.B) { |
| 80 |
input := "text/html; charset=utf-8" |
| 81 |
inputBytes := []byte(input) |
| 82 |
var before, after string |
| 83 |
var found bool |
| 84 |
b.Run("fiber", func(b *testing.B) { |
| 85 |
b.ReportAllocs() |
| 86 |
for b.Loop() { |
| 87 |
before, after, found = CutByte(input, ';') |
| 88 |
} |
| 89 |
require.True(b, found) |
| 90 |
}) |
| 91 |
b.Run("default", func(b *testing.B) { |
| 92 |
b.ReportAllocs() |
| 93 |
for b.Loop() { |
| 94 |
before, after, found = strings.Cut(input, ";") |
| 95 |
} |
| 96 |
require.True(b, found) |
| 97 |
}) |
| 98 |
_, _ = before, after |
| 99 |
var bb, ba []byte |
| 100 |
b.Run("fiber-bytes", func(b *testing.B) { |
| 101 |
b.ReportAllocs() |
| 102 |
for b.Loop() { |
| 103 |
bb, ba, found = CutByte(inputBytes, ';') |
| 104 |
} |
| 105 |
require.True(b, found) |
| 106 |
}) |
| 107 |
b.Run("default-bytes", func(b *testing.B) { |
| 108 |
b.ReportAllocs() |
| 109 |
for b.Loop() { |
| 110 |
bb, ba, found = bytes.Cut(inputBytes, []byte{';'}) |
| 111 |
} |
| 112 |
require.True(b, found) |
| 113 |
}) |
| 114 |
_, _ = bb, ba |
| 115 |
} |
| 116 |
|
| 117 |
func Benchmark_LastCutByte(b *testing.B) { |
| 118 |
input := "[2001:db8::1]:8080" |
| 119 |
var before, after string |
| 120 |
var found bool |
| 121 |
b.Run("fiber", func(b *testing.B) { |
| 122 |
b.ReportAllocs() |
| 123 |
for b.Loop() { |
| 124 |
before, after, found = LastCutByte(input, ':') |
| 125 |
} |
| 126 |
require.True(b, found) |
| 127 |
}) |
| 128 |
b.Run("default", func(b *testing.B) { |
| 129 |
b.ReportAllocs() |
| 130 |
for b.Loop() { |
| 131 |
if i := strings.LastIndexByte(input, ':'); i >= 0 { |
| 132 |
before, after, found = input[:i], input[i+1:], true |
| 133 |
} |
| 134 |
} |
| 135 |
require.True(b, found) |
| 136 |
}) |
| 137 |
_, _ = before, after |
| 138 |
} |