1 // Copyright 2011 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.
16 // Array is a pass-by-value UUID that can be used as an effecient key in a map.
19 // UUID converts uuid into a slice.
20 func (uuid Array) UUID() UUID {
24 // String returns the string representation of uuid,
25 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
26 func (uuid Array) String() string {
27 return uuid.UUID().String()
30 // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
34 // A Version represents a UUIDs version.
37 // A Variant represents a UUIDs variant.
40 // Constants returned by Variant.
42 Invalid = Variant(iota) // Invalid UUID
43 RFC4122 // The variant specified in RFC4122
44 Reserved // Reserved, NCS backward compatibility.
45 Microsoft // Reserved, Microsoft Corporation backward compatibility.
46 Future // Reserved for future definition.
49 var rander = rand.Reader // random function
51 // New returns a new random (version 4) UUID as a string. It is a convenience
52 // function for NewRandom().String().
54 return NewRandom().String()
57 // Parse decodes s into a UUID or returns nil. Both the UUID form of
58 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
59 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
60 func Parse(s string) UUID {
62 if strings.ToLower(s[:9]) != "urn:uuid:" {
66 } else if len(s) != 36 {
69 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
73 for i, x := range [16]int{
78 24, 26, 28, 30, 32, 34} {
79 if v, ok := xtob(s[x:]); !ok {
88 // Equal returns true if uuid1 and uuid2 are equal.
89 func Equal(uuid1, uuid2 UUID) bool {
90 return bytes.Equal(uuid1, uuid2)
93 // Array returns an array representation of uuid that can be used as a map key.
94 // Array panics if uuid is not valid.
95 func (uuid UUID) Array() Array {
104 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
105 // , or "" if uuid is invalid.
106 func (uuid UUID) String() string {
111 encodeHex(buf[:], uuid)
112 return string(buf[:])
115 // URN returns the RFC 2141 URN form of uuid,
116 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
117 func (uuid UUID) URN() string {
122 copy(buf[:], "urn:uuid:")
123 encodeHex(buf[9:], uuid)
124 return string(buf[:])
127 func encodeHex(dst []byte, uuid UUID) {
128 hex.Encode(dst[:], uuid[:4])
130 hex.Encode(dst[9:13], uuid[4:6])
132 hex.Encode(dst[14:18], uuid[6:8])
134 hex.Encode(dst[19:23], uuid[8:10])
136 hex.Encode(dst[24:], uuid[10:])
139 // Variant returns the variant encoded in uuid. It returns Invalid if
141 func (uuid UUID) Variant() Variant {
146 case (uuid[8] & 0xc0) == 0x80:
148 case (uuid[8] & 0xe0) == 0xc0:
150 case (uuid[8] & 0xe0) == 0xe0:
157 // Version returns the version of uuid. It returns false if uuid is not
159 func (uuid UUID) Version() (Version, bool) {
163 return Version(uuid[6] >> 4), true
166 func (v Version) String() string {
168 return fmt.Sprintf("BAD_VERSION_%d", v)
170 return fmt.Sprintf("VERSION_%d", v)
173 func (v Variant) String() string {
186 return fmt.Sprintf("BadVariant%d", int(v))
189 // SetRand sets the random number generator to r, which implements io.Reader.
190 // If r.Read returns an error when the package requests random data then
191 // a panic will be issued.
193 // Calling SetRand with nil sets the random number generator to the default
195 func SetRand(r io.Reader) {