MCPcopy
hub / github.com/google/uuid / ParseBytes

Function ParseBytes

uuid.go:120–162  ·  view source on GitHub ↗

ParseBytes is like Parse, except it parses a byte slice instead of a string.

(b []byte)

Source from the content-addressed store, hash-verified

118
119// ParseBytes is like Parse, except it parses a byte slice instead of a string.
120func ParseBytes(b []byte) (UUID, error) {
121 var uuid UUID
122 switch len(b) {
123 case 36: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
124 case 36 + 9: // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
125 if !bytes.EqualFold(b[:9], []byte("urn:uuid:")) {
126 return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
127 }
128 b = b[9:]
129 case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
130 b = b[1:]
131 case 32: // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
132 var ok bool
133 for i := 0; i < 32; i += 2 {
134 uuid[i/2], ok = xtob(b[i], b[i+1])
135 if !ok {
136 return uuid, errors.New("invalid UUID format")
137 }
138 }
139 return uuid, nil
140 default:
141 return uuid, invalidLengthError{len(b)}
142 }
143 // s is now at least 36 bytes long
144 // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
145 if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
146 return uuid, errors.New("invalid UUID format")
147 }
148 for i, x := range [16]int{
149 0, 2, 4, 6,
150 9, 11,
151 14, 16,
152 19, 21,
153 24, 26, 28, 30, 32, 34,
154 } {
155 v, ok := xtob(b[x], b[x+1])
156 if !ok {
157 return uuid, errors.New("invalid UUID format")
158 }
159 uuid[i] = v
160 }
161 return uuid, nil
162}
163
164// MustParse is like Parse but panics if the string cannot be parsed.
165// It simplifies safe initialization of global variables holding compiled UUIDs.

Callers 5

UnmarshalTextMethod · 0.85
UnmarshalTextMethod · 0.85
testBytesFunction · 0.85
FuzzParseBytesFunction · 0.85
BenchmarkParseBytesFunction · 0.85

Calls 1

xtobFunction · 0.85

Tested by 3

testBytesFunction · 0.68
FuzzParseBytesFunction · 0.68
BenchmarkParseBytesFunction · 0.68