OSDN Git Service

Small edit (#246)
[bytom/vapor.git] / vendor / github.com / google / uuid / uuid.go
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.
4
5 package uuid
6
7 import (
8         "bytes"
9         "crypto/rand"
10         "encoding/hex"
11         "errors"
12         "fmt"
13         "io"
14         "strings"
15 )
16
17 // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
18 // 4122.
19 type UUID [16]byte
20
21 // A Version represents a UUID's version.
22 type Version byte
23
24 // A Variant represents a UUID's variant.
25 type Variant byte
26
27 // Constants returned by Variant.
28 const (
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.
34 )
35
36 var rander = rand.Reader // random function
37
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) {
42         var uuid UUID
43         if len(s) != 36 {
44                 if len(s) != 36+9 {
45                         return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
46                 }
47                 if strings.ToLower(s[:9]) != "urn:uuid:" {
48                         return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
49                 }
50                 s = s[9:]
51         }
52         if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
53                 return uuid, errors.New("invalid UUID format")
54         }
55         for i, x := range [16]int{
56                 0, 2, 4, 6,
57                 9, 11,
58                 14, 16,
59                 19, 21,
60                 24, 26, 28, 30, 32, 34} {
61                 v, ok := xtob(s[x], s[x+1])
62                 if !ok {
63                         return uuid, errors.New("invalid UUID format")
64                 }
65                 uuid[i] = v
66         }
67         return uuid, nil
68 }
69
70 // ParseBytes is like Parse, except it parses a byte slice instead of a string.
71 func ParseBytes(b []byte) (UUID, error) {
72         var uuid UUID
73         if len(b) != 36 {
74                 if len(b) != 36+9 {
75                         return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
76                 }
77                 if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
78                         return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
79                 }
80                 b = b[9:]
81         }
82         if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
83                 return uuid, errors.New("invalid UUID format")
84         }
85         for i, x := range [16]int{
86                 0, 2, 4, 6,
87                 9, 11,
88                 14, 16,
89                 19, 21,
90                 24, 26, 28, 30, 32, 34} {
91                 v, ok := xtob(b[x], b[x+1])
92                 if !ok {
93                         return uuid, errors.New("invalid UUID format")
94                 }
95                 uuid[i] = v
96         }
97         return uuid, nil
98 }
99
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)
104         if err != nil {
105                 panic(`uuid: Parse(` + s + `): ` + err.Error())
106         }
107         return uuid
108 }
109
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)
114         return uuid, err
115 }
116
117 // Must returns uuid if err is nil and panics otherwise.
118 func Must(uuid UUID, err error) UUID {
119         if err != nil {
120                 panic(err)
121         }
122         return uuid
123 }
124
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 {
128         var buf [36]byte
129         encodeHex(buf[:], uuid)
130         return string(buf[:])
131 }
132
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 {
136         var buf [36 + 9]byte
137         copy(buf[:], "urn:uuid:")
138         encodeHex(buf[9:], uuid)
139         return string(buf[:])
140 }
141
142 func encodeHex(dst []byte, uuid UUID) {
143         hex.Encode(dst, uuid[:4])
144         dst[8] = '-'
145         hex.Encode(dst[9:13], uuid[4:6])
146         dst[13] = '-'
147         hex.Encode(dst[14:18], uuid[6:8])
148         dst[18] = '-'
149         hex.Encode(dst[19:23], uuid[8:10])
150         dst[23] = '-'
151         hex.Encode(dst[24:], uuid[10:])
152 }
153
154 // Variant returns the variant encoded in uuid.
155 func (uuid UUID) Variant() Variant {
156         switch {
157         case (uuid[8] & 0xc0) == 0x80:
158                 return RFC4122
159         case (uuid[8] & 0xe0) == 0xc0:
160                 return Microsoft
161         case (uuid[8] & 0xe0) == 0xe0:
162                 return Future
163         default:
164                 return Reserved
165         }
166 }
167
168 // Version returns the version of uuid.
169 func (uuid UUID) Version() Version {
170         return Version(uuid[6] >> 4)
171 }
172
173 func (v Version) String() string {
174         if v > 15 {
175                 return fmt.Sprintf("BAD_VERSION_%d", v)
176         }
177         return fmt.Sprintf("VERSION_%d", v)
178 }
179
180 func (v Variant) String() string {
181         switch v {
182         case RFC4122:
183                 return "RFC4122"
184         case Reserved:
185                 return "Reserved"
186         case Microsoft:
187                 return "Microsoft"
188         case Future:
189                 return "Future"
190         case Invalid:
191                 return "Invalid"
192         }
193         return fmt.Sprintf("BadVariant%d", int(v))
194 }
195
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.
199 //
200 // Calling SetRand with nil sets the random number generator to the default
201 // generator.
202 func SetRand(r io.Reader) {
203         if r == nil {
204                 rander = rand.Reader
205                 return
206         }
207         rander = r
208 }