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 5f6c979..ca93a9a 100644 --- a/lib/ulid/generate.rb +++ b/lib/ulid/generate.rb @@ -9,15 +9,18 @@ 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 # 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 + return "0#{b32}" if b32.length == 25 b32 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] 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 -