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

Function Parse

uuid.go:95–145  ·  uuid.go::Parse

Parse decodes s into a UUID or returns an error if it cannot be parsed. Both the standard UUID forms defined in RFC 9562 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) are decoded. In addition, Parse accepts non-standard strings such as the raw hex encodin

(s string)

Source from the content-addressed store, hash-verified

93// examined in the latter case. Parse should not be used to validate strings as
94// it parses non-standard encodings as indicated above.
95func Parse(s string) (UUID, error) {
96 var uuid UUID
97 switch len(s) {
98 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
99 case 36:
100
101 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
102 case 36 + 9:
103 if !strings.EqualFold(s[:9], "urn:uuid:") {
104 return uuid, URNPrefixError{s[:9]}
105 }
106 s = s[9:]
107
108 // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
109 case 36 + 2:
110 s = s[1:]
111
112 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
113 case 32:
114 var ok bool
115 for i := range uuid {
116 uuid[i], ok = xtob(s[i*2], s[i*2+1])
117 if !ok {
118 return uuid, ErrInvalidUUIDFormat
119 }
120 }
121 return uuid, nil
122 default:
123 return uuid, invalidLengthError{len(s)}
124 }
125 // s is now at least 36 bytes long
126 // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
127 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
128 return uuid, ErrInvalidUUIDFormat
129
130 }
131 for i, x := range [16]int{
132 0, 2, 4, 6,
133 9, 11,
134 14, 16,
135 19, 21,
136 24, 26, 28, 30, 32, 34,
137 } {
138 v, ok := xtob(s[x], s[x+1])
139 if !ok {
140 return uuid, ErrInvalidUUIDFormat
141 }
142 uuid[i] = v
143 }
144 return uuid, nil
145}
146
147// ParseBytes is like Parse, except it parses a byte slice instead of a string.
148func ParseBytes(b []byte) (UUID, error) {

Callers 15

ScanMethod · 0.85
testTestFunction · 0.85
testBytesFunction · 0.85
TestNewFunction · 0.85
TestCodingFunction · 0.85
TestNodeAndTimeFunction · 0.85
TestWrongLengthFunction · 0.85
TestIsWrongLengthFunction · 0.85
FuzzParseFunction · 0.85
BenchmarkParseFunction · 0.85
parseBytesUnsafeFunction · 0.85
parseBytesCopyFunction · 0.85

Calls 1

xtobFunction · 0.85

Tested by 15

testTestFunction · 0.68
testBytesFunction · 0.68
TestNewFunction · 0.68
TestCodingFunction · 0.68
TestNodeAndTimeFunction · 0.68
TestWrongLengthFunction · 0.68
TestIsWrongLengthFunction · 0.68
FuzzParseFunction · 0.68
BenchmarkParseFunction · 0.68
parseBytesUnsafeFunction · 0.68
parseBytesCopyFunction · 0.68
BenchmarkUUID_StringFunction · 0.68