github.com/hdt3213/godis@v1.2.9/lib/utils/utils.go (about)

     1  package utils
     2  
     3  // ToCmdLine convert strings to [][]byte
     4  func ToCmdLine(cmd ...string) [][]byte {
     5  	args := make([][]byte, len(cmd))
     6  	for i, s := range cmd {
     7  		args[i] = []byte(s)
     8  	}
     9  	return args
    10  }
    11  
    12  // ToCmdLine2 convert commandName and string-type argument to [][]byte
    13  func ToCmdLine2(commandName string, args ...string) [][]byte {
    14  	result := make([][]byte, len(args)+1)
    15  	result[0] = []byte(commandName)
    16  	for i, s := range args {
    17  		result[i+1] = []byte(s)
    18  	}
    19  	return result
    20  }
    21  
    22  // ToCmdLine3 convert commandName and []byte-type argument to CmdLine
    23  func ToCmdLine3(commandName string, args ...[]byte) [][]byte {
    24  	result := make([][]byte, len(args)+1)
    25  	result[0] = []byte(commandName)
    26  	for i, s := range args {
    27  		result[i+1] = s
    28  	}
    29  	return result
    30  }
    31  
    32  // Equals check whether the given value is equal
    33  func Equals(a interface{}, b interface{}) bool {
    34  	sliceA, okA := a.([]byte)
    35  	sliceB, okB := b.([]byte)
    36  	if okA && okB {
    37  		return BytesEquals(sliceA, sliceB)
    38  	}
    39  	return a == b
    40  }
    41  
    42  // BytesEquals check whether the given bytes is equal
    43  func BytesEquals(a []byte, b []byte) bool {
    44  	if (a == nil && b != nil) || (a != nil && b == nil) {
    45  		return false
    46  	}
    47  	if len(a) != len(b) {
    48  		return false
    49  	}
    50  	size := len(a)
    51  	for i := 0; i < size; i++ {
    52  		av := a[i]
    53  		bv := b[i]
    54  		if av != bv {
    55  			return false
    56  		}
    57  	}
    58  	return true
    59  }
    60  
    61  // ConvertRange converts redis index to go slice index
    62  // -1 => size-1
    63  // both inclusive [0, 10] => left inclusive right exclusive [0, 9)
    64  // out of bound to max inbound [size, size+1] => [-1, -1]
    65  func ConvertRange(start int64, end int64, size int64) (int, int) {
    66  	if start < -size {
    67  		return -1, -1
    68  	} else if start < 0 {
    69  		start = size + start
    70  	} else if start >= size {
    71  		return -1, -1
    72  	}
    73  	if end < -size {
    74  		return -1, -1
    75  	} else if end < 0 {
    76  		end = size + end + 1
    77  	} else if end < size {
    78  		end = end + 1
    79  	} else {
    80  		end = size
    81  	}
    82  	if start > end {
    83  		return -1, -1
    84  	}
    85  	return int(start), int(end)
    86  }