From 3ba0841933cc737d5bd65bfc9f5398376e9e080f Mon Sep 17 00:00:00 2001 From: Logan Martel Date: Mon, 10 Jun 2024 00:20:56 -0400 Subject: [PATCH 1/4] Fix intermittent nil ULID string error --- lib/ulid/generate.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ulid/generate.rb b/lib/ulid/generate.rb index 5f6c979..29b250d 100644 --- a/lib/ulid/generate.rb +++ b/lib/ulid/generate.rb @@ -15,7 +15,9 @@ def encode32 # use the RFC4648 Base32 encoding and convert to Crockford's Base32 with a simple translate # assumes that the value is a 128-bit integer # also assumes that .to_s(32) is lowercase - b32 = value.to_s(32).tr!(B32_RCF4648_FRAGMENT, B32_CROCKFORD_FRAGMENT).upcase! + b32 = value.to_s(32) + b32.tr!(B32_RCF4648_FRAGMENT, B32_CROCKFORD_FRAGMENT) + b32.upcase! return "0" + b32 if b32.length == 25 From 056a0eb5eee443e0de4184f4b4e9f07e2258ee5d Mon Sep 17 00:00:00 2001 From: Logan Martel Date: Mon, 10 Jun 2024 00:25:35 -0400 Subject: [PATCH 2/4] Cleanup assorted ULID encoding optimizations --- lib/ulid/constants.rb | 4 ++-- lib/ulid/generate.rb | 7 ++++--- lib/ulid/identifier.rb | 2 +- lib/ulid/parse.rb | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/ulid/constants.rb b/lib/ulid/constants.rb index 79d8d25..a3cda16 100644 --- a/lib/ulid/constants.rb +++ b/lib/ulid/constants.rb @@ -24,7 +24,7 @@ module Constants # B32_CROCKFORD_CHARS = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" # B32_RCF4648_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUV" - B32_CROCKFORD_FRAGMENT = "JKMNPQRSTVWXYZ".upcase.freeze - B32_RCF4648_FRAGMENT = "IJKLMNOPQRSTUV".downcase.freeze # forcing downcase becase .to_s(32) is always lowercase + B32_CROCKFORD_FRAGMENT = "JKMNPQRSTVWXYZ" + B32_RCF4648_FRAGMENT = "ijklmnopqrstuv" # downcase because .to_s(32) is always lowercase end end diff --git a/lib/ulid/generate.rb b/lib/ulid/generate.rb index 29b250d..ca93a9a 100644 --- a/lib/ulid/generate.rb +++ b/lib/ulid/generate.rb @@ -9,8 +9,9 @@ module Generate # returns the binary ULID as Base32 encoded string. def encode32 - (hi, lo) = bytes.unpack("Q>Q>") - value = (hi << 64) | lo + high = bytes.unpack1("Q>") + low = bytes.unpack1("@8Q>") + value = (high << 64) | low # use the RFC4648 Base32 encoding and convert to Crockford's Base32 with a simple translate # assumes that the value is a 128-bit integer @@ -19,7 +20,7 @@ def encode32 b32.tr!(B32_RCF4648_FRAGMENT, B32_CROCKFORD_FRAGMENT) b32.upcase! - return "0" + b32 if b32.length == 25 + return "0#{b32}" if b32.length == 25 b32 end diff --git a/lib/ulid/identifier.rb b/lib/ulid/identifier.rb index d7c07bc..7a8b783 100644 --- a/lib/ulid/identifier.rb +++ b/lib/ulid/identifier.rb @@ -69,7 +69,7 @@ def initialize(start = nil, seed = nil) @time, @seed = unpack_ulid_bytes(@bytes) else # unrecognized initial values type given, just generate fresh ULID - @time = Time.now + @time = Time.now.utc @seed = random_bytes end diff --git a/lib/ulid/parse.rb b/lib/ulid/parse.rb index dc3a189..8727e51 100644 --- a/lib/ulid/parse.rb +++ b/lib/ulid/parse.rb @@ -22,7 +22,7 @@ def decode10(input) end def unpack_ulid_bytes(packed_bytes) - time_int, _ = ("\x00\x00" + packed_bytes).unpack("Q>") + time_int = ("\x00\x00#{packed_bytes}").unpack1("Q>") seed = packed_bytes[6..-1] [Time.at(time_int.to_f / 1000.0), seed] From d365d31746249efa97519a9fd7d4d2bd1c3bf6b2 Mon Sep 17 00:00:00 2001 From: Logan Martel Date: Mon, 10 Jun 2024 16:58:03 -0400 Subject: [PATCH 3/4] Remove UTC conversion for UNIX epoch time --- lib/ulid/identifier.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ulid/identifier.rb b/lib/ulid/identifier.rb index 7a8b783..d7c07bc 100644 --- a/lib/ulid/identifier.rb +++ b/lib/ulid/identifier.rb @@ -69,7 +69,7 @@ def initialize(start = nil, seed = nil) @time, @seed = unpack_ulid_bytes(@bytes) else # unrecognized initial values type given, just generate fresh ULID - @time = Time.now.utc + @time = Time.now @seed = random_bytes end From de9176113d8a492d4eb657cd32b0ba7466f43de7 Mon Sep 17 00:00:00 2001 From: Logan Martel Date: Mon, 10 Jun 2024 16:59:24 -0400 Subject: [PATCH 4/4] Bump to version 1.0.4 --- lib/ulid/version.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ulid/version.rb b/lib/ulid/version.rb index 664393e..553b42c 100644 --- a/lib/ulid/version.rb +++ b/lib/ulid/version.rb @@ -1,4 +1,3 @@ module ULID - VERSION = "1.0.3" + VERSION = "1.0.4" end -