-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add UUID.parse?
#11998
Merged
straight-shoota
merged 4 commits into
crystal-lang:master
from
jgaskins:jgaskins/uuid-parsing-returns-nil-on-failure
Apr 28, 2022
Merged
Add UUID.parse?
#11998
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -75,9 +75,9 @@ struct UUID | |||||||||||
new(uuid.bytes, variant, version) | ||||||||||||
end | ||||||||||||
|
||||||||||||
# Creates new UUID by decoding `value` string from hyphenated (ie. `ba714f86-cac6-42c7-8956-bcf5105e1b81`), | ||||||||||||
# hexstring (ie. `89370a4ab66440c8add39e06f2bb6af6`) or URN (ie. `urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20`) | ||||||||||||
# format. | ||||||||||||
# Creates new UUID by decoding `value` string from hyphenated (ie `ba714f86-cac6-42c7-8956-bcf5105e1b81`), | ||||||||||||
# hexstring (ie `89370a4ab66440c8add39e06f2bb6af6`) or URN (ie `urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20`) | ||||||||||||
# format, raising an `ArgumentError` if the string fors not match any of these formats. | ||||||||||||
def self.new(value : String, variant = nil, version = nil) | ||||||||||||
bytes = uninitialized UInt8[16] | ||||||||||||
|
||||||||||||
|
@@ -107,16 +107,59 @@ struct UUID | |||||||||||
new(bytes, variant, version) | ||||||||||||
end | ||||||||||||
|
||||||||||||
# Creates new UUID by decoding `value` string from hyphenated (ie `ba714f86-cac6-42c7-8956-bcf5105e1b81`), | ||||||||||||
# hexstring (ie `89370a4ab66440c8add39e06f2bb6af6`) or URN (ie `urn:uuid:3f9eaf9e-cdb0-45cc-8ecb-0e5b2bfb0c20`) | ||||||||||||
# format, returning `nil` if the string does not match any of these formats. | ||||||||||||
def self.parse?(value : String, variant = nil, version = nil) : UUID? | ||||||||||||
bytes = uninitialized UInt8[16] | ||||||||||||
|
||||||||||||
case value.size | ||||||||||||
when 36 # Hyphenated | ||||||||||||
{8, 13, 18, 23}.each do |offset| | ||||||||||||
return if value[offset] != '-' | ||||||||||||
end | ||||||||||||
{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}.each_with_index do |offset, i| | ||||||||||||
if hex = hex_pair_at? value, offset | ||||||||||||
bytes[i] = hex | ||||||||||||
else | ||||||||||||
return | ||||||||||||
end | ||||||||||||
end | ||||||||||||
when 32 # Hexstring | ||||||||||||
16.times do |i| | ||||||||||||
if hex = hex_pair_at? value, i * 2 | ||||||||||||
bytes[i] = hex | ||||||||||||
else | ||||||||||||
return | ||||||||||||
end | ||||||||||||
end | ||||||||||||
when 45 # URN | ||||||||||||
return unless value.starts_with? "urn:uuid:" | ||||||||||||
{9, 11, 13, 15, 18, 20, 23, 25, 28, 30, 33, 35, 37, 39, 41, 43}.each_with_index do |offset, i| | ||||||||||||
if hex = hex_pair_at? value, offset | ||||||||||||
bytes[i] = hex | ||||||||||||
else | ||||||||||||
return | ||||||||||||
end | ||||||||||||
end | ||||||||||||
else | ||||||||||||
return | ||||||||||||
end | ||||||||||||
|
||||||||||||
new(bytes, variant, version) | ||||||||||||
end | ||||||||||||
|
||||||||||||
# Raises `ArgumentError` if string `value` at index `i` doesn't contain hex | ||||||||||||
# digit followed by another hex digit. | ||||||||||||
private def self.hex_pair_at(value : String, i) : UInt8 | ||||||||||||
hex_pair_at?(value, i) || raise ArgumentError.new "Invalid hex character at position #{i * 2} or #{i * 2 + 1}, expected '0' to '9', 'a' to 'f' or 'A' to 'F'" | ||||||||||||
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 is unreadable now. You could change it at least to sth like below:
Suggested change
|
||||||||||||
end | ||||||||||||
|
||||||||||||
# Parses 2 hex digits from `value` at index `i` and `i + 1`, returning `nil` | ||||||||||||
# if one or both are not actually hex digits. | ||||||||||||
private def self.hex_pair_at?(value : String, i) : UInt8? | ||||||||||||
if (ch1 = value[i].to_u8?(16)) && (ch2 = value[i + 1].to_u8?(16)) | ||||||||||||
ch1 * 16 + ch2 | ||||||||||||
else | ||||||||||||
raise ArgumentError.new [ | ||||||||||||
"Invalid hex character at position #{i * 2} or #{i * 2 + 1}", | ||||||||||||
"expected '0' to '9', 'a' to 'f' or 'A' to 'F'", | ||||||||||||
].join(", ") | ||||||||||||
end | ||||||||||||
end | ||||||||||||
|
||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Added this distinction and removed the dots because they truncated the document string in the method index section in an unexpected spot: