1 // Copyright 2018 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 standard UUID
39 // forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
40 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the
41 // Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex
42 // encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
43 func Parse(s string) (UUID, error) {
46 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
49 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
51 if strings.ToLower(s[:9]) != "urn:uuid:" {
52 return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
56 // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
60 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
64 uuid[i], ok = xtob(s[i*2], s[i*2+1])
66 return uuid, errors.New("invalid UUID format")
71 return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
73 // s is now at least 36 bytes long
74 // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
75 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
76 return uuid, errors.New("invalid UUID format")
78 for i, x := range [16]int{
83 24, 26, 28, 30, 32, 34} {
84 v, ok := xtob(s[x], s[x+1])
86 return uuid, errors.New("invalid UUID format")
93 // ParseBytes is like Parse, except it parses a byte slice instead of a string.
94 func ParseBytes(b []byte) (UUID, error) {
97 case 36: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
98 case 36 + 9: // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
99 if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
100 return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
103 case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
105 case 32: // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
107 for i := 0; i < 32; i += 2 {
108 uuid[i/2], ok = xtob(b[i], b[i+1])
110 return uuid, errors.New("invalid UUID format")
115 return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
117 // s is now at least 36 bytes long
118 // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
119 if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
120 return uuid, errors.New("invalid UUID format")
122 for i, x := range [16]int{
127 24, 26, 28, 30, 32, 34} {
128 v, ok := xtob(b[x], b[x+1])
130 return uuid, errors.New("invalid UUID format")
137 // MustParse is like Parse but panics if the string cannot be parsed.
138 // It simplifies safe initialization of global variables holding compiled UUIDs.
139 func MustParse(s string) UUID {
140 uuid, err := Parse(s)
142 panic(`uuid: Parse(` + s + `): ` + err.Error())
147 // FromBytes creates a new UUID from a byte slice. Returns an error if the slice
148 // does not have a length of 16. The bytes are copied from the slice.
149 func FromBytes(b []byte) (uuid UUID, err error) {
150 err = uuid.UnmarshalBinary(b)
154 // Must returns uuid if err is nil and panics otherwise.
155 func Must(uuid UUID, err error) UUID {
162 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
163 // , or "" if uuid is invalid.
164 func (uuid UUID) String() string {
166 encodeHex(buf[:], uuid)
167 return string(buf[:])
170 // URN returns the RFC 2141 URN form of uuid,
171 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
172 func (uuid UUID) URN() string {
174 copy(buf[:], "urn:uuid:")
175 encodeHex(buf[9:], uuid)
176 return string(buf[:])
179 func encodeHex(dst []byte, uuid UUID) {
180 hex.Encode(dst, uuid[:4])
182 hex.Encode(dst[9:13], uuid[4:6])
184 hex.Encode(dst[14:18], uuid[6:8])
186 hex.Encode(dst[19:23], uuid[8:10])
188 hex.Encode(dst[24:], uuid[10:])
191 // Variant returns the variant encoded in uuid.
192 func (uuid UUID) Variant() Variant {
194 case (uuid[8] & 0xc0) == 0x80:
196 case (uuid[8] & 0xe0) == 0xc0:
198 case (uuid[8] & 0xe0) == 0xe0:
205 // Version returns the version of uuid.
206 func (uuid UUID) Version() Version {
207 return Version(uuid[6] >> 4)
210 func (v Version) String() string {
212 return fmt.Sprintf("BAD_VERSION_%d", v)
214 return fmt.Sprintf("VERSION_%d", v)
217 func (v Variant) String() string {
230 return fmt.Sprintf("BadVariant%d", int(v))
233 // SetRand sets the random number generator to r, which implements io.Reader.
234 // If r.Read returns an error when the package requests random data then
235 // a panic will be issued.
237 // Calling SetRand with nil sets the random number generator to the default
239 func SetRand(r io.Reader) {