class BaseConvert::Digits
Public Class Methods
new()
click to toggle source
# File lib/base_convert/digits.rb, line 3 def initialize @registry = [:P95, :B64, :U47, :G94, :Q91, :W63] end
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/base_convert/digits.rb, line 9 def [](key) return (_=get key).is_a?(Symbol)?self[_]:_ if self.has_key? key case key when Symbol chars = Chars.new key.to_s.scan(/[+-]?[[:alnum:]]+/).each do |type| if self.has_key?(_=type.to_sym) chars.add super(_) next end case type when /^((u\h+)|(k\d+))+$/ # add single char by code type.scan(/[uk]\h+/).each{chars.top _1.to_sym} when /^(([ij]\d+)|([vw]\h+))+$/ # set start/stop range type.scan(/[ijvw]\h+/).each{chars.set _1} when /^[a-z][a-z]+$/ # add by POSIX bracket expression chars.add Regexp.new "[[:#{type}:]]" when /^[a-z]$/ # add by metacharacter chars.add Regexp.new "\\#{type}" when /^[A-Z]+$/i # add by property type.scan(/[A-Z][a-z]*/).each{chars.add(/\p{#{_1}}/)} when /^([+-])(\w+)$/ # top or remove chars of key given (_=self[$2.to_sym]) and ($1=='+')? chars.top(_): chars.remove(_) when /^(\p{L}+)(\d+)$/ # a registered set l,m = $1,$2.to_i-1 n = self.keys.select{_1=~/^#{l}\d+$/} .map{_1.to_s.sub(l,'').to_i}.max raise "no #{l}<n> digits defined" if n.nil? raise "out of range of #{l}#{n}" unless m<n chars.add self[:"#{l}#{n}"][0..m] else raise "unrecognized digits key: #{type}" end end return chars.to_s.freeze when String _=@registry.lazy.map{self[_1]}.detect{_1.start_with? key} and return _ raise 'need at least 2 digits' unless key.length > 1 if key.length > key.chars.uniq.length raise 'digits must not have duplicates' end return key when Integer raise 'need digits to cover base' if key > 95 return self[:P95] # Defined in configuration.rb end raise 'digits must be String|Symbol|Integer' end
Also aliased as: get
forget!(keys=@registry)
click to toggle source
# File lib/base_convert/digits.rb, line 79 def forget!(keys=@registry) [*keys].each do |k| # follow Symbol links 'till nil|String while s = get(k) and not s.is_a? String raise 'expected Symbol' unless s.is_a? Symbol k = s end self.delete(k) if s end end
label(d)
click to toggle source
# File lib/base_convert/digits.rb, line 64 def label(d) registry(d) or (d[0..1]+d[-2..-1]).to_sym end
memoize!(keys=@registry)
click to toggle source
# File lib/base_convert/digits.rb, line 68 def memoize!(keys=@registry) [*keys].each do |k| # follow Symbol links 'till nil|String while s=get(k) and not s.is_a? String raise 'expected Symbol' unless s.is_a? Symbol k = s end self[k]=self[k] unless s # memoize if needed end end
registry(d=nil)
click to toggle source
# File lib/base_convert/digits.rb, line 58 def registry(d=nil) # BaseConvert::Number memoizes and uses specifically :P95, :B64, and :U47; # giving these precedence above the rest. d ? @registry.detect{|_|self[_].start_with? d}: @registry end