v3 (deps): move `utils` to `https://github.com/gofiber/utils`

pull/2130/head
Muhammed Efe Çetin 2022-09-23 21:19:44 +03:00
parent 10d6f69a89
commit ca6f25a890
No known key found for this signature in database
GPG Key ID: 0AA4D45CBAA86F73
49 changed files with 33 additions and 1380 deletions

2
app.go
View File

@ -24,7 +24,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -2,7 +2,7 @@ package fiber
import ( import (
"github.com/gofiber/fiber/v3/binder" "github.com/gofiber/fiber/v3/binder"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// An interface to register custom binders. // An interface to register custom binders.

View File

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -1,7 +1,7 @@
package binder package binder
import ( import (
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
type jsonBinding struct{} type jsonBinding struct{}

View File

@ -6,7 +6,7 @@ import (
"sync" "sync"
"github.com/gofiber/fiber/v3/internal/schema" "github.com/gofiber/fiber/v3/internal/schema"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/bytebufferpool" "github.com/valyala/bytebufferpool"
) )

View File

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -16,7 +16,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

2
ctx.go
View File

@ -21,7 +21,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/savsgio/dictpool" "github.com/savsgio/dictpool"
"github.com/valyala/bytebufferpool" "github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"

View File

@ -25,7 +25,7 @@ import (
"time" "time"
"github.com/gofiber/fiber/v3/internal/storage/memory" "github.com/gofiber/fiber/v3/internal/storage/memory"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/valyala/bytebufferpool" "github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/gofiber/fiber/v3
go 1.18 go 1.18
require ( require (
github.com/gofiber/utils v1.0.0-beta.1
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/mattn/go-colorable v0.1.13 github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.16 github.com/mattn/go-isatty v0.0.16

2
go.sum
View File

@ -3,6 +3,8 @@ github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/utils v1.0.0-beta.1 h1:7BntpaQqfHa6r78vf+5a5nnZc8tuPWv89PSqk/+WxIc=
github.com/gofiber/utils v1.0.0-beta.1/go.mod h1:RYennBgjLkuNtU+dxg7QgBEU8tmixFQHQ2GE1ioZlxw=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=

View File

@ -10,7 +10,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -10,7 +10,7 @@ import (
"net/http" "net/http"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor" "github.com/valyala/fasthttp/fasthttpadaptor"
) )

View File

@ -5,7 +5,7 @@ import (
"strings" "strings"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// New creates a new middleware handler // New creates a new middleware handler

View File

@ -4,7 +4,7 @@ import (
"crypto/subtle" "crypto/subtle"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// Config defines the config for middleware. // Config defines the config for middleware.

View File

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// timestampUpdatePeriod is the period which is used to check the cache expiration. // timestampUpdatePeriod is the period which is used to check the cache expiration.

View File

@ -16,7 +16,7 @@ import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/internal/storage/memory" "github.com/gofiber/fiber/v3/internal/storage/memory"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -4,7 +4,7 @@ import (
"time" "time"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// Config defines the config for middleware. // Config defines the config for middleware.

View File

@ -6,7 +6,7 @@ import (
"time" "time"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// Config defines the config for middleware. // Config defines the config for middleware.

View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -12,7 +12,7 @@ import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/internal/tlstest" "github.com/gofiber/fiber/v3/internal/tlstest"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@ -2,7 +2,7 @@ package requestid
import ( import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// Config defines the config for middleware. // Config defines the config for middleware.

View File

@ -5,7 +5,7 @@ import (
"time" "time"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
) )
// Config defines the config for middleware. // Config defines the config for middleware.

View File

@ -7,7 +7,7 @@ import (
"time" "time"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -6,7 +6,7 @@ import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/internal/storage/memory" "github.com/gofiber/fiber/v3/internal/storage/memory"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -9,7 +9,7 @@ import (
"sync" "sync"
"github.com/gofiber/fiber/v3/binder" "github.com/gofiber/fiber/v3/binder"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/bytebufferpool" "github.com/valyala/bytebufferpool"
) )

View File

@ -12,7 +12,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -16,7 +16,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/gofiber/fiber/v3/utils" "github.com/gofiber/utils"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )

View File

@ -1,61 +0,0 @@
A collection of common functions but with better performance, less allocations and no dependencies created for [Fiber](https://github.com/gofiber/fiber).
```go
// go test -benchmem -run=^$ -bench=Benchmark_ -count=2
goos: windows
goarch: amd64
pkg: github.com/gofiber/fiber/v3/utils
cpu: AMD Ryzen 7 5800X 8-Core Processor
Benchmark_ToLowerBytes/fiber-16 51138252 22.61 ns/op 0 B/op 0 allocs/op
Benchmark_ToLowerBytes/fiber-16 52126545 22.63 ns/op 0 B/op 0 allocs/op
Benchmark_ToLowerBytes/default-16 16114736 72.76 ns/op 80 B/op 1 allocs/op
Benchmark_ToLowerBytes/default-16 16651540 73.85 ns/op 80 B/op 1 allocs/op
Benchmark_ToUpperBytes/fiber-16 52127224 22.62 ns/op 0 B/op 0 allocs/op
Benchmark_ToUpperBytes/fiber-16 54283167 22.86 ns/op 0 B/op 0 allocs/op
Benchmark_ToUpperBytes/default-16 14060098 84.12 ns/op 80 B/op 1 allocs/op
Benchmark_ToUpperBytes/default-16 14183122 84.51 ns/op 80 B/op 1 allocs/op
Benchmark_EqualFoldBytes/fiber-16 29240264 41.22 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFoldBytes/fiber-16 28535826 40.84 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFoldBytes/default-16 7929867 150.2 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFoldBytes/default-16 7935478 149.7 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFold/fiber-16 35442768 34.25 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFold/fiber-16 35946870 34.96 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFold/default-16 8942130 133.5 ns/op 0 B/op 0 allocs/op
Benchmark_EqualFold/default-16 8977231 134.3 ns/op 0 B/op 0 allocs/op
Benchmark_UUID/fiber-16 30726213 40.57 ns/op 48 B/op 1 allocs/op
Benchmark_UUID/fiber-16 26539394 40.25 ns/op 48 B/op 1 allocs/op
Benchmark_UUID/default-16 4737199 247.5 ns/op 168 B/op 6 allocs/op
Benchmark_UUID/default-16 4603738 250.8 ns/op 168 B/op 6 allocs/op
Benchmark_ConvertToBytes/fiber-16 62450884 19.41 ns/op 0 B/op 0 allocs/op
Benchmark_ConvertToBytes/fiber-16 52123602 19.53 ns/op 0 B/op 0 allocs/op
Benchmark_UnsafeString/unsafe-16 1000000000 0.4496 ns/op 0 B/op 0 allocs/op
Benchmark_UnsafeString/unsafe-16 1000000000 0.4488 ns/op 0 B/op 0 allocs/op
Benchmark_UnsafeString/default-16 79925935 13.99 ns/op 16 B/op 1 allocs/op
Benchmark_UnsafeString/default-16 85637211 14.35 ns/op 16 B/op 1 allocs/op
Benchmark_UnsafeBytes/unsafe-16 540970148 2.214 ns/op 0 B/op 0 allocs/op
Benchmark_UnsafeBytes/unsafe-16 543356940 2.212 ns/op 0 B/op 0 allocs/op
Benchmark_UnsafeBytes/default-16 68896224 17.19 ns/op 16 B/op 1 allocs/op
Benchmark_UnsafeBytes/default-16 70560426 17.05 ns/op 16 B/op 1 allocs/op
Benchmark_ToString-16 29504036 39.57 ns/op 40 B/op 2 allocs/op
Benchmark_ToString-16 30738334 38.89 ns/op 40 B/op 2 allocs/op
Benchmark_GetMIME/fiber-16 28207086 41.84 ns/op 0 B/op 0 allocs/op
Benchmark_GetMIME/fiber-16 28165773 41.83 ns/op 0 B/op 0 allocs/op
Benchmark_GetMIME/default-16 12583132 94.04 ns/op 0 B/op 0 allocs/op
Benchmark_GetMIME/default-16 12829614 93.50 ns/op 0 B/op 0 allocs/op
Benchmark_ParseVendorSpecificContentType/vendorContentType-16 30267411 38.72 ns/op 16 B/op 1 allocs/op
Benchmark_ParseVendorSpecificContentType/vendorContentType-16 28543563 38.60 ns/op 16 B/op 1 allocs/op
Benchmark_ParseVendorSpecificContentType/defaultContentType-16 249869286 4.830 ns/op 0 B/op 0 allocs/op
Benchmark_ParseVendorSpecificContentType/defaultContentType-16 248999592 4.805 ns/op 0 B/op 0 allocs/op
Benchmark_StatusMessage/fiber-16 1000000000 0.6744 ns/op 0 B/op 0 allocs/op
Benchmark_StatusMessage/fiber-16 1000000000 0.6788 ns/op 0 B/op 0 allocs/op
Benchmark_StatusMessage/default-16 446818872 2.664 ns/op 0 B/op 0 allocs/op
Benchmark_StatusMessage/default-16 447009616 2.661 ns/op 0 B/op 0 allocs/op
Benchmark_ToUpper/fiber-16 20480331 56.50 ns/op 80 B/op 1 allocs/op
Benchmark_ToUpper/fiber-16 21541200 56.65 ns/op 80 B/op 1 allocs/op
Benchmark_ToUpper/default-16 8433409 141.2 ns/op 80 B/op 1 allocs/op
Benchmark_ToUpper/default-16 8473737 141.1 ns/op 80 B/op 1 allocs/op
Benchmark_ToLower/fiber-16 27248326 44.68 ns/op 80 B/op 1 allocs/op
Benchmark_ToLower/fiber-16 26918443 44.70 ns/op 80 B/op 1 allocs/op
Benchmark_ToLower/default-16 8447336 141.9 ns/op 80 B/op 1 allocs/op
Benchmark_ToLower/default-16 8423156 140.6 ns/op 80 B/op 1 allocs/op
```

View File

@ -1,21 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
// ToLowerBytes converts ascii slice to lower-case in-place.
func ToLowerBytes(b []byte) []byte {
for i := 0; i < len(b); i++ {
b[i] = toLowerTable[b[i]]
}
return b
}
// ToUpperBytes converts ascii slice to upper-case in-place.
func ToUpperBytes(b []byte) []byte {
for i := 0; i < len(b); i++ {
b[i] = toUpperTable[b[i]]
}
return b
}

View File

@ -1,68 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
func Test_ToLowerBytes(t *testing.T) {
t.Parallel()
require.Equal(t, []byte("/my/name/is/:param/*"), ToLowerBytes([]byte("/MY/NAME/IS/:PARAM/*")))
require.Equal(t, []byte("/my1/name/is/:param/*"), ToLowerBytes([]byte("/MY1/NAME/IS/:PARAM/*")))
require.Equal(t, []byte("/my2/name/is/:param/*"), ToLowerBytes([]byte("/MY2/NAME/IS/:PARAM/*")))
require.Equal(t, []byte("/my3/name/is/:param/*"), ToLowerBytes([]byte("/MY3/NAME/IS/:PARAM/*")))
require.Equal(t, []byte("/my4/name/is/:param/*"), ToLowerBytes([]byte("/MY4/NAME/IS/:PARAM/*")))
}
func Benchmark_ToLowerBytes(b *testing.B) {
path := []byte(largeStr)
want := []byte(lowerStr)
var res []byte
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ToLowerBytes(path)
}
require.Equal(b, bytes.Equal(want, res), true)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = bytes.ToLower(path)
}
require.Equal(b, bytes.Equal(want, res), true)
})
}
func Test_ToUpperBytes(t *testing.T) {
t.Parallel()
require.Equal(t, []byte("/MY/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my/name/is/:param/*")))
require.Equal(t, []byte("/MY1/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my1/name/is/:param/*")))
require.Equal(t, []byte("/MY2/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my2/name/is/:param/*")))
require.Equal(t, []byte("/MY3/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my3/name/is/:param/*")))
require.Equal(t, []byte("/MY4/NAME/IS/:PARAM/*"), ToUpperBytes([]byte("/my4/name/is/:param/*")))
}
func Benchmark_ToUpperBytes(b *testing.B) {
path := []byte(largeStr)
want := []byte(upperStr)
var res []byte
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ToUpperBytes(path)
}
require.Equal(b, want, res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = bytes.ToUpper(path)
}
require.Equal(b, want, res)
})
}

