-
Notifications
You must be signed in to change notification settings - Fork 19
/
uuid.go
43 lines (35 loc) 路 983 Bytes
/
uuid.go
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
package gedcom
import (
"fmt"
"regexp"
"strings"
)
type UUID string
var uuidRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
// NewUUIDFromString returns a valid UUID in one of the forms:
//
// EE13561DDB204985BFFDEEBF82A5226C
// e0d4d387-618a-4713-ab3b-5fa3500b7a75
//
// An error is returned if there is more than 32 hexadecimal characters.
func NewUUIDFromString(s string) (UUID, error) {
s2 := strings.Replace(s, "-", "", -1)
s2 = strings.ToLower(s2)
if !uuidRegexp.MatchString(s2) {
return "", fmt.Errorf("invalid UUID: " + s)
}
uuid := fmt.Sprintf("%s-%s-%s-%s-%s",
s2[:8], s2[8:12], s2[12:16], s2[16:20], s2[20:])
return UUID(uuid), nil
}
// String returns a lowercase, hyphenated UUID in the form of:
//
// e0d4d387-618a-4713-ab3b-5fa3500b7a75
//
func (uuid UUID) String() string {
return string(uuid)
}
// Equals is true only if both UUIDs represent the same value.
func (uuid UUID) Equals(uuid2 UUID) bool {
return string(uuid) == string(uuid2)
}