mirror of https://github.com/go-sonic/sonic.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
933 B
Go
58 lines
933 B
Go
package util
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
// BytesToString converts byte slice to string without a memory allocation.
|
|
func BytesToString(b []byte) string {
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
}
|
|
|
|
// StringToBytes converts string to byte slice without a memory allocation.
|
|
func StringToBytes(s string) []byte {
|
|
return *(*[]byte)(unsafe.Pointer(
|
|
&struct {
|
|
string
|
|
Cap int
|
|
}{s, len(s)},
|
|
))
|
|
}
|
|
|
|
func TimePtr(t time.Time) *time.Time {
|
|
return &t
|
|
}
|
|
|
|
func BoolPtr(b bool) *bool {
|
|
return &b
|
|
}
|
|
|
|
func Int32Ptr(i int32) *int32 {
|
|
return &i
|
|
}
|
|
|
|
func Int64Ptr(i int64) *int64 {
|
|
return &i
|
|
}
|
|
|
|
func StringPtr(s string) *string {
|
|
return &s
|
|
}
|
|
|
|
func Md5Hex(str string) string {
|
|
hash := md5.Sum([]byte(str))
|
|
return hex.EncodeToString(hash[:])
|
|
}
|
|
|
|
func MapKeyToArray[K comparable, V any](m map[K]V) []K {
|
|
values := make([]K, 0, len(m))
|
|
|
|
for k := range m {
|
|
values = append(values, k)
|
|
}
|
|
return values
|
|
}
|