Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lib/ulid/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions lib/ulid/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ulid/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions lib/ulid/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module ULID
VERSION = "1.0.3"
VERSION = "1.0.4"
end