Skip to content

Commit fe48793

Browse files

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

utils/convert.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@ import (
1010
"strconv"
1111
"strings"
1212
"time"
13-
"unsafe"
1413
)
1514

16-
// UnsafeString returns a string pointer without allocation
17-
//
18-
//nolint:gosec // unsafe is used for better performance here
19-
func UnsafeString(b []byte) string {
20-
return *(*string)(unsafe.Pointer(&b))
21-
}
22-
2315
// CopyString copies a string to make it immutable
2416
func CopyString(s string) string {
2517
return string(UnsafeBytes(s))

utils/convert_b2s_new.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build go1.20
2+
// +build go1.20
3+
4+
package utils
5+
6+
import (
7+
"unsafe"
8+
)
9+
10+
// UnsafeString returns a string pointer without allocation
11+
func UnsafeString(b []byte) string {
12+
return unsafe.String(unsafe.SliceData(b), len(b))
13+
}

utils/convert_b2s_old.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//go:build !go1.20
2+
// +build !go1.20
3+
4+
package utils
5+
6+
import (
7+
"unsafe"
8+
)
9+
10+
// UnsafeString returns a string pointer without allocation
11+
//
12+
//nolint:gosec // unsafe is used for better performance here
13+
func UnsafeString(b []byte) string {
14+
return *(*string)(unsafe.Pointer(&b))
15+
}

0 commit comments

Comments
 (0)