123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- package uuid
- import (
- "bytes"
- "crypto/rand"
- "encoding/hex"
- "errors"
- "fmt"
- "io"
- "strings"
- "sync"
- )
- type UUID [16]byte
- type Version byte
- type Variant byte
- const (
- Invalid = Variant(iota)
- RFC4122
- Reserved
- Microsoft
- Future
- )
- const randPoolSize = 16 * 16
- var (
- rander = rand.Reader
- poolEnabled = false
- poolMu sync.Mutex
- poolPos = randPoolSize
- pool [randPoolSize]byte
- )
- type invalidLengthError struct{ len int }
- func (err invalidLengthError) Error() string {
- return fmt.Sprintf("invalid UUID length: %d", err.len)
- }
- func IsInvalidLengthError(err error) bool {
- _, ok := err.(invalidLengthError)
- return ok
- }
- func Parse(s string) (UUID, error) {
- var uuid UUID
- switch len(s) {
-
- case 36:
-
- case 36 + 9:
- if strings.ToLower(s[:9]) != "urn:uuid:" {
- return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
- }
- s = s[9:]
-
- case 36 + 2:
- s = s[1:]
-
- case 32:
- var ok bool
- for i := range uuid {
- uuid[i], ok = xtob(s[i*2], s[i*2+1])
- if !ok {
- return uuid, errors.New("invalid UUID format")
- }
- }
- return uuid, nil
- default:
- return uuid, invalidLengthError{len(s)}
- }
-
-
- if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
- return uuid, errors.New("invalid UUID format")
- }
- for i, x := range [16]int{
- 0, 2, 4, 6,
- 9, 11,
- 14, 16,
- 19, 21,
- 24, 26, 28, 30, 32, 34} {
- v, ok := xtob(s[x], s[x+1])
- if !ok {
- return uuid, errors.New("invalid UUID format")
- }
- uuid[i] = v
- }
- return uuid, nil
- }
- func ParseBytes(b []byte) (UUID, error) {
- var uuid UUID
- switch len(b) {
- case 36:
- case 36 + 9:
- if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
- return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
- }
- b = b[9:]
- case 36 + 2:
- b = b[1:]
- case 32:
- var ok bool
- for i := 0; i < 32; i += 2 {
- uuid[i/2], ok = xtob(b[i], b[i+1])
- if !ok {
- return uuid, errors.New("invalid UUID format")
- }
- }
- return uuid, nil
- default:
- return uuid, invalidLengthError{len(b)}
- }
-
-
- if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
- return uuid, errors.New("invalid UUID format")
- }
- for i, x := range [16]int{
- 0, 2, 4, 6,
- 9, 11,
- 14, 16,
- 19, 21,
- 24, 26, 28, 30, 32, 34} {
- v, ok := xtob(b[x], b[x+1])
- if !ok {
- return uuid, errors.New("invalid UUID format")
- }
- uuid[i] = v
- }
- return uuid, nil
- }
- func MustParse(s string) UUID {
- uuid, err := Parse(s)
- if err != nil {
- panic(`uuid: Parse(` + s + `): ` + err.Error())
- }
- return uuid
- }
- func FromBytes(b []byte) (uuid UUID, err error) {
- err = uuid.UnmarshalBinary(b)
- return uuid, err
- }
- func Must(uuid UUID, err error) UUID {
- if err != nil {
- panic(err)
- }
- return uuid
- }
- func (uuid UUID) String() string {
- var buf [36]byte
- encodeHex(buf[:], uuid)
- return string(buf[:])
- }
- func (uuid UUID) URN() string {
- var buf [36 + 9]byte
- copy(buf[:], "urn:uuid:")
- encodeHex(buf[9:], uuid)
- return string(buf[:])
- }
- func encodeHex(dst []byte, uuid UUID) {
- hex.Encode(dst, uuid[:4])
- dst[8] = '-'
- hex.Encode(dst[9:13], uuid[4:6])
- dst[13] = '-'
- hex.Encode(dst[14:18], uuid[6:8])
- dst[18] = '-'
- hex.Encode(dst[19:23], uuid[8:10])
- dst[23] = '-'
- hex.Encode(dst[24:], uuid[10:])
- }
- func (uuid UUID) Variant() Variant {
- switch {
- case (uuid[8] & 0xc0) == 0x80:
- return RFC4122
- case (uuid[8] & 0xe0) == 0xc0:
- return Microsoft
- case (uuid[8] & 0xe0) == 0xe0:
- return Future
- default:
- return Reserved
- }
- }
- func (uuid UUID) Version() Version {
- return Version(uuid[6] >> 4)
- }
- func (v Version) String() string {
- if v > 15 {
- return fmt.Sprintf("BAD_VERSION_%d", v)
- }
- return fmt.Sprintf("VERSION_%d", v)
- }
- func (v Variant) String() string {
- switch v {
- case RFC4122:
- return "RFC4122"
- case Reserved:
- return "Reserved"
- case Microsoft:
- return "Microsoft"
- case Future:
- return "Future"
- case Invalid:
- return "Invalid"
- }
- return fmt.Sprintf("BadVariant%d", int(v))
- }
- func SetRand(r io.Reader) {
- if r == nil {
- rander = rand.Reader
- return
- }
- rander = r
- }
- func EnableRandPool() {
- poolEnabled = true
- }
- func DisableRandPool() {
- poolEnabled = false
- defer poolMu.Unlock()
- poolMu.Lock()
- poolPos = randPoolSize
- }
|