Skip to content

Commit

Permalink
optimize parseUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
horpto authored and jackc committed Sep 23, 2023
1 parent 0ef38d9 commit 26a2115
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,29 @@ func (src *UUID) AssignTo(dst interface{}) error {

// parseUUID converts a string UUID in standard form to a byte array.
func parseUUID(src string) (dst [16]byte, err error) {
var uuidBuf [32]byte
srcBuf := uuidBuf[:]

switch len(src) {
case 36:
src = src[0:8] + src[9:13] + src[14:18] + src[19:23] + src[24:]
copy(srcBuf[0:8], src[:8])
copy(srcBuf[8:12], src[9:13])
copy(srcBuf[12:16], src[14:18])
copy(srcBuf[16:20], src[19:23])
copy(srcBuf[20:], src[24:])
case 32:
// dashes already stripped, assume valid
copy(srcBuf, src)

default:
// assume invalid.
return dst, fmt.Errorf("cannot parse UUID %v", src)
}

buf, err := hex.DecodeString(src)
_, err = hex.Decode(dst[:], srcBuf)
if err != nil {
return dst, err
}

copy(dst[:], buf)
return dst, err
}

Expand Down

0 comments on commit 26a2115

Please sign in to comment.