Skip to content

Commit 14d91f9

Browse files
authored
Merge pull request #432 from dr2chase/master
update unsafe code to user newer-faster-better idioms
2 parents 3847ca2 + 2b845f2 commit 14d91f9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

util/util_unsafe.go renamed to util/util_unsafe_go120.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !appengine && !js
2-
// +build !appengine,!js
1+
//go:build !appengine && !js && !go1.21
2+
// +build !appengine,!js,!go1.21
33

44
package util
55

util/util_unsafe_go121.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build !appengine && !js && go1.21
2+
// +build !appengine,!js,go1.21
3+
4+
package util
5+
6+
import (
7+
"unsafe"
8+
)
9+
10+
// BytesToReadOnlyString returns a string converted from given bytes.
11+
func BytesToReadOnlyString(b []byte) string {
12+
return unsafe.String(unsafe.SliceData(b), len(b))
13+
}
14+
15+
// StringToReadOnlyBytes returns bytes converted from given string.
16+
func StringToReadOnlyBytes(s string) []byte {
17+
return unsafe.Slice(unsafe.StringData(s), len(s))
18+
}

0 commit comments

Comments
 (0)