1 // Copyright 2016 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
21 // A Version represents a UUID's version.
24 // A Variant represents a UUID's variant.
27 // Constants returned by Variant.
29 Invalid = Variant(iota) // Invalid UUID
30 RFC4122 // The variant specified in RFC4122
31 Reserved // Reserved, NCS backward compatibility.
32 Microsoft // Reserved, Microsoft Corporation backward compatibility.
33 Future // Reserved for future definition.
36 var rander = rand.Reader // random function
38 // Parse decodes s into a UUID or returns an error. Both the UUID form of
39 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
40 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
41 func Parse(s string) (UUID, error) {
45 return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
47 if strings.ToLower(s[:9]) != "urn:uuid:" {
48 return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
52 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
53 return uuid, errors.New("invalid UUID format")
55 for i, x := range [16]int{
60 24, 26, 28, 30, 32, 34} {
61 v, ok := xtob(s[x], s[x+1])
63 return uuid, errors.New("invalid UUID format")
70 // ParseBytes is like Parse, except it parses a byte slice instead of a string.
71 func ParseBytes(b []byte) (UUID, error) {
75 return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
77 if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
78 return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
82 if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
83 return uuid, errors.New("invalid UUID format")
85 for i, x := range [16]int{
90 24, 26, 28, 30, 32, 34} {
91 v, ok := xtob(b[x], b[x+1])
93 return uuid, errors.New("invalid UUID format")
100 // MustParse is like Parse but panics if the string cannot be parsed.
101 // It simplifies safe initialization of global variables holding compiled UUIDs.
102 func MustParse(s string) UUID {
103 uuid, err := Parse(s)
105 panic(`uuid: Parse(` + s + `): ` + err.Error())
110 // FromBytes creates a new UUID from a byte slice. Returns an error if the slice
111 // does not have a length of 16. The bytes are copied from the slice.
112 func FromBytes(b []byte) (uuid UUID, err error) {
113 err = uuid.UnmarshalBinary(b)
117 // Must returns uuid if err is nil and panics otherwise.
118 func Must(uuid UUID, err error) UUID {
125 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
126 // , or "" if uuid is invalid.
127 func (uuid UUID) String() string {
129 encodeHex(buf[:], uuid)
130 return string(buf[:])
133 // URN returns the RFC 2141 URN form of uuid,
134 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
135 func (uuid UUID) URN() string {
137 copy(buf[:], "urn:uuid:")
138 encodeHex(buf[9:], uuid)
139 return string(buf[:])
142 func encodeHex(dst []byte, uuid UUID) {
143 hex.Encode(dst, uuid[:4])
145 hex.Encode(dst[9:13], uuid[4:6])
147 hex.Encode(dst[14:18], uuid[6:8])
149 hex.Encode(dst[19:23], uuid[8:10])
151 hex.Encode(dst[24:], uuid[10:])
154 // Variant returns the variant encoded in uuid.
155 func (uuid UUID) Variant() Variant {
157 case (uuid[8] & 0xc0) == 0x80:
159 case (uuid[8] & 0xe0) == 0xc0:
161 case (uuid[8] & 0xe0) == 0xe0:
168 // Version returns the version of uuid.
169 func (uuid UUID) Version() Version {
170 return Version(uuid[6] >> 4)
173 func (v Version) String() string {
175 return fmt.Sprintf("BAD_VERSION_%d", v)
177 return fmt.Sprintf("VERSION_%d", v)
180 func (v Variant) String() string {
193 return fmt.Sprintf("BadVariant%d", int(v))
196 // SetRand sets the random number generator to r, which implements io.Reader.
197 // If r.Read returns an error when the package requests random data then
198 // a panic will be issued.
200 // Calling SetRand with nil sets the random number generator to the default
202 func SetRand(r io.Reader) {