-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuuid.go
More file actions
48 lines (40 loc) · 1.01 KB
/
uuid.go
File metadata and controls
48 lines (40 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package dogma
import (
"fmt"
)
func normalizeUUID(id string) (string, error) {
const size = 36
if len(id) != size {
return "", fmt.Errorf("%q is not a canonical RFC 9562 UUID: expected 36 characters", id)
}
var normalized [size]byte
isNil := true
for i := range size {
c := id[i]
normalized[i] = c
switch i {
case 8, 13, 18, 23: // indexes of hyphens
if c != '-' {
return "", fmt.Errorf("%q is not a canonical RFC 9562 UUID: expected hyphen at position %d", id, i)
}
default:
switch {
case c == '0':
// ok
case c >= '1' && c <= '9':
isNil = false
case c >= 'a' && c <= 'f':
isNil = false
case c >= 'A' && c <= 'F':
isNil = false
normalized[i] += 'a' - 'A' // convert to lowercase
default:
return "", fmt.Errorf("%q is not a canonical RFC 9562 UUID: expected hex digit at position %d", id, i)
}
}
}
if isNil {
return "", fmt.Errorf(`%q is not a canonical RFC 9562 UUID: the "nil" UUID is not supported`, id)
}
return string(normalized[:]), nil
}