Skip to content

Commit f33fd2c

Browse files
committed
🔥 get changes from v3-beta, update README
1 parent e1b70f3 commit f33fd2c

21 files changed

+319
-701
lines changed

README.md

+76-85
Large diffs are not rendered by default.

assertions.go

-67
This file was deleted.

assertions_test.go

-13
This file was deleted.

bytes.go

+2-50
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,18 @@
44

55
package utils
66

7-
// ToLowerBytes converts ascii slice to lower-case
7+
// ToLowerBytes converts ascii slice to lower-case in-place.
88
func ToLowerBytes(b []byte) []byte {
99
for i := 0; i < len(b); i++ {
1010
b[i] = toLowerTable[b[i]]
1111
}
1212
return b
1313
}
1414

15-
// ToUpperBytes converts ascii slice to upper-case
15+
// ToUpperBytes converts ascii slice to upper-case in-place.
1616
func ToUpperBytes(b []byte) []byte {
1717
for i := 0; i < len(b); i++ {
1818
b[i] = toUpperTable[b[i]]
1919
}
2020
return b
2121
}
22-
23-
// TrimRightBytes is the equivalent of bytes.TrimRight
24-
func TrimRightBytes(b []byte, cutset byte) []byte {
25-
lenStr := len(b)
26-
for lenStr > 0 && b[lenStr-1] == cutset {
27-
lenStr--
28-
}
29-
return b[:lenStr]
30-
}
31-
32-
// TrimLeftBytes is the equivalent of bytes.TrimLeft
33-
func TrimLeftBytes(b []byte, cutset byte) []byte {
34-
lenStr, start := len(b), 0
35-
for start < lenStr && b[start] == cutset {
36-
start++
37-
}
38-
return b[start:]
39-
}
40-
41-
// TrimBytes is the equivalent of bytes.Trim
42-
func TrimBytes(b []byte, cutset byte) []byte {
43-
i, j := 0, len(b)-1
44-
for ; i <= j; i++ {
45-
if b[i] != cutset {
46-
break
47-
}
48-
}
49-
for ; i < j; j-- {
50-
if b[j] != cutset {
51-
break
52-
}
53-
}
54-
55-
return b[i : j+1]
56-
}
57-
58-
// EqualFoldBytes tests ascii slices for equality case-insensitively
59-
func EqualFoldBytes(b, s []byte) bool {
60-
if len(b) != len(s) {
61-
return false
62-
}
63-
for i := len(b) - 1; i >= 0; i-- {
64-
if toUpperTable[b[i]] != toUpperTable[s[i]] {
65-
return false
66-
}
67-
}
68-
return true
69-
}

bytes_test.go

+18-168
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ package utils
77
import (
88
"bytes"
99
"testing"
10+
11+
"github.com/stretchr/testify/require"
1012
)
1113

1214
func Test_ToLowerBytes(t *testing.T) {
1315
t.Parallel()
14-
res := ToLowerBytes([]byte("/MY/NAME/IS/:PARAM/*"))
15-
AssertEqual(t, true, bytes.Equal([]byte("/my/name/is/:param/*"), res))
16-
res = ToLowerBytes([]byte("/MY1/NAME/IS/:PARAM/*"))
17-
AssertEqual(t, true, bytes.Equal([]byte("/my1/name/is/:param/*"), res))
18-
res = ToLowerBytes([]byte("/MY2/NAME/IS/:PARAM/*"))
19-
AssertEqual(t, true, bytes.Equal([]byte("/my2/name/is/:param/*"), res))
20-
res = ToLowerBytes([]byte("/MY3/NAME/IS/:PARAM/*"))
21-
AssertEqual(t, true, bytes.Equal([]byte("/my3/name/is/:param/*"), res))
22-
res = ToLowerBytes([]byte("/MY4/NAME/IS/:PARAM/*"))
23-
AssertEqual(t, true, bytes.Equal([]byte("/my4/name/is/:param/*"), res))
16+
17+
require.Equal(t, []byte("/my/name/is/:param/*"), ToLowerBytes([]byte("/MY/NAME/IS/:PARAM/*")))
18+
require.Equal(t, []byte("/my1/name/is/:param/*"), ToLowerBytes([]byte("/MY1/NAME/IS/:PARAM/*")))
19+
require.Equal(t, []byte("/my2/name/is/:param/*"), ToLowerBytes([]byte("/MY2/NAME/IS/:PARAM/*")))
20+
require.Equal(t, []byte("/my3/name/is/:param/*"), ToLowerBytes([]byte("/MY3/NAME/IS/:PARAM/*")))
21+
require.Equal(t, []byte("/my4/name/is/:param/*"), ToLowerBytes([]byte("/MY4/NAME/IS/:PARAM/*")))
2422
}
2523

2624
func Benchmark_ToLowerBytes(b *testing.B) {
@@ -31,28 +29,24 @@ func Benchmark_ToLowerBytes(b *testing.B) {
3129
for n := 0; n < b.N; n++ {
3230
res = ToLowerBytes(path)
3331
}
34-
AssertEqual(b, bytes.Equal(want, res), true)
32+
require.Equal(b, bytes.Equal(want, res), true)
3533
})
3634
b.Run("default", func(b *testing.B) {
3735
for n := 0; n < b.N; n++ {
3836
res = bytes.ToLower(path)
3937
}
40-
AssertEqual(b, bytes.Equal(want, res), true)
38+
require.Equal(b, bytes.Equal(want, res), true)
4139
})
4240
}
4341

4442
func Test_ToUpperBytes(t *testing.T) {
4543
t.Parallel()
46-
res := ToUpperBytes([]byte("/my/name/is/:param/*"))
47-
AssertEqual(t, true, bytes.Equal([]byte("/MY/NAME/IS/:PARAM/*"), res))
48-
res = ToUpperBytes([]byte("/my1/name/is/:param/*"))
49-
AssertEqual(t, true, bytes.Equal([]byte("/MY1/NAME/IS/:PARAM/*"), res))
50-
res = ToUpperBytes([]byte("/my2/name/is/:param/*"))
51-
AssertEqual(t, true, bytes.Equal([]byte("/MY2/NAME/IS/:PARAM/*"), res))
52-
res = ToUpperBytes([]byte("/my3/name/is/:param/*"))
53-
AssertEqual(t, true, bytes.Equal([]byte("/MY3/NAME/IS/:PARAM/*"), res))
54-
res = ToUpperBytes([]byte("/my4/name/is/:param/*"))
55-
AssertEqual(t, true, bytes.Equal([]byte("/MY4/NAME/IS/:PARAM/*"), res))
44+
45+
require.Equal(t, []byte("/MY/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my/name/is/:param/*")))
46+
require.Equal(t, []byte("/MY1/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my1/name/is/:param/*")))
47+
require.Equal(t, []byte("/MY2/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my2/name/is/:param/*")))
48+
require.Equal(t, []byte("/MY3/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my3/name/is/:param/*")))
49+
require.Equal(t, []byte("/MY4/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my4/name/is/:param/*")))
5650
}
5751

