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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package internal
import (
"encoding/hex"
"fmt"
"strings"
"lukeshu.com/btrfs-tools/pkg/util"
)
type UUID [16]byte
func (uuid UUID) String() string {
str := hex.EncodeToString(uuid[:])
return strings.Join([]string{
str[:8],
str[8:12],
str[12:16],
str[16:20],
str[20:32],
}, "-")
}
func (uuid UUID) Format(f fmt.State, verb rune) {
util.FormatByteArrayStringer(uuid, uuid[:], f, verb)
}
func ParseUUID(str string) (UUID, error) {
var ret UUID
j := 0
for i := 0; i < len(str); i++ {
if j >= len(ret)*2 {
return UUID{}, fmt.Errorf("too long to be a UUID: %q|%q", str[:i], str[i:])
}
c := str[i]
var v byte
switch {
case '0' <= c && c <= '9':
v = c - '0'
case 'a' <= c && c <= 'f':
v = c - 'a' + 10
case 'A' <= c && c <= 'F':
v = c - 'A' + 10
case c == '-':
continue
default:
return UUID{}, fmt.Errorf("illegal byte in UUID: %q|%q|%q", str[:i], str[i:i+1], str[i+1:])
}
if j%2 == 0 {
ret[j/2] = v << 4
} else {
ret[j/2] = (ret[j/2] & 0xf0) | (v & 0x0f)
}
j++
}
return ret, nil
}
func MustParseUUID(str string) UUID {
ret, err := ParseUUID(str)
if err != nil {
panic(err)
}
return ret
}
|