Skip to content
This repository was archived by the owner on Jan 1, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ module Version

##
# Version number stamped into the UUID to identify it as time-based.
VERSION_CLOCK = 0x0100
VERSION_CLOCK = 0x1000

##
# Variant number stamped into the UUID to identify it RFC4211.
VARIANT_CLOCK = 0x80

##
# Formats supported by the UUID generator.
Expand All @@ -95,9 +99,9 @@ module Version
# hyphens
# <tt>:urn</tt>:: Adds the prefix <tt>urn:uuid:</tt> to the default format
FORMATS = {
:compact => '%08x%04x%04x%04x%012x',
:default => '%08x-%04x-%04x-%04x-%012x',
:urn => 'urn:uuid:%08x-%04x-%04x-%04x-%012x',
:compact => '%08x%04x%04x%02x%02x%012x',
:default => '%08x-%04x-%04x-%02x%02x-%012x',
:urn => 'urn:uuid:%08x-%04x-%04x-%02x%02x-%012x',
}

##
Expand Down Expand Up @@ -320,7 +324,8 @@ def generate(format = :default)
clock & 0xFFFFFFFF,
(clock >> 32) & 0xFFFF,
((clock >> 48) & 0xFFFF | VERSION_CLOCK),
@sequence & 0xFFFF,
(@sequence >> 8) & 0xBF | 0x80,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't 0x80 be VARIANT_CLOCK?

@sequence & 0xFF,
@mac & 0xFFFFFFFFFFFF
]
end
Expand Down
14 changes: 7 additions & 7 deletions test/test-uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ def test_with_no_state_file
UUID.state_file = false
assert !UUID.state_file
uuid = UUID.new
assert_match(/\A[\da-f]{32}\z/i, uuid.generate(:compact))
assert_match(/\A[\da-f]{12}1[\da-f]{3}[89ab][\da-f]{15}\z/i, uuid.generate(:compact))
seq = uuid.next_sequence
assert_equal seq + 1, uuid.next_sequence
assert !UUID.state_file
end

def validate_uuid_generator(uuid)
assert_match(/\A[\da-f]{32}\z/i, uuid.generate(:compact))
assert_match(/\A[\da-f]{12}1[\da-f]{3}[89ab][\da-f]{15}\z/i, uuid.generate(:compact))

assert_match(/\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
assert_match(/\A[\da-f]{8}-[\da-f]{4}-1[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}\z/i,
uuid.generate(:default))

assert_match(/^urn:uuid:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
assert_match(/^urn:uuid:[\da-f]{8}-[\da-f]{4}-1[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}\z/i,
uuid.generate(:urn))

e = assert_raise ArgumentError do
Expand All @@ -84,12 +84,12 @@ def test_instance_generate
end

def test_class_generate
assert_match(/\A[\da-f]{32}\z/i, UUID.generate(:compact))
assert_match(/\A[\da-f]{12}1[\da-f]{3}[89ab][\da-f]{15}\z/i, UUID.generate(:compact))

assert_match(/\A[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
assert_match(/\A[\da-f]{8}-[\da-f]{4}-1[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}\z/i,
UUID.generate(:default))

assert_match(/^urn:uuid:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}\z/i,
assert_match(/^urn:uuid:[\da-f]{8}-[\da-f]{4}-1[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}\z/i,
UUID.generate(:urn))

e = assert_raise ArgumentError do
Expand Down