5852
func Benchmark_ToUpperBytes(b *testing.B) {
@@ -63,156 +57,12 @@ func Benchmark_ToUpperBytes(b *testing.B) {
6357
for n := 0; n < b.N; n++ {
6458
res = ToUpperBytes(path)
6559
}
66-
AssertEqual(b, bytes.Equal(want, res), true)
60+
require.Equal(b, want, res)
6761
})
6862
b.Run("default", func(b *testing.B) {
6963
for n := 0; n < b.N; n++ {
7064
res = bytes.ToUpper(path)
7165
}
72-
AssertEqual(b, bytes.Equal(want, res), true)
73-
})
74-
}
75-
76-
func Test_TrimRightBytes(t *testing.T) {
77-
t.Parallel()
78-
res := TrimRightBytes([]byte("/test//////"), '/')
79-
AssertEqual(t, []byte("/test"), res)
80-
81-
res = TrimRightBytes([]byte("/test"), '/')
82-
AssertEqual(t, []byte("/test"), res)
83-
84-
res = TrimRightBytes([]byte(" "), ' ')
85-
AssertEqual(t, 0, len(res))
86-
87-
res = TrimRightBytes([]byte(" "), ' ')
88-
AssertEqual(t, 0, len(res))
89-
90-
res = TrimRightBytes([]byte(""), ' ')
91-
AssertEqual(t, 0, len(res))
92-
}
93-
94-
func Benchmark_TrimRightBytes(b *testing.B) {
95-
var res []byte
96-
97-
b.Run("fiber", func(b *testing.B) {
98-
for n := 0; n < b.N; n++ {
99-
res = TrimRightBytes([]byte("foobar "), ' ')
100-
}
101-
AssertEqual(b, []byte("foobar"), res)
102-
})
103-
b.Run("default", func(b *testing.B) {
104-
for n := 0; n < b.N; n++ {
105-
res = bytes.TrimRight([]byte("foobar "), " ")
106-
}
107-
AssertEqual(b, []byte("foobar"), res)
108-
})
109-
}
110-
111-
func Test_TrimLeftBytes(t *testing.T) {
112-
t.Parallel()
113-
res := TrimLeftBytes([]byte("////test/"), '/')
114-
AssertEqual(t, []byte("test/"), res)
115-
116-
res = TrimLeftBytes([]byte("test/"), '/')
117-
AssertEqual(t, []byte("test/"), res)
118-
119-
res = TrimLeftBytes([]byte(" "), ' ')
120-
AssertEqual(t, 0, len(res))
121-
122-
res = TrimLeftBytes([]byte(" "), ' ')
123-
AssertEqual(t, 0, len(res))
124-
125-
res = TrimLeftBytes([]byte(""), ' ')
126-
AssertEqual(t, 0, len(res))
127-
}
128-
129-
func Benchmark_TrimLeftBytes(b *testing.B) {
130-
var res []byte
131-
132-
b.Run("fiber", func(b *testing.B) {
133-
for n := 0; n < b.N; n++ {
134-
res = TrimLeftBytes([]byte(" foobar"), ' ')
135-
}
136-
AssertEqual(b, []byte("foobar"), res)
66+
require.Equal(b, want, res)
13767
})
138-
b.Run("default", func(b *testing.B) {
139-
for n := 0; n < b.N; n++ {
140-
res = bytes.TrimLeft([]byte(" foobar"), " ")
141-
}
142-
AssertEqual(b, []byte("foobar"), res)
143-
})
144-
}
145-
146-
func Test_TrimBytes(t *testing.T) {
147-
t.Parallel()
148-
res := TrimBytes([]byte(" test "), ' ')
149-
AssertEqual(t, []byte("test"), res)
150-
151-
res = TrimBytes([]byte("test"), ' ')
152-
AssertEqual(t, []byte("test"), res)
153-
154-
res = TrimBytes([]byte(".test"), '.')
155-
AssertEqual(t, []byte("test"), res)
156-
157-
res = TrimBytes([]byte(" "), ' ')
158-
AssertEqual(t, 0, len(res))
159-
160-
res = TrimBytes([]byte(" "), ' ')
161-
AssertEqual(t, 0, len(res))
162-
163-
res = TrimBytes([]byte(""), ' ')
164-
AssertEqual(t, 0, len(res))
165-
}
166-
167-
func Benchmark_TrimBytes(b *testing.B) {
168-
var res []byte
169-
170-
b.Run("fiber", func(b *testing.B) {
171-
for n := 0; n < b.N; n++ {
172-
res = TrimBytes([]byte(" foobar "), ' ')
173-
}
174-
AssertEqual(b, []byte("foobar"), res)
175-
})
176-
b.Run("default", func(b *testing.B) {
177-
for n := 0; n < b.N; n++ {
178-
res = bytes.Trim([]byte(" foobar "), " ")
179-
}
180-
AssertEqual(b, []byte("foobar"), res)
181-
})
182-
}
183-
184-
func Benchmark_EqualFoldBytes(b *testing.B) {
185-
left := []byte(upperStr)
186-
right := []byte(lowerStr)
187-
var res bool
188-
b.Run("fiber", func(b *testing.B) {
189-
for n := 0; n < b.N; n++ {
190-
res = EqualFoldBytes(left, right)
191-
}
192-
AssertEqual(b, true, res)
193-
})
194-
b.Run("default", func(b *testing.B) {
195-
for n := 0; n < b.N; n++ {
196-
res = bytes.EqualFold(left, right)
197-
}
198-
AssertEqual(b, true, res)
199-
})
200-
}
201-
202-
func Test_EqualFoldBytes(t *testing.T) {
203-
t.Parallel()
204-
res := EqualFoldBytes([]byte("/MY/NAME/IS/:PARAM/*"), []byte("/my/name/is/:param/*"))
205-
AssertEqual(t, true, res)
206-
res = EqualFoldBytes([]byte("/MY1/NAME/IS/:PARAM/*"), []byte("/MY1/NAME/IS/:PARAM/*"))
207-
AssertEqual(t, true, res)
208-
res = EqualFoldBytes([]byte("/my2/name/is/:param/*"), []byte("/my2/name"))
209-
AssertEqual(t, false, res)
210-
res = EqualFoldBytes([]byte("/dddddd"), []byte("eeeeee"))
211-
AssertEqual(t, false, res)
212-
res = EqualFoldBytes([]byte("\na"), []byte("*A"))
213-
AssertEqual(t, false, res)
214-
res = EqualFoldBytes([]byte("/MY3/NAME/IS/:PARAM/*"), []byte("/my3/name/is/:param/*"))
215-
AssertEqual(t, true, res)
216-
res = EqualFoldBytes([]byte("/MY4/NAME/IS/:PARAM/*"), []byte("/my4/nAME/IS/:param/*"))
217-
AssertEqual(t, true, res)
21868
}

byteseq.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package utils
2+
3+
type byteSeq interface {
4+
~string | ~[]byte
5+
}
6+
7+
// EqualFold tests ascii strings or bytes for equality case-insensitively
8+
func EqualFold[S byteSeq](b, s S) bool {
9+
if len(b) != len(s) {
10+
return false
11+
}
12+
for i := len(b) - 1; i >= 0; i-- {
13+
if toUpperTable[b[i]] != toUpperTable[s[i]] {
14+
return false
15+
}
16+
}
17+
return true
18+
}

0 commit comments

Comments
 (0)