View File

@ -1,18 +0,0 @@
package utils
type byteSeq interface {
~string | ~[]byte
}
// EqualFold tests ascii strings or bytes for equality case-insensitively
func EqualFold[S byteSeq](b, s S) bool {
if len(b) != len(s) {
return false
}
for i := len(b) - 1; i >= 0; i-- {
if toUpperTable[b[i]] != toUpperTable[s[i]] {
return false
}
}
return true
}

View File

@ -1,70 +0,0 @@
package utils
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Benchmark_EqualFoldBytes(b *testing.B) {
left := []byte(upperStr)
right := []byte(lowerStr)
var res bool
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = EqualFold(left, right)
}
require.True(b, res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = bytes.EqualFold(left, right)
}
require.True(b, res)
})
}
// go test -v -run=^$ -bench=Benchmark_EqualFold -benchmem -count=4 ./utils/
func Benchmark_EqualFold(b *testing.B) {
var res bool
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = EqualFold(upperStr, lowerStr)
}
require.True(b, res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = strings.EqualFold(upperStr, lowerStr)
}
require.True(b, res)
})
}
func Test_EqualFold(t *testing.T) {
t.Parallel()
testCases := []struct {
Expected bool
S1 string
S2 string
}{
{Expected: true, S1: "/MY/NAME/IS/:PARAM/*", S2: "/my/name/is/:param/*"},
{Expected: true, S1: "/MY/NAME/IS/:PARAM/*", S2: "/my/name/is/:param/*"},
{Expected: true, S1: "/MY1/NAME/IS/:PARAM/*", S2: "/MY1/NAME/IS/:PARAM/*"},
{Expected: false, S1: "/my2/name/is/:param/*", S2: "/my2/name"},
{Expected: false, S1: "/dddddd", S2: "eeeeee"},
{Expected: false, S1: "\na", S2: "*A"},
{Expected: true, S1: "/MY3/NAME/IS/:PARAM/*", S2: "/my3/name/is/:param/*"},
{Expected: true, S1: "/MY4/NAME/IS/:PARAM/*", S2: "/my4/nAME/IS/:param/*"},
}
for _, tc := range testCases {
res := EqualFold[string](tc.S1, tc.S2)
require.Equal(t, tc.Expected, res, "string")
res = EqualFold[[]byte]([]byte(tc.S1), []byte(tc.S2))
require.Equal(t, tc.Expected, res, "bytes")
}
}

