mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-14 05:11:17 +08:00
22 lines
709 B
Go
22 lines
709 B
Go
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 {
|
|
b := unsafe.StringData(s)
|
|
return unsafe.Slice(b, len(s))
|
|
}
|
|
|
|
// 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 {
|
|
if len(bs) == 0 {
|
|
return ""
|
|
}
|
|
return unsafe.String(&bs[0], len(bs))
|
|
}
|