2023-06-06 10:47:50 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
// ImmutableBytesFromString is equivalent to []byte(s), except that it uses the
|
|
|
|
// same memory backing s instead of making a heap-allocated copy. This is only
|
|
|
|
// valid if the returned slice is never mutated.
|
|
|
|
func ImmutableBytesFromString(s string) []byte {
|
2023-07-02 09:59:18 +08:00
|
|
|
b := unsafe.StringData(s)
|
|
|
|
return unsafe.Slice(b, len(s))
|
2023-06-06 10:47:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// StringFromImmutableBytes is equivalent to string(bs), except that it uses
|
|
|
|
// the same memory backing bs instead of making a heap-allocated copy. This is
|
|
|
|
// only valid if bs is never mutated after StringFromImmutableBytes returns.
|
|
|
|
func StringFromImmutableBytes(bs []byte) string {
|
2023-07-02 09:59:18 +08:00
|
|
|
if len(bs) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return unsafe.String(&bs[0], len(bs))
|
2023-06-06 10:47:50 +08:00
|
|
|
}
|