View File

@ -1,155 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"math"
"net"
"os"
"reflect"
"runtime"
"strconv"
"sync"
"sync/atomic"
"unicode"
"github.com/google/uuid"
)
const (
toLowerTable = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
toUpperTable = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\u007f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
)
// Copyright © 2014, Roger Peppe
// github.com/rogpeppe/fastuuid
// All rights reserved.
var (
uuidSeed [24]byte
uuidCounter uint64
uuidSetup sync.Once
unitsSlice = []byte("kmgtp")
)
// UUID generates an universally unique identifier (UUID)
func UUID() string {
// Setup seed & counter once
uuidSetup.Do(func() {
if _, err := rand.Read(uuidSeed[:]); err != nil {
return
}
uuidCounter = binary.LittleEndian.Uint64(uuidSeed[:8])
})
if atomic.LoadUint64(&uuidCounter) <= 0 {
return "00000000-0000-0000-0000-000000000000"
}
// first 8 bytes differ, taking a slice of the first 16 bytes
x := atomic.AddUint64(&uuidCounter, 1)
uuid := uuidSeed
binary.LittleEndian.PutUint64(uuid[:8], x)
uuid[6], uuid[9] = uuid[9], uuid[6]
// RFC4122 v4
uuid[6] = (uuid[6] & 0x0f) | 0x40
uuid[8] = uuid[8]&0x3f | 0x80
// create UUID representation of the first 128 bits
b := make([]byte, 36)
hex.Encode(b[0:8], uuid[0:4])
b[8] = '-'
hex.Encode(b[9:13], uuid[4:6])
b[13] = '-'
hex.Encode(b[14:18], uuid[6:8])
b[18] = '-'
hex.Encode(b[19:23], uuid[8:10])
b[23] = '-'
hex.Encode(b[24:], uuid[10:16])
return UnsafeString(b)
}
// UUIDv4 returns a Random (Version 4) UUID.
// The strength of the UUIDs is based on the strength of the crypto/rand package.
func UUIDv4() string {
token, err := uuid.NewRandom()
if err != nil {
return UUID()
}
return token.String()
}
// FunctionName returns function name
func FunctionName(fn any) string {
t := reflect.ValueOf(fn).Type()
if t.Kind() == reflect.Func {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
}
return t.String()
}
// GetArgument check if key is in arguments
func GetArgument(arg string) bool {
for i := range os.Args[1:] {
if os.Args[1:][i] == arg {
return true
}
}
return false
}
// IncrementIPRange Find available next IP address
func IncrementIPRange(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// ConvertToBytes returns integer size of bytes from human-readable string, ex. 42kb, 42M
// Returns 0 if string is unrecognized
func ConvertToBytes(humanReadableString string) int {
strLen := len(humanReadableString)
if strLen == 0 {
return 0
}
var unitPrefixPos, lastNumberPos int
// loop the string
for i := strLen - 1; i >= 0; i-- {
// check if the char is a number
if unicode.IsDigit(rune(humanReadableString[i])) {
lastNumberPos = i
break
} else if humanReadableString[i] != ' ' {
unitPrefixPos = i
}
}
if lastNumberPos < 0 {
return 0
}
// fetch the number part and parse it to float
size, err := strconv.ParseFloat(humanReadableString[:lastNumberPos+1], 64)
if err != nil {
return 0
}
// check the multiplier from the string and use it
if unitPrefixPos > 0 {
// convert multiplier char to lowercase and check if exists in units slice
index := bytes.IndexByte(unitsSlice, toLowerTable[humanReadableString[unitPrefixPos]])
if index != -1 {
size *= math.Pow(1000, float64(index+1))
}
}
return int(size)
}

View File

@ -1,128 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"crypto/rand"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func Test_FunctionName(t *testing.T) {
t.Parallel()
require.Equal(t, "github.com/gofiber/fiber/v3/utils.Test_UUID", FunctionName(Test_UUID))
require.Equal(t, "github.com/gofiber/fiber/v3/utils.Test_FunctionName.func1", FunctionName(func() {}))
dummyint := 20
require.Equal(t, "int", FunctionName(dummyint))
}
func Test_UUID(t *testing.T) {
t.Parallel()
res := UUID()
require.Equal(t, 36, len(res))
require.True(t, res != "00000000-0000-0000-0000-000000000000")
}
func Test_UUID_Concurrency(t *testing.T) {
t.Parallel()
iterations := 1000
var res string
ch := make(chan string, iterations)
results := make(map[string]string)
for i := 0; i < iterations; i++ {
go func() {
ch <- UUID()
}()
}
for i := 0; i < iterations; i++ {
res = <-ch
results[res] = res
}
require.Equal(t, iterations, len(results))
}
func Test_UUIDv4(t *testing.T) {
t.Parallel()
res := UUIDv4()
require.Equal(t, 36, len(res))
require.True(t, res != "00000000-0000-0000-0000-000000000000")
}
func Test_UUIDv4_Concurrency(t *testing.T) {
t.Parallel()
iterations := 1000
var res string
ch := make(chan string, iterations)
results := make(map[string]string)
for i := 0; i < iterations; i++ {
go func() {
ch <- UUIDv4()
}()
}
for i := 0; i < iterations; i++ {
res = <-ch
results[res] = res
}
require.Equal(t, iterations, len(results))
}
// go test -v -run=^$ -bench=Benchmark_UUID -benchmem -count=2
func Benchmark_UUID(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = UUID()
}
require.Equal(b, 36, len(res))
})
b.Run("default", func(b *testing.B) {
rnd := make([]byte, 16)
_, _ = rand.Read(rnd)
for n := 0; n < b.N; n++ {
res = fmt.Sprintf("%x-%x-%x-%x-%x", rnd[0:4], rnd[4:6], rnd[6:8], rnd[8:10], rnd[10:])
}
require.Equal(b, 36, len(res))
})
}
func Test_ConvertToBytes(t *testing.T) {
t.Parallel()
require.Equal(t, 0, ConvertToBytes(""))
require.Equal(t, 42, ConvertToBytes("42"))
require.Equal(t, 42, ConvertToBytes("42b"))
require.Equal(t, 42, ConvertToBytes("42B"))
require.Equal(t, 42, ConvertToBytes("42 b"))
require.Equal(t, 42, ConvertToBytes("42 B"))
require.Equal(t, 42*1000, ConvertToBytes("42k"))
require.Equal(t, 42*1000, ConvertToBytes("42K"))
require.Equal(t, 42*1000, ConvertToBytes("42kb"))
require.Equal(t, 42*1000, ConvertToBytes("42KB"))
require.Equal(t, 42*1000, ConvertToBytes("42 kb"))
require.Equal(t, 42*1000, ConvertToBytes("42 KB"))
require.Equal(t, 42*1000000, ConvertToBytes("42M"))
require.Equal(t, int(42.5*1000000), ConvertToBytes("42.5MB"))
require.Equal(t, 42*1000000000, ConvertToBytes("42G"))
require.Equal(t, 0, ConvertToBytes("string"))
require.Equal(t, 0, ConvertToBytes("MB"))
}
// go test -v -run=^$ -bench=Benchmark_ConvertToBytes -benchmem -count=2
func Benchmark_ConvertToBytes(b *testing.B) {
var res int
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ConvertToBytes("42B")
}
require.Equal(b, 42, res)
})
}

