forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c44a9aa
commit ef220d0
Showing
2 changed files
with
147 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters