Skip to content

Commit cefa447

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 4df063c + 84e346c commit cefa447

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ Benchmark_ToUpperBytes/fiber-12 25859680 46.43 ns/op
2929
Benchmark_ToUpperBytes/default-12 10015346 117.2 ns/op 80 B/op 1 allocs/op
3030
Benchmark_ToUpperBytes/default-12 10185375 117.8 ns/op 80 B/op 1 allocs/op
3131

32+
Benchmark_TrimRight/fiber-12 522399795 2.138 ns/op 0 B/op 0 allocs/op
33+
Benchmark_TrimRight/fiber-12 578245326 2.084 ns/op 0 B/op 0 allocs/op
34+
Benchmark_TrimRight/default-12 345155300 3.380 ns/op 0 B/op 0 allocs/op
35+
Benchmark_TrimRight/default-12 366972850 3.328 ns/op 0 B/op 0 allocs/op
36+
37+
Benchmark_TrimRightBytes/fiber-12 586471208 2.099 ns/op 0 B/op 0 allocs/op
38+
Benchmark_TrimRightBytes/fiber-12 576055069 2.087 ns/op 0 B/op 0 allocs/op
39+
Benchmark_TrimRightBytes/default-12 348849292 3.316 ns/op 0 B/op 0 allocs/op
40+
Benchmark_TrimRightBytes/default-12 359904445 3.384 ns/op 0 B/op 0 allocs/op
41+
42+
Benchmark_TrimLeft/fiber-12 578044544 2.122 ns/op 0 B/op 0 allocs/op
43+
Benchmark_TrimLeft/fiber-12 585290433 2.074 ns/op 0 B/op 0 allocs/op
44+
Benchmark_TrimLeft/default-12 351906888 3.667 ns/op 0 B/op 0 allocs/op
45+
Benchmark_TrimLeft/default-12 330852666 3.448 ns/op 0 B/op 0 allocs/op
46+
47+
Benchmark_TrimLeftBytes/fiber-12 545400109 2.239 ns/op 0 B/op 0 allocs/op
48+
Benchmark_TrimLeftBytes/fiber-12 544800061 2.270 ns/op 0 B/op 0 allocs/op
49+
Benchmark_TrimLeftBytes/default-12 329749006 3.521 ns/op 0 B/op 0 allocs/op
50+
Benchmark_TrimLeftBytes/default-12 344199560 3.452 ns/op 0 B/op 0 allocs/op
51+
52+
Benchmark_Trim/fiber-12 280692232 4.128 ns/op 0 B/op 0 allocs/op
53+
Benchmark_Trim/fiber-12 297070083 3.961 ns/op 0 B/op 0 allocs/op
54+
Benchmark_Trim/default-12 232522952 5.163 ns/op 0 B/op 0 allocs/op
55+
Benchmark_Trim/default-12 230659057 5.172 ns/op 0 B/op 0 allocs/op
56+
Benchmark_Trim/default.trimspace-12 227328967 5.245 ns/op 0 B/op 0 allocs/op
57+
Benchmark_Trim/default.trimspace-12 227340775 5.253 ns/op 0 B/op 0 allocs/op
58+
59+
Benchmark_TrimBytes/fiber-12 275612090 4.280 ns/op 0 B/op 0 allocs/op
60+
Benchmark_TrimBytes/fiber-12 284892168 4.302 ns/op 0 B/op 0 allocs/op
61+
Benchmark_TrimBytes/default-12 224021550 5.163 ns/op 0 B/op 0 allocs/op
62+
Benchmark_TrimBytes/default-12 239689282 4.922 ns/op 0 B/op 0 allocs/op
63+
Benchmark_TrimBytes/default.trimspace-12 216809300 5.514 ns/op 0 B/op 0 allocs/op
64+
Benchmark_TrimBytes/default.trimspace-12 218177734 5.603 ns/op 0 B/op 0 allocs/op
65+
3266
Benchmark_EqualFoldBytes/fiber-12 22944849 47.14 ns/op 0 B/op 0 allocs/op
3367
Benchmark_EqualFoldBytes/fiber-12 26006342 46.82 ns/op 0 B/op 0 allocs/op
3468
Benchmark_EqualFoldBytes/default-12 5222006 222.5 ns/op 0 B/op 0 allocs/op
@@ -100,6 +134,7 @@ Benchmark_CalculateTimestamp/fiber-12 1000000000 0.2634 ns/op
100134
Benchmark_CalculateTimestamp/fiber-12 1000000000 0.2935 ns/op 0 B/op 0 allocs/op
101135
Benchmark_CalculateTimestamp/default-12 15740576 73.79 ns/op 0 B/op 0 allocs/op
102136
Benchmark_CalculateTimestamp/default-12 15789036 71.12 ns/op 0 B/op 0 allocs/op
137+
103138
```
104139

105140
See all the benchmarks under https://gofiber.github.io/utils/

byteseq.go

+35
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,38 @@ func EqualFold[S byteSeq](b, s S) bool {
1616
}
1717
return true
1818
}
19+
20+
// TrimLeft is the equivalent of strings/bytes.TrimLeft
21+
func TrimLeft[S byteSeq](s S, cutset byte) S {
22+
lenStr, start := len(s), 0
23+
for start < lenStr && s[start] == cutset {
24+
start++
25+
}
26+
return s[start:]
27+
}
28+
29+
// Trim is the equivalent of strings/bytes.Trim
30+
func Trim[S byteSeq](s S, cutset byte) S {
31+
i, j := 0, len(s)-1
32+
for ; i <= j; i++ {
33+
if s[i] != cutset {
34+
break
35+
}
36+
}
37+
for ; i < j; j-- {
38+
if s[j] != cutset {
39+
break
40+
}
41+
}
42+
43+
return s[i : j+1]
44+
}
45+
46+
// TrimRight is the equivalent of strings/bytes.TrimRight
47+
func TrimRight[S byteSeq](s S, cutset byte) S {
48+
lenStr := len(s)
49+
for lenStr > 0 && s[lenStr-1] == cutset {
50+
lenStr--
51+
}
52+
return s[:lenStr]
53+
}

byteseq_test.go

+214
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ func Benchmark_EqualFoldBytes(b *testing.B) {
3939
right := []byte(lowerStr)
4040
var res bool
4141
b.Run("fiber", func(b *testing.B) {
42+
b.ReportAllocs()
4243
for n := 0; n < b.N; n++ {
4344
res = EqualFold(left, right)
4445
}
4546
require.True(b, res)
4647
})
4748
b.Run("default", func(b *testing.B) {
49+
b.ReportAllocs()
4850
for n := 0; n < b.N; n++ {
4951
res = bytes.EqualFold(left, right)
5052
}
@@ -56,15 +58,227 @@ func Benchmark_EqualFoldBytes(b *testing.B) {
5658
func Benchmark_EqualFold(b *testing.B) {
5759
var res bool
5860
b.Run("fiber", func(b *testing.B) {
61+
b.ReportAllocs()
5962
for n := 0; n < b.N; n++ {
6063
res = EqualFold(upperStr, lowerStr)
6164
}
6265
require.True(b, res)
6366
})
6467
b.Run("default", func(b *testing.B) {
68+
b.ReportAllocs()
6569
for n := 0; n < b.N; n++ {
6670
res = strings.EqualFold(upperStr, lowerStr)
6771
}
6872
require.True(b, res)
6973
})
7074
}
75+
76+
func Test_TrimRight(t *testing.T) {
77+
t.Parallel()
78+
testCases := []struct {
79+
S1 string
80+
S2 string
81+
Cutset byte
82+
}{
83+
{S1: "/test//////", S2: "/test", Cutset: '/'},
84+
{S1: "/test", S2: "/test", Cutset: '/'},
85+
{S1: " ", S2: "", Cutset: ' '},
86+
{S1: " ", S2: "", Cutset: ' '},
87+
{S1: "", S2: "", Cutset: ' '},
88+
}
89+
90+
for _, tc := range testCases {
91+
res := TrimRight(tc.S1, tc.Cutset)
92+
require.Equal(t, tc.S2, res, "string")
93+
94+
resB := TrimRight([]byte(tc.S1), tc.Cutset)
95+
require.Equal(t, []byte(tc.S2), resB, "bytes")
96+
}
97+
}
98+
99+
func Benchmark_TrimRight(b *testing.B) {
100+
var res string
101+
word := "foobar "
102+
expected := "foobar"
103+
104+
b.Run("fiber", func(b *testing.B) {
105+
b.ReportAllocs()
106+
for n := 0; n < b.N; n++ {
107+
res = TrimRight(word, ' ')
108+
}
109+
require.Equal(b, expected, res)
110+
})
111+
b.Run("default", func(b *testing.B) {
112+
b.ReportAllocs()
113+
for n := 0; n < b.N; n++ {
114+
res = strings.TrimRight(word, " ")
115+
}
116+
require.Equal(b, expected, res)
117+
})
118+
}
119+
120+
func Benchmark_TrimRightBytes(b *testing.B) {
121+
var res []byte
122+
word := []byte("foobar ")
123+
expected := []byte("foobar")
124+
125+
b.Run("fiber", func(b *testing.B) {
126+
b.ReportAllocs()
127+
for n := 0; n < b.N; n++ {
128+
res = TrimRight(word, ' ')
129+
}
130+
require.Equal(b, expected, res)
131+
})
132+
b.Run("default", func(b *testing.B) {
133+
b.ReportAllocs()
134+
for n := 0; n < b.N; n++ {
135+
res = bytes.TrimRight(word, " ")
136+
}
137+
require.Equal(b, expected, res)
138+
})
139+
}
140+
141+
func Test_TrimLeft(t *testing.T) {
142+
t.Parallel()
143+
testCases := []struct {
144+
S1 string
145+
S2 string
146+
Cutset byte
147+
}{
148+
{S1: "////test/", S2: "test/", Cutset: '/'},
149+
{S1: "test/", S2: "test/", Cutset: '/'},
150+
{S1: " ", S2: "", Cutset: ' '},
151+
{S1: " ", S2: "", Cutset: ' '},
152+
{S1: "", S2: "", Cutset: ' '},
153+
}
154+
155+
for _, tc := range testCases {
156+
res := TrimLeft(tc.S1, tc.Cutset)
157+
require.Equal(t, tc.S2, res, "string")
158+
159+
resB := TrimLeft([]byte(tc.S1), tc.Cutset)
160+
require.Equal(t, []byte(tc.S2), resB, "bytes")
161+
}
162+
}
163+
164+
func Benchmark_TrimLeft(b *testing.B) {
165+
var res string
166+
word := " foobar"
167+
expected := "foobar"
168+
169+
b.Run("fiber", func(b *testing.B) {
170+
b.ReportAllocs()
171+
for n := 0; n < b.N; n++ {
172+
res = TrimLeft(word, ' ')
173+
}
174+
require.Equal(b, expected, res)
175+
})
176+
b.Run("default", func(b *testing.B) {
177+
b.ReportAllocs()
178+
for n := 0; n < b.N; n++ {
179+
res = strings.TrimLeft(word, " ")
180+
}
181+
require.Equal(b, expected, res)
182+
})
183+
}
184+
185+
func Benchmark_TrimLeftBytes(b *testing.B) {
186+
var res []byte
187+
word := []byte(" foobar")
188+
expected := []byte("foobar")
189+
190+
b.Run("fiber", func(b *testing.B) {
191+
b.ReportAllocs()
192+
for n := 0; n < b.N; n++ {
193+
res = TrimLeft(word, ' ')
194+
}
195+
require.Equal(b, expected, res)
196+
})
197+
b.Run("default", func(b *testing.B) {
198+
b.ReportAllocs()
199+
for n := 0; n < b.N; n++ {
200+
res = bytes.TrimLeft(word, " ")
201+
}
202+
require.Equal(b, expected, res)
203+
})
204+
}
205+
206+
func Test_Trim(t *testing.T) {
207+
t.Parallel()
208+
testCases := []struct {
209+
S1 string
210+
S2 string
211+
Cutset byte
212+
}{
213+
{S1: " test ", S2: "test", Cutset: ' '},
214+
{S1: "test", S2: "test", Cutset: ' '},
215+
{S1: ".test", S2: "test", Cutset: '.'},
216+
{S1: " ", S2: "", Cutset: ' '},
217+
{S1: " ", S2: "", Cutset: ' '},
218+
{S1: "", S2: "", Cutset: ' '},
219+
}
220+
221+
for _, tc := range testCases {
222+
res := Trim(tc.S1, tc.Cutset)
223+
require.Equal(t, tc.S2, res, "string")
224+
225+
resB := Trim([]byte(tc.S1), tc.Cutset)
226+
require.Equal(t, []byte(tc.S2), resB, "bytes")
227+
}
228+
}
229+
230+
func Benchmark_Trim(b *testing.B) {
231+
var res string
232+
word := " foobar "
233+
expected := "foobar"
234+
235+
b.Run("fiber", func(b *testing.B) {
236+
b.ReportAllocs()
237+
for n := 0; n < b.N; n++ {
238+
res = Trim(word, ' ')
239+
}
240+
require.Equal(b, expected, res)
241+
})
242+
b.Run("default", func(b *testing.B) {
243+
b.ReportAllocs()
244+
for n := 0; n < b.N; n++ {
245+
res = strings.Trim(word, " ")
246+
}
247+
require.Equal(b, expected, res)
248+
})
249+
b.Run("default.trimspace", func(b *testing.B) {
250+
b.ReportAllocs()
251+
for n := 0; n < b.N; n++ {
252+
res = strings.TrimSpace(word)
253+
}
254+
require.Equal(b, expected, res)
255+
})
256+
}
257+
258+
func Benchmark_TrimBytes(b *testing.B) {
259+
var res []byte
260+
word := []byte(" foobar ")
261+
expected := []byte("foobar")
262+
263+
b.Run("fiber", func(b *testing.B) {
264+
b.ReportAllocs()
265+
for n := 0; n < b.N; n++ {
266+
res = Trim(word, ' ')
267+
}
268+
require.Equal(b, expected, res)
269+
})
270+
b.Run("default", func(b *testing.B) {
271+
b.ReportAllocs()
272+
for n := 0; n < b.N; n++ {
273+
res = bytes.Trim(word, " ")
274+
}
275+
require.Equal(b, expected, res)
276+
})
277+
b.Run("default.trimspace", func(b *testing.B) {
278+
b.ReportAllocs()
279+
for n := 0; n < b.N; n++ {
280+
res = bytes.TrimSpace(word)
281+
}
282+
require.Equal(b, expected, res)
283+
})
284+
}

0 commit comments

Comments
 (0)