Skip to content

Commit

Permalink
Merge pull request #11 from robotblake/master
Browse files Browse the repository at this point in the history
Increase speed of UUID generation
  • Loading branch information
nobu authored Dec 29, 2023
2 parents e8be089 + b587b8c commit a228741
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/random/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ def urlsafe_base64(n=nil, padding=false)
# See RFC4122[https://datatracker.ietf.org/doc/html/rfc4122] for details of UUID.
#
def uuid
ary = random_bytes(16).unpack("NnnnnN")
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
"%08x-%04x-%04x-%04x-%04x%08x" % ary
ary = random_bytes(16)
ary.setbyte(6, (ary.getbyte(6) & 0x0f) | 0x40)
ary.setbyte(8, (ary.getbyte(8) & 0x3f) | 0x80)
ary.unpack("H8H4H4H4H12").join(?-)
end

alias uuid_v4 uuid
Expand Down

0 comments on commit a228741

Please sign in to comment.