mirror of
https://github.com/google/uuid.git
synced 2024-11-24 08:32:23 +02:00
do not use fmt.Sprintf() in UUID.String()
A single array is used as a buffer and "encoding/hex".Encode() hex encodes directly its final destination. Loops are avoided because they are simple enough to unroll manually.
This commit is contained in:
parent
4e1e17316e
commit
71fb85a64c
15
uuid.go
15
uuid.go
@ -7,6 +7,7 @@ package uuid
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
@ -81,9 +82,17 @@ func (uuid UUID) String() string {
|
||||
if uuid == nil || len(uuid) != 16 {
|
||||
return ""
|
||||
}
|
||||
b := []byte(uuid)
|
||||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
|
||||
b[:4], b[4:6], b[6:8], b[8:10], b[10:])
|
||||
var buf [36]byte
|
||||
hex.Encode(buf[:], uuid[:4])
|
||||
buf[8] = '-'
|
||||
hex.Encode(buf[9:13], uuid[4:6])
|
||||
buf[13] = '-'
|
||||
hex.Encode(buf[14:18], uuid[6:8])
|
||||
buf[18] = '-'
|
||||
hex.Encode(buf[19:23], uuid[8:10])
|
||||
buf[23] = '-'
|
||||
hex.Encode(buf[24:], uuid[10:])
|
||||
return string(buf[:])
|
||||
}
|
||||
|
||||
// URN returns the RFC 2141 URN form of uuid,
|
||||
|
Loading…
Reference in New Issue
Block a user