From 2b845f2615d504c22a4e6aab3db1a8e7461176fc Mon Sep 17 00:00:00 2001 From: David Chase Date: Tue, 28 Nov 2023 11:15:38 -0500 Subject: [PATCH] update unsafe code to user newer-faster-better idioms --- util/{util_unsafe.go => util_unsafe_go120.go} | 4 ++-- util/util_unsafe_go121.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) rename util/{util_unsafe.go => util_unsafe_go120.go} (87%) create mode 100644 util/util_unsafe_go121.go diff --git a/util/util_unsafe.go b/util/util_unsafe_go120.go similarity index 87% rename from util/util_unsafe.go rename to util/util_unsafe_go120.go index 840007a..d6be534 100644 --- a/util/util_unsafe.go +++ b/util/util_unsafe_go120.go @@ -1,5 +1,5 @@ -//go:build !appengine && !js -// +build !appengine,!js +//go:build !appengine && !js && !go1.21 +// +build !appengine,!js,!go1.21 package util diff --git a/util/util_unsafe_go121.go b/util/util_unsafe_go121.go new file mode 100644 index 0000000..50c7fce --- /dev/null +++ b/util/util_unsafe_go121.go @@ -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)) +}