Module: UUIDv7::Base62
- Defined in:
- lib/uuidv7/base62.rb
Constant Summary collapse
- ALPHABET =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'- BASE =
62- COMPACT_LENGTH =
22
Class Method Summary collapse
Class Method Details
.decode(compact_string) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/uuidv7/base62.rb', line 29 def decode(compact_string) raise InvalidCompactStringError, 'Compact string cannot be nil' if compact_string.nil? unless compact_string.length == COMPACT_LENGTH raise InvalidCompactStringError, "Compact string must be exactly #{COMPACT_LENGTH} characters (got #{compact_string.length})" end value = 0 compact_string.each_char do |char| digit = ALPHABET.index(char) raise InvalidCompactStringError, "Invalid compact string character: #{char}" if digit.nil? value = (value * BASE) + digit end msb = (value >> 64) & 0xFFFFFFFFFFFFFFFF lsb = value & 0xFFFFFFFFFFFFFFFF hex = format('%016x%016x', msb, lsb) "#{hex[0, 8]}-#{hex[8, 4]}-#{hex[12, 4]}-#{hex[16, 4]}-#{hex[20, 12]}" end |
.encode(uuid) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/uuidv7/base62.rb', line 10 def encode(uuid) raise InvalidUUIDError, 'UUID cannot be nil' if uuid.nil? hex = uuid.delete('-') raise InvalidUUIDError, 'Invalid UUID format' unless hex.match?(/\A[0-9a-f]{32}\z/i) value = hex.to_i(16) result = [] while value.positive? value, remainder = value.divmod(BASE) result << ALPHABET[remainder] end result << '0' while result.length < COMPACT_LENGTH result.reverse.join end |