forked from mirror/redis
148 lines
2.3 KiB
Go
148 lines
2.3 KiB
Go
package internal
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func AppendArg(b []byte, v interface{}) []byte {
|
|
switch v := v.(type) {
|
|
case nil:
|
|
return append(b, "<nil>"...)
|
|
case string:
|
|
return appendUTF8String(b, Bytes(v))
|
|
case []byte:
|
|
return appendUTF8String(b, v)
|
|
case int:
|
|
return strconv.AppendInt(b, int64(v), 10)
|
|
case int8:
|
|
return strconv.AppendInt(b, int64(v), 10)
|
|
case int16:
|
|
return strconv.AppendInt(b, int64(v), 10)
|
|
case int32:
|
|
return strconv.AppendInt(b, int64(v), 10)
|
|
case int64:
|
|
return strconv.AppendInt(b, v, 10)
|
|
case uint:
|
|
return strconv.AppendUint(b, uint64(v), 10)
|
|
case uint8:
|
|
return strconv.AppendUint(b, uint64(v), 10)
|
|
case uint16:
|
|
return strconv.AppendUint(b, uint64(v), 10)
|
|
case uint32:
|
|
return strconv.AppendUint(b, uint64(v), 10)
|
|
case uint64:
|
|
return strconv.AppendUint(b, v, 10)
|
|
case float32:
|
|
return strconv.AppendFloat(b, float64(v), 'f', -1, 64)
|
|
case float64:
|
|
return strconv.AppendFloat(b, v, 'f', -1, 64)
|
|
case bool:
|
|
if v {
|
|
return append(b, "true"...)
|
|
}
|
|
return append(b, "false"...)
|
|
case time.Time:
|
|
return v.AppendFormat(b, time.RFC3339Nano)
|
|
default:
|
|
return append(b, fmt.Sprint(v)...)
|
|
}
|
|
}
|
|
|
|
func appendUTF8String(dst []byte, src []byte) []byte {
|
|
if isSimple(src) {
|
|
dst = append(dst, src...)
|
|
return dst
|
|
}
|
|
|
|
s := len(dst)
|
|
dst = append(dst, make([]byte, hex.EncodedLen(len(src)))...)
|
|
hex.Encode(dst[s:], src)
|
|
return dst
|
|
}
|
|
|
|
func isSimple(b []byte) bool {
|
|
for _, c := range b {
|
|
if !isSimpleByte(c) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func isSimpleByte(c byte) bool {
|
|
return simple[c]
|
|
}
|
|
|
|
var simple = [256]bool{
|
|
'-': true,
|
|
'_': true,
|
|
|
|
'0': true,
|
|
'1': true,
|
|
'2': true,
|
|
'3': true,
|
|
'4': true,
|
|
'5': true,
|
|
'6': true,
|
|
'7': true,
|
|
'8': true,
|
|
'9': true,
|
|
|
|
'a': true,
|
|
'b': true,
|
|
'c': true,
|
|
'd': true,
|
|
'e': true,
|
|
'f': true,
|
|
'g': true,
|
|
'h': true,
|
|
'i': true,
|
|
'j': true,
|
|
'k': true,
|
|
'l': true,
|
|
'm': true,
|
|
'n': true,
|
|
'o': true,
|
|
'p': true,
|
|
'q': true,
|
|
'r': true,
|
|
's': true,
|
|
't': true,
|
|
'u': true,
|
|
'v': true,
|
|
'w': true,
|
|
'x': true,
|
|
'y': true,
|
|
'z': true,
|
|
|
|
'A': true,
|
|
'B': true,
|
|
'C': true,
|
|
'D': true,
|
|
'E': true,
|
|
'F': true,
|
|
'G': true,
|
|
'H': true,
|
|
'I': true,
|
|
'J': true,
|
|
'K': true,
|
|
'L': true,
|
|
'M': true,
|
|
'N': true,
|
|
'O': true,
|
|
'P': true,
|
|
'Q': true,
|
|
'R': true,
|
|
'S': true,
|
|
'T': true,
|
|
'U': true,
|
|
'V': true,
|
|
'W': true,
|
|
'X': true,
|
|
'Y': true,
|
|
'Z': true,
|
|
}
|