View File

@ -1,135 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"unsafe"
)
// #nosec G103
// UnsafeString returns a string pointer without allocation
func UnsafeString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// #nosec G103
// UnsafeBytes returns a byte pointer without allocation
func UnsafeBytes(s string) (bs []byte) {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return
}
// CopyString copies a string to make it immutable
func CopyString(s string) string {
return string(UnsafeBytes(s))
}
// CopyBytes copies a slice to make it immutable
func CopyBytes(b []byte) []byte {
tmp := make([]byte, len(b))
copy(tmp, b)
return tmp
}
const (
uByte = 1 << (10 * iota)
uKilobyte
uMegabyte
uGigabyte
uTerabyte
uPetabyte
uExabyte
)
// ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth.
// The unit that results in the smallest number greater than or equal to 1 is always chosen.
func ByteSize(bytes uint64) string {
unit := ""
value := float64(bytes)
switch {
case bytes >= uExabyte:
unit = "EB"
value /= uExabyte
case bytes >= uPetabyte:
unit = "PB"
value /= uPetabyte
case bytes >= uTerabyte:
unit = "TB"
value /= uTerabyte
case bytes >= uGigabyte:
unit = "GB"
value /= uGigabyte
case bytes >= uMegabyte:
unit = "MB"
value /= uMegabyte
case bytes >= uKilobyte:
unit = "KB"
value /= uKilobyte
case bytes >= uByte:
unit = "B"
default:
return "0B"
}
result := strconv.FormatFloat(value, 'f', 1, 64)
result = strings.TrimSuffix(result, ".0")
return result + unit
}
// ToString Change arg to string
func ToString(arg any, timeFormat ...string) string {
var tmp = reflect.Indirect(reflect.ValueOf(arg)).Interface()
switch v := tmp.(type) {
case int:
return strconv.Itoa(v)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.Itoa(int(v))
case uint8:
return strconv.FormatInt(int64(v), 10)
case uint16:
return strconv.FormatInt(int64(v), 10)
case uint32:
return strconv.FormatInt(int64(v), 10)
case uint64:
return strconv.FormatInt(int64(v), 10)
case string:
return v
case []byte:
return string(v)
case bool:
return strconv.FormatBool(v)
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case time.Time:
if len(timeFormat) > 0 {
return v.Format(timeFormat[0])
}
return v.Format("2006-01-02 15:04:05")
case reflect.Value:
return ToString(v.Interface(), timeFormat...)
case fmt.Stringer:
return v.String()
default:
return ""
}
}

