2014-05-09 10:49:22 +04:00
|
|
|
package ledis
|
2014-05-09 05:17:28 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2014-05-14 12:35:49 +04:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
2014-05-09 05:17:28 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
var errIntNumber = errors.New("invalid integer")
|
|
|
|
|
2014-05-14 12:35:49 +04:00
|
|
|
// no copy to change slice to string
|
|
|
|
// use your own risk
|
|
|
|
func String(b []byte) (s string) {
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
|
|
pstring.Data = pbytes.Data
|
|
|
|
pstring.Len = pbytes.Len
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// no copy to change string to slice
|
|
|
|
// use your own risk
|
|
|
|
func Slice(s string) (b []byte) {
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
|
|
|
|
pbytes.Data = pstring.Data
|
|
|
|
pbytes.Len = pstring.Len
|
|
|
|
pbytes.Cap = pstring.Len
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func Int64(v []byte, err error) (int64, error) {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v == nil || len(v) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
} else if len(v) != 8 {
|
|
|
|
return 0, errIntNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(binary.LittleEndian.Uint64(v)), nil
|
|
|
|
}
|
|
|
|
|
2014-09-17 19:06:42 +04:00
|
|
|
func Uint64(v []byte, err error) (uint64, error) {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v == nil || len(v) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
} else if len(v) != 8 {
|
|
|
|
return 0, errIntNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return binary.LittleEndian.Uint64(v), nil
|
|
|
|
}
|
|
|
|
|
2014-05-09 05:17:28 +04:00
|
|
|
func PutInt64(v int64) []byte {
|
2014-05-14 12:35:49 +04:00
|
|
|
var b []byte
|
|
|
|
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
|
|
|
|
pbytes.Data = uintptr(unsafe.Pointer(&v))
|
|
|
|
pbytes.Len = 8
|
|
|
|
pbytes.Cap = 8
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func StrInt64(v []byte, err error) (int64, error) {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
} else {
|
|
|
|
return strconv.ParseInt(String(v), 10, 64)
|
|
|
|
}
|
2014-05-09 05:17:28 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
func StrUint64(v []byte, err error) (uint64, error) {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
} else {
|
|
|
|
return strconv.ParseUint(String(v), 10, 64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 13:10:44 +04:00
|
|
|
func StrInt32(v []byte, err error) (int32, error) {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
} else {
|
|
|
|
res, err := strconv.ParseInt(String(v), 10, 32)
|
|
|
|
return int32(res), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StrInt8(v []byte, err error) (int8, error) {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
} else {
|
2014-07-16 07:29:44 +04:00
|
|
|
res, err := strconv.ParseInt(String(v), 10, 8)
|
2014-07-08 13:10:44 +04:00
|
|
|
return int8(res), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 12:35:49 +04:00
|
|
|
func StrPutInt64(v int64) []byte {
|
2014-05-16 04:56:32 +04:00
|
|
|
return strconv.AppendInt(nil, v, 10)
|
2014-05-09 05:17:28 +04:00
|
|
|
}
|
2014-06-26 12:27:25 +04:00
|
|
|
|
2014-09-24 05:46:36 +04:00
|
|
|
func StrPutUint64(v uint64) []byte {
|
|
|
|
return strconv.AppendUint(nil, v, 10)
|
|
|
|
}
|
|
|
|
|
2014-06-26 12:27:25 +04:00
|
|
|
func MinUInt32(a uint32, b uint32) uint32 {
|
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
} else {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MaxUInt32(a uint32, b uint32) uint32 {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
} else {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MaxInt32(a int32, b int32) int32 {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
} else {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
2014-09-23 13:28:09 +04:00
|
|
|
|
|
|
|
func AsyncNotify(ch chan struct{}) {
|
|
|
|
select {
|
|
|
|
case ch <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|