Skip to content

Commit

Permalink
Merge pull request #432 from dr2chase/master
Browse files Browse the repository at this point in the history
update unsafe code to user newer-faster-better idioms
  • Loading branch information
yuin authored Oct 12, 2024
2 parents 3847ca2 + 2b845f2 commit 14d91f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions util/util_unsafe.go → util/util_unsafe_go120.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !appengine && !js
// +build !appengine,!js
//go:build !appengine && !js && !go1.21
// +build !appengine,!js,!go1.21

package util

Expand Down
18 changes: 18 additions & 0 deletions util/util_unsafe_go121.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build !appengine && !js && go1.21
// +build !appengine,!js,go1.21

package util

import (
"unsafe"
)

// BytesToReadOnlyString returns a string converted from given bytes.
func BytesToReadOnlyString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}

// StringToReadOnlyBytes returns bytes converted from given string.
func StringToReadOnlyBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}

0 comments on commit 14d91f9

Please sign in to comment.