class Multibases::OrdTable
Attributes
base[R]
encoding[R]
factor[R]
padder[R]
Public Class Methods
new(ords, strict:, padder: nil, encoding: nil)
click to toggle source
# File lib/multibases/ord_table.rb, line 7 def initialize(ords, strict:, padder: nil, encoding: nil) ords = ords.uniq @ords = ords @base = ords.length @padder = padder calculate_strictness(strict: strict) calculate_encoding(encoding: encoding) end
Public Instance Methods
alphabet()
click to toggle source
# File lib/multibases/ord_table.rb, line 40 def alphabet @ords.pack('C*') end
eql?(other)
click to toggle source
# File lib/multibases/ord_table.rb, line 18 def eql?(other) other.is_a?(OrdTable) && other.alphabet == alphabet && other.strict? == strict? end
Also aliased as: ==
hash()
click to toggle source
# File lib/multibases/ord_table.rb, line 26 def hash @ords.hash end
strict?()
click to toggle source
# File lib/multibases/ord_table.rb, line 30 def strict? @strict end
tr_ords(force_strict: false)
click to toggle source
# File lib/multibases/ord_table.rb, line 34 def tr_ords(force_strict: false) return @ords + [@padder].compact if strict? || force_strict @loose_ords + [@padder].compact end
Private Instance Methods
calculate_encoding(encoding:)
click to toggle source
# File lib/multibases/ord_table.rb, line 65 def calculate_encoding(encoding:) invalid_ord = @ords.find { |ord| ord > 255 } raise AlphabetOutOfBoundary, invalid_ord if invalid_ord if encoding raise AlphabetEncodingInvalid, encoding unless valid_encoding?(encoding) @encoding = encoding return end @encoding = fits_seven_bits? ? Encoding::US_ASCII : Encoding::ASCII_8BIT end
calculate_strictness(strict:)
click to toggle source
# File lib/multibases/ord_table.rb, line 48 def calculate_strictness(strict:) chars = alphabet.chars chars_downcased = chars.map(&:downcase).uniq chars_upcased = chars.map(&:upcase).uniq chars_cased = chars_upcased - chars_downcased # Strict means that the algorithm may _not_ treat incorrectly cased # input the same as correctly cased input. In other words, the table is # strict if a character exists that is both upcased and downcased and # therefore has a canonical casing. @strict = strict || chars_cased.empty? || chars.length != chars_downcased.length @loose_ords = (chars + chars_downcased + chars_upcased).uniq.map(&:ord) end
fits_seven_bits?()
click to toggle source
# File lib/multibases/ord_table.rb, line 84 def fits_seven_bits? valid_encoding?(Encoding::US_ASCII) end
valid_encoding?(encoding)
click to toggle source
# File lib/multibases/ord_table.rb, line 79 def valid_encoding?(encoding) ords = strict? ? @ords : @loose_ords ords.pack('C*').force_encoding(encoding).valid_encoding? end