mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-06 01:07:06 +08:00
87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package utils
|
|
|
|
// ToCmdLine convert strings to [][]byte
|
|
func ToCmdLine(cmd ...string) [][]byte {
|
|
args := make([][]byte, len(cmd))
|
|
for i, s := range cmd {
|
|
args[i] = []byte(s)
|
|
}
|
|
return args
|
|
}
|
|
|
|
// ToCmdLine2 convert commandName and string-type argument to [][]byte
|
|
func ToCmdLine2(commandName string, args ...string) [][]byte {
|
|
result := make([][]byte, len(args)+1)
|
|
result[0] = []byte(commandName)
|
|
for i, s := range args {
|
|
result[i+1] = []byte(s)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// ToCmdLine3 convert commandName and []byte-type argument to CmdLine
|
|
func ToCmdLine3(commandName string, args ...[]byte) [][]byte {
|
|
result := make([][]byte, len(args)+1)
|
|
result[0] = []byte(commandName)
|
|
for i, s := range args {
|
|
result[i+1] = s
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Equals check whether the given value is equal
|
|
func Equals(a interface{}, b interface{}) bool {
|
|
sliceA, okA := a.([]byte)
|
|
sliceB, okB := b.([]byte)
|
|
if okA && okB {
|
|
return BytesEquals(sliceA, sliceB)
|
|
}
|
|
return a == b
|
|
}
|
|
|
|
// BytesEquals check whether the given bytes is equal
|
|
func BytesEquals(a []byte, b []byte) bool {
|
|
if (a == nil && b != nil) || (a != nil && b == nil) {
|
|
return false
|
|
}
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
size := len(a)
|
|
for i := 0; i < size; i++ {
|
|
av := a[i]
|
|
bv := b[i]
|
|
if av != bv {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ConvertRange converts redis index to go slice index
|
|
// -1 => size-1
|
|
// both inclusive [0, 10] => left inclusive right exclusive [0, 9)
|
|
// out of bound to max inbound [size, size+1] => [-1, -1]
|
|
func ConvertRange(start int64, end int64, size int64) (int, int) {
|
|
if start < -size {
|
|
return -1, -1
|
|
} else if start < 0 {
|
|
start = size + start
|
|
} else if start >= size {
|
|
return -1, -1
|
|
}
|
|
if end < -size {
|
|
return -1, -1
|
|
} else if end < 0 {
|
|
end = size + end + 1
|
|
} else if end < size {
|
|
end = end + 1
|
|
} else {
|
|
end = size
|
|
}
|
|
if start > end {
|
|
return -1, -1
|
|
}
|
|
return int(start), int(end)
|
|
}
|