From b587b8c7cb68f06b8463679986b1efe1bd3ac637 Mon Sep 17 00:00:00 2001 From: Blake Imsland Date: Sun, 13 Mar 2022 23:27:46 -0700 Subject: [PATCH] Increase speed of UUID generation --- lib/random/formatter.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/random/formatter.rb b/lib/random/formatter.rb index 744853a..9b8b38f 100644 --- a/lib/random/formatter.rb +++ b/lib/random/formatter.rb @@ -142,10 +142,10 @@ def urlsafe_base64(n=nil, padding=false) # See RFC 4122 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 private def gen_random(n)