-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed issue #131 by improving the Parse function in uuid.go + unit tests #132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,10 @@ func Parse(s string) (UUID, error) { | |
switch len(s) { | ||
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ||
case 36: | ||
// Ensure proper dash placement | ||
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { | ||
return uuid, errors.New("invalid UUID format") | ||
} | ||
|
||
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ||
case 36 + 9: | ||
|
@@ -74,44 +78,58 @@ func Parse(s string) (UUID, error) { | |
} | ||
s = s[9:] | ||
|
||
// Ensure proper dash placement | ||
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { | ||
return uuid, errors.New("invalid UUID format") | ||
} | ||
s = s[1:] | ||
|
||
// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} | ||
case 36 + 2: | ||
// Ensure proper dash placement | ||
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { | ||
return uuid, errors.New("invalid UUID format") | ||
} | ||
s = s[1:] | ||
|
||
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | ||
case 32: | ||
var ok bool | ||
// Ensure proper length and valid characters | ||
for i := range uuid { | ||
uuid[i], ok = xtob(s[i*2], s[i*2+1]) | ||
if !ok { | ||
uuid[i] = s[i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code ignores half of the UUID and will provide the wrong answer. It is possible that the existing for loop could be rewritten as
|
||
if !isValidHexChar(s[i]) { | ||
return uuid, errors.New("invalid UUID format") | ||
} | ||
} | ||
|
||
return uuid, nil | ||
|
||
default: | ||
return uuid, invalidLengthError{len(s)} | ||
} | ||
// s is now at least 36 bytes long | ||
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ||
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { | ||
return uuid, errors.New("invalid UUID format") | ||
} | ||
|
||
// Validate the UUID parts | ||
for i, x := range [16]int{ | ||
0, 2, 4, 6, | ||
9, 11, | ||
14, 16, | ||
19, 21, | ||
24, 26, 28, 30, 32, 34, | ||
} { | ||
v, ok := xtob(s[x], s[x+1]) | ||
if !ok { | ||
if !isValidHexChar(s[x]) || !isValidHexChar(s[x+1]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block is both slower and produces the wrong results. both s[x] and s[x+1] are required to determine the value of uuid byte |
||
return uuid, errors.New("invalid UUID format") | ||
} | ||
uuid[i] = v | ||
uuid[i] = s[x] | ||
} | ||
|
||
return uuid, nil | ||
} | ||
|
||
// isValidHexChar checks if a character is a valid hexadecimal character. | ||
func isValidHexChar(c byte) bool { | ||
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') | ||
} | ||
|
||
// ParseBytes is like Parse, except it parses a byte slice instead of a string. | ||
func ParseBytes(b []byte) (UUID, error) { | ||
var uuid UUID | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see any benefit in these changes. Checking for "-" is already done on lines 94-89.