View File

@ -1,85 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_UnsafeString(t *testing.T) {
t.Parallel()
res := UnsafeString([]byte("Hello, World!"))
require.Equal(t, "Hello, World!", res)
}
// go test -v -run=^$ -bench=UnsafeString -benchmem -count=2
func Benchmark_UnsafeString(b *testing.B) {
hello := []byte("Hello, World!")
var res string
b.Run("unsafe", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = UnsafeString(hello)
}
require.Equal(b, "Hello, World!", res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = string(hello)
}
require.Equal(b, "Hello, World!", res)
})
}
func Test_UnsafeBytes(t *testing.T) {
t.Parallel()
res := UnsafeBytes("Hello, World!")
require.Equal(t, []byte("Hello, World!"), res)
}
// go test -v -run=^$ -bench=UnsafeBytes -benchmem -count=4
func Benchmark_UnsafeBytes(b *testing.B) {
hello := "Hello, World!"
var res []byte
b.Run("unsafe", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = UnsafeBytes(hello)
}
require.Equal(b, []byte("Hello, World!"), res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = []byte(hello)
}
require.Equal(b, []byte("Hello, World!"), res)
})
}
func Test_CopyString(t *testing.T) {
t.Parallel()
res := CopyString("Hello, World!")
require.Equal(t, "Hello, World!", res)
}
func Test_ToString(t *testing.T) {
t.Parallel()
res := ToString([]byte("Hello, World!"))
require.Equal(t, "Hello, World!", res)
res = ToString(true)
require.Equal(t, "true", res)
res = ToString(uint(100))
require.Equal(t, "100", res)
}
// go test -v -run=^$ -bench=ToString -benchmem -count=2
func Benchmark_ToString(b *testing.B) {
hello := []byte("Hello, World!")
for n := 0; n < b.N; n++ {
ToString(hello)
}
}

