Skip to content

Commit

Permalink
Tweak append arg
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 5, 2020
1 parent c44a9aa commit ef220d0
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 74 deletions.
147 changes: 147 additions & 0 deletions internal/arg.go
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,
}
74 changes: 0 additions & 74 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package internal

import (
"context"
"fmt"
"strconv"
"time"
"unicode/utf8"

"github.com/go-redis/redis/v8/internal/proto"
"github.com/go-redis/redis/v8/internal/util"
Expand Down Expand Up @@ -63,77 +60,6 @@ func Unwrap(err error) error {
return u.Unwrap()
}

func AppendArg(b []byte, v interface{}) []byte {
switch v := v.(type) {
case nil:
return append(b, "<nil>"...)
case string:
return appendUTF8String(b, v)
case []byte:
return appendUTF8String(b, String(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(b []byte, s string) []byte {
for _, r := range s {
b = appendRune(b, r)
}
return b
}

func appendRune(b []byte, r rune) []byte {
if r < utf8.RuneSelf {
switch c := byte(r); c {
case '\n':
return append(b, "\\n"...)
case '\r':
return append(b, "\\r"...)
default:
return append(b, c)
}
}

l := len(b)
b = append(b, make([]byte, utf8.UTFMax)...)
n := utf8.EncodeRune(b[l:l+utf8.UTFMax], r)
b = b[:l+n]

return b
}

//------------------------------------------------------------------------------

func WithSpan(ctx context.Context, name string, fn func(context.Context) error) error {
Expand Down

0 comments on commit ef220d0

Please sign in to comment.