class Multibases::IndexedOrdTable

Public Class Methods

new(ords, **opts) click to toggle source
Calls superclass method Multibases::OrdTable::new
# File lib/multibases/ord_table.rb, line 90
def initialize(ords, **opts)
  super(ords, **opts)

  @forward = ords.each_with_index.to_h
  @backward = Hash[@forward.to_a.collect(&:reverse)]
  @factor = Math.log(256) / Math.log(base)
end

Public Instance Methods

decoded_length(encoded_bytes) click to toggle source
# File lib/multibases/ord_table.rb, line 117
def decoded_length(encoded_bytes)
  (encoded_bytes.length / factor).round
end
decoded_zeroes_length(count) click to toggle source
# File lib/multibases/ord_table.rb, line 129
def decoded_zeroes_length(count)
  # For power of 2 bases, add "canonical-width"
  return (count / factor).round if pad_to_power?

  # For other bases, add a equivalent count to front
  count
end
encoded_length(plain_bytes) click to toggle source
# File lib/multibases/ord_table.rb, line 113
def encoded_length(plain_bytes)
  (plain_bytes.length.to_f * factor).ceil
end
encoded_zeroes_length(count) click to toggle source
# File lib/multibases/ord_table.rb, line 121
def encoded_zeroes_length(count)
  # For power of 2 bases, add "canonical-width"
  return (factor * count).floor if pad_to_power?

  # For other bases, add a equivalent count to front
  count
end
index(byte) click to toggle source
# File lib/multibases/ord_table.rb, line 102
def index(byte)
  @forward[byte] || !strict? && (
    @forward[byte.chr.upcase.ord] ||
    @forward[byte.chr.downcase.ord]
  )
end
ord_at(index) click to toggle source
# File lib/multibases/ord_table.rb, line 109
def ord_at(index)
  @backward[index]
end
pad_to_power?() click to toggle source
# File lib/multibases/ord_table.rb, line 137
def pad_to_power?
  (Math.log2(base) % 1).zero?
end
zero() click to toggle source
# File lib/multibases/ord_table.rb, line 98
def zero
  @backward[0]
end