View File

@ -1,242 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import "strings"
const MIMEOctetStream = "application/octet-stream"
// GetMIME returns the content-type of a file extension
func GetMIME(extension string) (mime string) {
if len(extension) == 0 {
return mime
}
if extension[0] == '.' {
mime = mimeExtensions[extension[1:]]
} else {
mime = mimeExtensions[extension]
}
if len(mime) == 0 {
return MIMEOctetStream
}
return mime
}
// ParseVendorSpecificContentType check if content type is vendor specific and
// if it is parsable to any known types. If its not vendor specific then returns
// the original content type.
func ParseVendorSpecificContentType(cType string) string {
plusIndex := strings.Index(cType, "+")
if plusIndex == -1 {
return cType
}
var parsableType string
if semiColonIndex := strings.Index(cType, ";"); semiColonIndex == -1 {
parsableType = cType[plusIndex+1:]
} else if plusIndex < semiColonIndex {
parsableType = cType[plusIndex+1 : semiColonIndex]
} else {
return cType[:semiColonIndex]
}
slashIndex := strings.Index(cType, "/")
if slashIndex == -1 {
return cType
}
return cType[0:slashIndex+1] + parsableType
}
// limits for HTTP statuscodes
const (
statusMessageMin = 100
statusMessageMax = 511
)
// StatusMessage returns the correct message for the provided HTTP statuscode
func StatusMessage(status int) string {
if status < statusMessageMin || status > statusMessageMax {
return ""
}
return statusMessage[status]
}
// HTTP status codes were copied from net/http.
var statusMessage = []string{
100: "Continue",
101: "Switching Protocols",
102: "Processing",
103: "Early Hints",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
207: "Multi-Status",
208: "Already Reported",
226: "IM Used",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
306: "Switch Proxy",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Request Entity Too Large",
414: "Request URI Too Long",
415: "Unsupported Media Type",
416: "Requested Range Not Satisfiable",
417: "Expectation Failed",
418: "I'm a teapot",
421: "Misdirected Request",
422: "Unprocessable Entity",
423: "Locked",
424: "Failed Dependency",
426: "Upgrade Required",
428: "Precondition Required",
429: "Too Many Requests",
431: "Request Header Fields Too Large",
451: "Unavailable For Legal Reasons",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
510: "Not Extended",
511: "Network Authentication Required",
}
// MIME types were copied from https://github.com/nginx/nginx/blob/master/conf/mime.types
var mimeExtensions = map[string]string{
"html": "text/html",
"htm": "text/html",
"shtml": "text/html",
"css": "text/css",
"gif": "image/gif",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"xml": "application/xml",
"js": "application/javascript",
"atom": "application/atom+xml",
"rss": "application/rss+xml",
"mml": "text/mathml",
"txt": "text/plain",
"jad": "text/vnd.sun.j2me.app-descriptor",
"wml": "text/vnd.wap.wml",
"htc": "text/x-component",
"png": "image/png",
"svg": "image/svg+xml",
"svgz": "image/svg+xml",
"tif": "image/tiff",
"tiff": "image/tiff",
"wbmp": "image/vnd.wap.wbmp",
"webp": "image/webp",
"ico": "image/x-icon",
"jng": "image/x-jng",
"bmp": "image/x-ms-bmp",
"woff": "font/woff",
"woff2": "font/woff2",
"jar": "application/java-archive",
"war": "application/java-archive",
"ear": "application/java-archive",
"json": "application/json",
"hqx": "application/mac-binhex40",
"doc": "application/msword",
"pdf": "application/pdf",
"ps": "application/postscript",
"eps": "application/postscript",
"ai": "application/postscript",
"rtf": "application/rtf",
"m3u8": "application/vnd.apple.mpegurl",
"kml": "application/vnd.google-earth.kml+xml",
"kmz": "application/vnd.google-earth.kmz",
"xls": "application/vnd.ms-excel",
"eot": "application/vnd.ms-fontobject",
"ppt": "application/vnd.ms-powerpoint",
"odg": "application/vnd.oasis.opendocument.graphics",
"odp": "application/vnd.oasis.opendocument.presentation",
"ods": "application/vnd.oasis.opendocument.spreadsheet",
"odt": "application/vnd.oasis.opendocument.text",
"wmlc": "application/vnd.wap.wmlc",
"7z": "application/x-7z-compressed",
"cco": "application/x-cocoa",
"jardiff": "application/x-java-archive-diff",
"jnlp": "application/x-java-jnlp-file",
"run": "application/x-makeself",
"pl": "application/x-perl",
"pm": "application/x-perl",
"prc": "application/x-pilot",
"pdb": "application/x-pilot",
"rar": "application/x-rar-compressed",
"rpm": "application/x-redhat-package-manager",
"sea": "application/x-sea",
"swf": "application/x-shockwave-flash",
"sit": "application/x-stuffit",
"tcl": "application/x-tcl",
"tk": "application/x-tcl",
"der": "application/x-x509-ca-cert",
"pem": "application/x-x509-ca-cert",
"crt": "application/x-x509-ca-cert",
"xpi": "application/x-xpinstall",
"xhtml": "application/xhtml+xml",
"xspf": "application/xspf+xml",
"zip": "application/zip",
"bin": "application/octet-stream",
"exe": "application/octet-stream",
"dll": "application/octet-stream",
"deb": "application/octet-stream",
"dmg": "application/octet-stream",
"iso": "application/octet-stream",
"img": "application/octet-stream",
"msi": "application/octet-stream",
"msp": "application/octet-stream",
"msm": "application/octet-stream",
"mid": "audio/midi",
"midi": "audio/midi",
"kar": "audio/midi",
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"m4a": "audio/x-m4a",
"ra": "audio/x-realaudio",
"3gpp": "video/3gpp",
"3gp": "video/3gpp",
"ts": "video/mp2t",
"mp4": "video/mp4",
"mpeg": "video/mpeg",
"mpg": "video/mpeg",
"mov": "video/quicktime",
"webm": "video/webm",
"flv": "video/x-flv",
"m4v": "video/x-m4v",
"mng": "video/x-mng",
"asx": "video/x-ms-asf",
"asf": "video/x-ms-asf",
"wmv": "video/x-ms-wmv",
"avi": "video/x-msvideo",
}

View File

@ -1,142 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"mime"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func Test_GetMIME(t *testing.T) {
t.Parallel()
res := GetMIME(".json")
require.Equal(t, "application/json", res)
res = GetMIME(".xml")
require.Equal(t, "application/xml", res)
res = GetMIME("xml")
require.Equal(t, "application/xml", res)
res = GetMIME("unknown")
require.Equal(t, MIMEOctetStream, res)
// empty case
res = GetMIME("")
require.Equal(t, "", res)
}
// go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2
func Benchmark_GetMIME(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = GetMIME(".xml")
res = GetMIME(".txt")
res = GetMIME(".png")
res = GetMIME(".exe")
res = GetMIME(".json")
}
require.Equal(b, "application/json", res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = mime.TypeByExtension(".xml")
res = mime.TypeByExtension(".txt")
res = mime.TypeByExtension(".png")
res = mime.TypeByExtension(".exe")
res = mime.TypeByExtension(".json")
}
require.Equal(b, "application/json", res)
})
}
func Test_ParseVendorSpecificContentType(t *testing.T) {
t.Parallel()
cType := ParseVendorSpecificContentType("application/json")
require.Equal(t, "application/json", cType)
cType = ParseVendorSpecificContentType("multipart/form-data; boundary=dart-http-boundary-ZnVy.ICWq+7HOdsHqWxCFa8g3D.KAhy+Y0sYJ_lBADypu8po3_X")
require.Equal(t, "multipart/form-data", cType)
cType = ParseVendorSpecificContentType("multipart/form-data")
require.Equal(t, "multipart/form-data", cType)
cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1")
require.Equal(t, "application/json", cType)
cType = ParseVendorSpecificContentType("application/vnd.api+json")
require.Equal(t, "application/json", cType)
cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded")
require.Equal(t, "application/x-www-form-urlencoded", cType)
cType = ParseVendorSpecificContentType("something invalid")
require.Equal(t, "something invalid", cType)
}
func Benchmark_ParseVendorSpecificContentType(b *testing.B) {
var cType string
b.Run("vendorContentType", func(b *testing.B) {
for n := 0; n < b.N; n++ {
cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1")
}
require.Equal(b, "application/json", cType)
})
b.Run("defaultContentType", func(b *testing.B) {
for n := 0; n < b.N; n++ {
cType = ParseVendorSpecificContentType("application/json")
}
require.Equal(b, "application/json", cType)
})
}
func Test_StatusMessage(t *testing.T) {
t.Parallel()
res := StatusMessage(204)
require.Equal(t, "No Content", res)
res = StatusMessage(404)
require.Equal(t, "Not Found", res)
res = StatusMessage(426)
require.Equal(t, "Upgrade Required", res)
res = StatusMessage(511)
require.Equal(t, "Network Authentication Required", res)
res = StatusMessage(1337)
require.Equal(t, "", res)
res = StatusMessage(-1)
require.Equal(t, "", res)
res = StatusMessage(0)
require.Equal(t, "", res)
res = StatusMessage(600)
require.Equal(t, "", res)
}
// go test -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=2
func Benchmark_StatusMessage(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = StatusMessage(http.StatusNotExtended)
}
require.Equal(b, "Not Extended", res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = http.StatusText(http.StatusNotExtended)
}
require.Equal(b, "Not Extended", res)
})
}

View File

