Skip to content

Commit

Permalink
optimize encodeUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
horpto authored and jackc committed Sep 23, 2023
1 parent 9df8472 commit 0ef38d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion database_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func DatabaseSQLValue(ci *ConnInfo, src Value) (interface{}, error) {
}

func EncodeValueText(src TextEncoder) (interface{}, error) {
buf, err := src.EncodeText(nil, make([]byte, 0, 32))
var encBuf [36]byte
buf, err := src.EncodeText(nil, encBuf[:])
if err != nil {
return nil, err
}
Expand Down
21 changes: 17 additions & 4 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (src *UUID) AssignTo(dst interface{}) error {
copy(*v, src.Bytes[:])
return nil
case *string:
*v = encodeUUID(src.Bytes)
*v = string(encodeUUID(src.Bytes))
return nil
default:
if nextDst, retry := GetAssignToDstType(v); retry {
Expand Down Expand Up @@ -120,8 +120,21 @@ func parseUUID(src string) (dst [16]byte, err error) {
}

// encodeUUID converts a uuid byte array to UUID standard string form.
func encodeUUID(src [16]byte) string {
return fmt.Sprintf("%x-%x-%x-%x-%x", src[0:4], src[4:6], src[6:8], src[8:10], src[10:16])
func encodeUUID(src [16]byte) (dst []byte) {
var buf [36]byte
dst = buf[:]

hex.Encode(dst, src[:4])
buf[8] = '-'
hex.Encode(dst[9:13], src[4:6])
buf[13] = '-'
hex.Encode(dst[14:18], src[6:8])
buf[18] = '-'
hex.Encode(dst[19:23], src[8:10])
buf[23] = '-'
hex.Encode(dst[24:], src[10:])

return
}

func (dst *UUID) DecodeText(ci *ConnInfo, src []byte) error {
Expand Down Expand Up @@ -209,7 +222,7 @@ func (src UUID) MarshalJSON() ([]byte, error) {
case Present:
var buff bytes.Buffer
buff.WriteByte('"')
buff.WriteString(encodeUUID(src.Bytes))
buff.Write(encodeUUID(src.Bytes))
buff.WriteByte('"')
return buff.Bytes(), nil
case Null:
Expand Down

0 comments on commit 0ef38d9

Please sign in to comment.