Skip to content

Commit

Permalink
♻️ Refactor: remove use of deprecated functions (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcen committed Aug 6, 2021
1 parent 9a7b08e commit 15987a2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ func New(config ...Config) *App {
},
// Create config
config: Config{},
getBytes: utils.GetBytes,
getString: utils.GetString,
getBytes: utils.UnsafeBytes,
getString: utils.UnsafeString,
}
// Override config if provided
if len(config) > 0 {
Expand Down
8 changes: 4 additions & 4 deletions utils/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func Benchmark_ToLowerBytes(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ToLowerBytes(path)
}
AssertEqual(b, bytes.Equal(GetBytes("/repos/gofiber/fiber/issues/187643/comments"), res), true)
AssertEqual(b, bytes.Equal(UnsafeBytes("/repos/gofiber/fiber/issues/187643/comments"), res), true)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = bytes.ToLower(path)
}
AssertEqual(b, bytes.Equal(GetBytes("/repos/gofiber/fiber/issues/187643/comments"), res), true)
AssertEqual(b, bytes.Equal(UnsafeBytes("/repos/gofiber/fiber/issues/187643/comments"), res), true)
})
}

Expand All @@ -63,13 +63,13 @@ func Benchmark_ToUpperBytes(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ToUpperBytes(path)
}
AssertEqual(b, bytes.Equal(GetBytes("/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS"), res), true)
AssertEqual(b, bytes.Equal(UnsafeBytes("/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS"), res), true)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = bytes.ToUpper(path)
}
AssertEqual(b, bytes.Equal(GetBytes("/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS"), res), true)
AssertEqual(b, bytes.Equal(UnsafeBytes("/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS"), res), true)
})
}

Expand Down
2 changes: 1 addition & 1 deletion utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func UUID() string {
b[23] = '-'
hex.Encode(b[24:], uuid[10:16])

return GetString(b)
return UnsafeString(b)
}

// UUIDv4 returns a Random (Version 4) UUID.
Expand Down
4 changes: 2 additions & 2 deletions utils/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

// #nosec G103
// GetString returns a string pointer without allocation
// UnsafeString returns a string pointer without allocation
func UnsafeString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// #nosec G103
// GetBytes returns a byte pointer without allocation
// 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))
Expand Down
24 changes: 12 additions & 12 deletions utils/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ package utils

import "testing"

func Test_GetString(t *testing.T) {
func Test_UnsafeString(t *testing.T) {
t.Parallel()
res := GetString([]byte("Hello, World!"))
res := UnsafeString([]byte("Hello, World!"))
AssertEqual(t, "Hello, World!", res)
}

// go test -v -run=^$ -bench=GetString -benchmem -count=2
// go test -v -run=^$ -bench=UnsafeString -benchmem -count=2

func Benchmark_GetString(b *testing.B) {
func Benchmark_UnsafeString(b *testing.B) {
var hello = []byte("Hello, World!")
var res string
b.Run("unsafe", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = GetString(hello)
res = UnsafeString(hello)
}
AssertEqual(b, "Hello, World!", res)
})
Expand All @@ -31,20 +31,20 @@ func Benchmark_GetString(b *testing.B) {
})
}

func Test_GetBytes(t *testing.T) {
func Test_UnsafeBytes(t *testing.T) {
t.Parallel()
res := GetBytes("Hello, World!")
res := UnsafeBytes("Hello, World!")
AssertEqual(t, []byte("Hello, World!"), res)
}

// go test -v -run=^$ -bench=GetBytes -benchmem -count=4
// go test -v -run=^$ -bench=UnsafeBytes -benchmem -count=4

func Benchmark_GetBytes(b *testing.B) {
func Benchmark_UnsafeBytes(b *testing.B) {
var hello = "Hello, World!"
var res []byte
b.Run("unsafe", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = GetBytes(hello)
res = UnsafeBytes(hello)
}
AssertEqual(b, []byte("Hello, World!"), res)
})
Expand All @@ -56,8 +56,8 @@ func Benchmark_GetBytes(b *testing.B) {
})
}

func Test_ImmutableString(t *testing.T) {
func Test_CopyString(t *testing.T) {
t.Parallel()
res := ImmutableString("Hello, World!")
res := CopyString("Hello, World!")
AssertEqual(t, "Hello, World!", res)
}
4 changes: 2 additions & 2 deletions utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ToLower(b string) string {
res[i] = toLowerTable[res[i]]
}

return GetString(res)
return UnsafeString(res)
}

// ToUpper is the equivalent of strings.ToUpper
Expand All @@ -23,7 +23,7 @@ func ToUpper(b string) string {
res[i] = toUpperTable[res[i]]
}

return GetString(res)
return UnsafeString(res)
}

// TrimLeft is the equivalent of strings.TrimLeft
Expand Down

0 comments on commit 15987a2

Please sign in to comment.