Skip to content

Commit ffae93e

Browse files
authored
Merge pull request #24 from Fenny/master
v0.1
2 parents 1402c2f + d470af7 commit ffae93e

18 files changed

+1253
-1163
lines changed

README.md

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,87 @@
1-
# Utils
2-
3-
[![Release](https://img.shields.io/github/release/gofiber/utils.svg)](https://github.com/gofiber/fiber/releases)
4-
[![Discord](https://img.shields.io/discord/704680098577514527?label=Discord&logo=discord&logoColor=white&color=7289DA)](https://gofiber.io/discord)
5-
[![Test](https://github.com/gofiber/utils/workflows/Test/badge.svg)](https://github.com/gofiber/utils/actions?query=workflow%3ATest)
6-
[![Security](https://github.com/gofiber/utils/workflows/Security/badge.svg)](https://github.com/gofiber/utils/actions?query=workflow%3ASecurity)
7-
[![Linter](https://github.com/gofiber/utils/workflows/Linter/badge.svg)](https://github.com/gofiber/utils/actions?query=workflow%3ALinter)
8-
91
A collection of common functions but with better performance, less allocations and no dependencies created for [Fiber](https://github.com/gofiber/fiber).
102

113
```go
12-
// go test -v -benchmem -run=^$ -bench=Benchmark_ -count=2
13-
14-
Benchmark_GetMIME/fiber 14287550 84.2 ns/op 0 B/op 0 allocs/op
15-
Benchmark_GetMIME/fiber 14819698 78.3 ns/op 0 B/op 0 allocs/op
16-
Benchmark_GetMIME/default 6459128 184 ns/op 0 B/op 0 allocs/op
17-
Benchmark_GetMIME/default 6385042 184 ns/op 0 B/op 0 allocs/op
18-
19-
Benchmark_UUID/fiber 17652744 59.1 ns/op 48 B/op 1 allocs/op
20-
Benchmark_UUID/fiber 19361145 58.5 ns/op 48 B/op 1 allocs/op
21-
Benchmark_UUID/default 4271024 281 ns/op 64 B/op 2 allocs/op
22-
Benchmark_UUID/default 4435306 278 ns/op 64 B/op 2 allocs/op
23-
24-
Benchmark_ToLower/fiber 22987184 48.2 ns/op 48 B/op 1 allocs/op
25-
Benchmark_ToLower/fiber 24491794 49.6 ns/op 48 B/op 1 allocs/op
26-
Benchmark_ToLower/default 9232608 123 ns/op 48 B/op 1 allocs/op
27-
Benchmark_ToLower/default 9454870 123 ns/op 48 B/op 1 allocs/op
28-
29-
Benchmark_ToLowerBytes/fiber 44463876 26.1 ns/op 0 B/op 0 allocs/op
30-
Benchmark_ToLowerBytes/fiber 39997200 26.1 ns/op 0 B/op 0 allocs/op
31-
Benchmark_ToLowerBytes/default 14879088 77.6 ns/op 48 B/op 1 allocs/op
32-
Benchmark_ToLowerBytes/default 14631433 79.2 ns/op 48 B/op 1 allocs/op
33-
34-
Benchmark_ToUpper/fiber 22648730 49.4 ns/op 48 B/op 1 allocs/op
35-
Benchmark_ToUpper/fiber 23084425 48.6 ns/op 48 B/op 1 allocs/op
36-
Benchmark_ToUpper/default 9520122 124 ns/op 48 B/op 1 allocs/op
37-
Benchmark_ToUpper/default 9375014 133 ns/op 48 B/op 1 allocs/op
38-
39-
Benchmark_ToUpperBytes/fiber 44439176 25.6 ns/op 0 B/op 0 allocs/op
40-
Benchmark_ToUpperBytes/fiber 44458934 25.5 ns/op 0 B/op 0 allocs/op
41-
Benchmark_ToUpperBytes/default 15347073 74.1 ns/op 48 B/op 1 allocs/op
42-
Benchmark_ToUpperBytes/default 15511370 74.2 ns/op 48 B/op 1 allocs/op
43-
44-
Benchmark_EqualFolds/fiber 34297864 33.8 ns/op 0 B/op 0 allocs/op
45-
Benchmark_EqualFolds/fiber 34285322 34.0 ns/op 0 B/op 0 allocs/op
46-
Benchmark_EqualFolds/default 12756945 91.8 ns/op 0 B/op 0 allocs/op
47-
Benchmark_EqualFolds/default 13015282 91.1 ns/op 0 B/op 0 allocs/op
48-
49-
Benchmark_Trim/fiber 207314002 5.85 ns/op 0 B/op 0 allocs/op
50-
Benchmark_Trim/fiber 207386125 5.78 ns/op 0 B/op 0 allocs/op
51-
Benchmark_Trim/default 16506302 68.5 ns/op 32 B/op 1 allocs/op
52-
Benchmark_Trim/default 16669119 68.9 ns/op 32 B/op 1 allocs/op
53-
54-
Benchmark_TrimLeft/fiber 343254828 3.47 ns/op 0 B/op 0 allocs/op
55-
Benchmark_TrimLeft/fiber 344407171 3.45 ns/op 0 B/op 0 allocs/op
56-
Benchmark_TrimLeft/default 24999790 46.4 ns/op 32 B/op 1 allocs/op
57-
Benchmark_TrimLeft/default 25001926 45.3 ns/op 32 B/op 1 allocs/op
58-
59-
Benchmark_TrimRight/fiber 374543056 3.15 ns/op 0 B/op 0 allocs/op
60-
Benchmark_TrimRight/fiber 336067616 3.15 ns/op 0 B/op 0 allocs/op
61-
Benchmark_TrimRight/default 20868186 52.8 ns/op 32 B/op 1 allocs/op
62-
Benchmark_TrimRight/default 21434695 55.1 ns/op 32 B/op 1 allocs/op
4+
// go test -benchmem -run=^$ -bench=Benchmark_ -count=2
5+
6+
Benchmark_ToLowerBytes/fiber-16 42847654 25.7 ns/op 0 B/op 0 allocs/op
7+
Benchmark_ToLowerBytes/fiber-16 46143196 25.7 ns/op 0 B/op 0 allocs/op
8+
Benchmark_ToLowerBytes/default-16 17387322 67.4 ns/op 48 B/op 1 allocs/op
9+
Benchmark_ToLowerBytes/default-16 17906491 67.4 ns/op 48 B/op 1 allocs/op
10+
11+
Benchmark_ToUpperBytes/fiber-16 46143729 25.7 ns/op 0 B/op 0 allocs/op
12+
Benchmark_ToUpperBytes/fiber-16 47989250 25.6 ns/op 0 B/op 0 allocs/op
13+
Benchmark_ToUpperBytes/default-16 15580854 76.7 ns/op 48 B/op 1 allocs/op
14+
Benchmark_ToUpperBytes/default-16 15381202 76.9 ns/op 48 B/op 1 allocs/op
15+
16+
Benchmark_TrimRightBytes/fiber-16 70572459 16.3 ns/op 8 B/op 1 allocs/op
17+
Benchmark_TrimRightBytes/fiber-16 74983597 16.3 ns/op 8 B/op 1 allocs/op
18+
Benchmark_TrimRightBytes/default-16 16212578 74.1 ns/op 40 B/op 2 allocs/op
19+
Benchmark_TrimRightBytes/default-16 16434686 74.1 ns/op 40 B/op 2 allocs/op
20+
21+
Benchmark_TrimLeftBytes/fiber-16 74983128 16.3 ns/op 8 B/op 1 allocs/op
22+
Benchmark_TrimLeftBytes/fiber-16 74985002 16.3 ns/op 8 B/op 1 allocs/op
23+
Benchmark_TrimLeftBytes/default-16 21047868 56.5 ns/op 40 B/op 2 allocs/op
24+
Benchmark_TrimLeftBytes/default-16 21048015 56.5 ns/op 40 B/op 2 allocs/op
25+
26+
Benchmark_TrimBytes/fiber-16 54533307 21.9 ns/op 16 B/op 1 allocs/op
27+
Benchmark_TrimBytes/fiber-16 54532812 21.9 ns/op 16 B/op 1 allocs/op
28+
Benchmark_TrimBytes/default-16 14282517 84.6 ns/op 48 B/op 2 allocs/op
29+
Benchmark_TrimBytes/default-16 14114508 84.7 ns/op 48 B/op 2 allocs/op
30+
31+
Benchmark_EqualFolds/fiber-16 36355153 32.6 ns/op 0 B/op 0 allocs/op
32+
Benchmark_EqualFolds/fiber-16 36355593 32.6 ns/op 0 B/op 0 allocs/op
33+
Benchmark_EqualFolds/default-16 15186220 78.1 ns/op 0 B/op 0 allocs/op
34+
Benchmark_EqualFolds/default-16 15186412 78.3 ns/op 0 B/op 0 allocs/op
35+
36+
Benchmark_UUID/fiber-16 23994625 49.8 ns/op 48 B/op 1 allocs/op
37+
Benchmark_UUID/fiber-16 23994768 50.1 ns/op 48 B/op 1 allocs/op
38+
Benchmark_UUID/default-16 3233772 371 ns/op 208 B/op 6 allocs/op
39+
Benchmark_UUID/default-16 3251295 370 ns/op 208 B/op 6 allocs/op
40+
41+
Benchmark_GetString/unsafe-16 1000000000 0.709 ns/op 0 B/op 0 allocs/op
42+
Benchmark_GetString/unsafe-16 1000000000 0.713 ns/op 0 B/op 0 allocs/op
43+
Benchmark_GetString/default-16 59986202 19.0 ns/op 16 B/op 1 allocs/op
44+
Benchmark_GetString/default-16 63142939 19.0 ns/op 16 B/op 1 allocs/op
45+
46+
Benchmark_GetBytes/unsafe-16 508360195 2.36 ns/op 0 B/op 0 allocs/op
47+
Benchmark_GetBytes/unsafe-16 508359979 2.35 ns/op 0 B/op 0 allocs/op
48+
Benchmark_GetBytes/default-16 46143019 25.7 ns/op 16 B/op 1 allocs/op
49+
Benchmark_GetBytes/default-16 44434734 25.6 ns/op 16 B/op 1 allocs/op
50+
51+
Benchmark_GetMIME/fiber-16 21423750 56.3 ns/op 0 B/op 0 allocs/op
52+
Benchmark_GetMIME/fiber-16 21423559 55.4 ns/op 0 B/op 0 allocs/op
53+
Benchmark_GetMIME/default-16 6735282 173 ns/op 0 B/op 0 allocs/op
54+
Benchmark_GetMIME/default-16 6895002 172 ns/op 0 B/op 0 allocs/op
55+
56+
Benchmark_StatusMessage/fiber-16 1000000000 0.766 ns/op 0 B/op 0 allocs/op
57+
Benchmark_StatusMessage/fiber-16 1000000000 0.767 ns/op 0 B/op 0 allocs/op
58+
Benchmark_StatusMessage/default-16 159538528 7.50 ns/op 0 B/op 0 allocs/op
59+
Benchmark_StatusMessage/default-16 159750830 7.51 ns/op 0 B/op 0 allocs/op
60+
61+
Benchmark_ToUpper/fiber-16 22217408 53.3 ns/op 48 B/op 1 allocs/op
62+
Benchmark_ToUpper/fiber-16 22636554 53.2 ns/op 48 B/op 1 allocs/op
63+
Benchmark_ToUpper/default-16 11108600 108 ns/op 48 B/op 1 allocs/op
64+
Benchmark_ToUpper/default-16 11108580 108 ns/op 48 B/op 1 allocs/op
65+
66+
Benchmark_ToLower/fiber-16 23994720 49.8 ns/op 48 B/op 1 allocs/op
67+
Benchmark_ToLower/fiber-16 23994768 50.1 ns/op 48 B/op 1 allocs/op
68+
Benchmark_ToLower/default-16 10808376 110 ns/op 48 B/op 1 allocs/op
69+
Benchmark_ToLower/default-16 10617034 110 ns/op 48 B/op 1 allocs/op
70+
71+
Benchmark_TrimRight/fiber-16 413699521 2.94 ns/op 0 B/op 0 allocs/op
72+
Benchmark_TrimRight/fiber-16 415131687 2.91 ns/op 0 B/op 0 allocs/op
73+
Benchmark_TrimRight/default-16 23994577 49.1 ns/op 32 B/op 1 allocs/op
74+
Benchmark_TrimRight/default-16 24484249 49.4 ns/op 32 B/op 1 allocs/op
75+
76+
Benchmark_TrimLeft/fiber-16 379661170 3.13 ns/op 0 B/op 0 allocs/op
77+
Benchmark_TrimLeft/fiber-16 382079941 3.16 ns/op 0 B/op 0 allocs/op
78+
Benchmark_TrimLeft/default-16 27900877 41.9 ns/op 32 B/op 1 allocs/op
79+
Benchmark_TrimLeft/default-16 28564898 42.0 ns/op 32 B/op 1 allocs/op
80+
81+
Benchmark_Trim/fiber-16 236632856 4.96 ns/op 0 B/op 0 allocs/op
82+
Benchmark_Trim/fiber-16 237570085 4.93 ns/op 0 B/op 0 allocs/op
83+
Benchmark_Trim/default-16 18457221 66.0 ns/op 32 B/op 1 allocs/op
84+
Benchmark_Trim/default-16 18177328 65.9 ns/op 32 B/op 1 allocs/op
85+
Benchmark_Trim/default.trimspace-16 188933770 6.33 ns/op 0 B/op 0 allocs/op
86+
Benchmark_Trim/default.trimspace-16 184007649 6.42 ns/op 0 B/op 0 allocs/op
6387
```

assertions.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
2+
// 🤖 Github Repository: https://github.com/gofiber/fiber
3+
// 📌 API Documentation: https://docs.gofiber.io
4+
5+
package utils
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"log"
11+
"path/filepath"
12+
"reflect"
13+
"runtime"
14+
"testing"
15+
"text/tabwriter"
16+
)
17+
18+
// AssertEqual checks if values are equal
19+
func AssertEqual(t testing.TB, expected interface{}, actual interface{}, description ...string) {
20+
if reflect.DeepEqual(expected, actual) {
21+
return
22+
}
23+
var aType = "<nil>"
24+
var bType = "<nil>"
25+
if reflect.ValueOf(expected).IsValid() {
26+
aType = reflect.TypeOf(expected).Name()
27+
}
28+
if reflect.ValueOf(actual).IsValid() {
29+
bType = reflect.TypeOf(actual).Name()
30+
}
31+
32+
testName := "AssertEqual"
33+
if t != nil {
34+
testName = t.Name()
35+
}
36+
37+
_, file, line, _ := runtime.Caller(1)
38+
39+
var buf bytes.Buffer
40+
w := tabwriter.NewWriter(&buf, 0, 0, 5, ' ', 0)
41+
fmt.Fprintf(w, "\nTest:\t%s", testName)
42+
fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
43+
fmt.Fprintf(w, "\nError:\tNot equal")
44+
fmt.Fprintf(w, "\nExpect:\t%v\t[%s]", expected, aType)
45+
fmt.Fprintf(w, "\nResult:\t%v\t[%s]", actual, bType)
46+
47+
if len(description) > 0 {
48+
fmt.Fprintf(w, "\nDescription:\t%s", description[0])
49+
}
50+
51+
result := ""
52+
if err := w.Flush(); err != nil {
53+
result = err.Error()
54+
} else {
55+
result = buf.String()
56+
}
57+
if t != nil {
58+
t.Fatal(result)
59+
} else {
60+
log.Fatal(result)
61+
}
62+
}

assertions_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
2+
// 🤖 Github Repository: https://github.com/gofiber/fiber
3+
// 📌 API Documentation: https://docs.gofiber.io
4+
5+
package utils
6+
7+
import "testing"
8+
9+
func Test_Utils_AssertEqual(t *testing.T) {
10+
t.Parallel()
11+
AssertEqual(nil, []string{}, []string{})
12+
AssertEqual(t, []string{}, []string{})
13+
}

bytes.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
2+
// 🤖 Github Repository: https://github.com/gofiber/fiber
3+
// 📌 API Documentation: https://docs.gofiber.io
4+
5+
package utils
6+
7+
// ToLowerBytes is the equivalent of bytes.ToLower
8+
func ToLowerBytes(b []byte) []byte {
9+
for i := 0; i < len(b); i++ {
10+
b[i] = toLowerTable[b[i]]
11+
}
12+
return b
13+
}
14+
15+
// ToUpperBytes is the equivalent of bytes.ToUpper
16+
func ToUpperBytes(b []byte) []byte {
17+
for i := 0; i < len(b); i++ {
18+
b[i] = toUpperTable[b[i]]
19+
}
20+
return b
21+
}
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+
// EqualFold the equivalent of bytes.EqualFold
59+
func EqualsFold(b, s []byte) (equals bool) {
60+
n := len(b)
61+
equals = n == len(s)
62+
if equals {
63+
for i := 0; i < n; i++ {
64+
if equals = b[i]|0x20 == s[i]|0x20; !equals {
65+
break
66+
}
67+
}
68+
}
69+
return
70+
}

0 commit comments

Comments
 (0)