@ -1,9 +0,0 @@
package utils
// JSONMarshal returns the JSON encoding of v.
type JSONMarshal func(v any) ([]byte, error)
// JSONUnmarshal parses the JSON-encoded data and stores the result
// in the value pointed to by v. If v is nil or not a pointer,
// Unmarshal returns an InvalidUnmarshalError.
type JSONUnmarshal func(data []byte, v any) error

View File

@ -1,60 +0,0 @@
package utils
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
type sampleStructure struct {
ImportantString string `json:"important_string"`
}
func Test_GolangJSONEncoder(t *testing.T) {
t.Parallel()
var (
ss = &sampleStructure{
ImportantString: "Hello World",
}
importantString = `{"important_string":"Hello World"}`
jsonEncoder JSONMarshal = json.Marshal
)
raw, err := jsonEncoder(ss)
require.NoError(t, err)
require.Equal(t, string(raw), importantString)
}
func Test_DefaultJSONEncoder(t *testing.T) {
t.Parallel()
var (
ss = &sampleStructure{
ImportantString: "Hello World",
}
importantString = `{"important_string":"Hello World"}`
jsonEncoder JSONMarshal = json.Marshal
)
raw, err := jsonEncoder(ss)
require.NoError(t, err)
require.Equal(t, string(raw), importantString)
}
func Test_DefaultJSONDecoder(t *testing.T) {
t.Parallel()
var (
ss sampleStructure
importantString = []byte(`{"important_string":"Hello World"}`)
jsonDecoder JSONUnmarshal = json.Unmarshal
)
err := jsonDecoder(importantString, &ss)
require.NoError(t, err)
require.Equal(t, "Hello World", ss.ImportantString)
}

View File

@ -1,27 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
// ToLower converts ascii string to lower-case
func ToLower(b string) string {
res := make([]byte, len(b))
copy(res, b)
for i := 0; i < len(res); i++ {
res[i] = toLowerTable[res[i]]
}
return UnsafeString(res)
}
// ToUpper converts ascii string to upper-case
func ToUpper(b string) string {
res := make([]byte, len(b))
copy(res, b)
for i := 0; i < len(res); i++ {
res[i] = toUpperTable[res[i]]
}
return UnsafeString(res)
}

View File

@ -1,64 +0,0 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_ToUpper(t *testing.T) {
t.Parallel()
require.Equal(t, "/MY/NAME/IS/:PARAM/*", ToUpper("/my/name/is/:param/*"))
}
const (
largeStr = "/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts/RePos/GoFiBer/FibEr/iSsues/CoMmEnts"
upperStr = "/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS/REPOS/GOFIBER/FIBER/ISSUES/COMMENTS"
lowerStr = "/repos/gofiber/fiber/issues/187643/comments/repos/gofiber/fiber/issues/comments"
)
func Benchmark_ToUpper(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ToUpper(largeStr)
}
require.Equal(b, upperStr, res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = strings.ToUpper(largeStr)
}
require.Equal(b, upperStr, res)
})
}
func Test_ToLower(t *testing.T) {
t.Parallel()
require.Equal(t, "/my/name/is/:param/*", ToLower("/MY/NAME/IS/:PARAM/*"))
require.Equal(t, "/my1/name/is/:param/*", ToLower("/MY1/NAME/IS/:PARAM/*"))
require.Equal(t, "/my2/name/is/:param/*", ToLower("/MY2/NAME/IS/:PARAM/*"))
require.Equal(t, "/my3/name/is/:param/*", ToLower("/MY3/NAME/IS/:PARAM/*"))
require.Equal(t, "/my4/name/is/:param/*", ToLower("/MY4/NAME/IS/:PARAM/*"))
}
func Benchmark_ToLower(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ToLower(largeStr)
}
require.Equal(b, lowerStr, res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = strings.ToLower(largeStr)
}
require.Equal(b, lowerStr, res)
})
}

View File

@ -1,4 +0,0 @@
package utils
// XMLMarshal returns the XML encoding of v.
type XMLMarshal func(v any) ([]byte, error)

View File

@ -1,61 +0,0 @@
package utils
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/require"
)
type serversXMLStructure struct {
XMLName xml.Name `xml:"servers"`
Version string `xml:"version,attr"`
Servers []serverXMLStructure `xml:"server"`
}
type serverXMLStructure struct {
XMLName xml.Name `xml:"server"`
Name string `xml:"name"`
}
var xmlString = `<servers version="1"><server><name>fiber one</name></server><server><name>fiber two</name></server></servers>`
func Test_GolangXMLEncoder(t *testing.T) {
t.Parallel()
var (
ss = &serversXMLStructure{
Version: "1",
Servers: []serverXMLStructure{
{Name: "fiber one"},
{Name: "fiber two"},
},
}
xmlEncoder XMLMarshal = xml.Marshal
)
raw, err := xmlEncoder(ss)
require.NoError(t, err)
require.Equal(t, string(raw), xmlString)
}
func Test_DefaultXMLEncoder(t *testing.T) {
t.Parallel()
var (
ss = &serversXMLStructure{
Version: "1",
Servers: []serverXMLStructure{
{Name: "fiber one"},
{Name: "fiber two"},
},
}
xmlEncoder XMLMarshal = xml.Marshal
)
raw, err := xmlEncoder(ss)
require.NoError(t, err)
require.Equal(t, string(raw), xmlString)
}