Class: Anybase

Inherits:
Object
  • Object
show all
Defined in:
lib/anybase.rb,
lib/anybase.rb,
lib/anybase/version.rb

Constant Summary collapse

UnrecognizedCharacterError =
Class.new(StandardError)
NegativeSignListedAsDigitError =
Class.new(StandardError)
NegativeSignTooLongError =
Class.new(StandardError)
UnknownNegativeSignError =
Class.new(StandardError)
Hex =
Anybase.new("0123456789abcdef", ignore_case: true)
Base62 =
Anybase.new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
Base64 =
Anybase.new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
Base64ForURL =
Anybase.new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
Base73ForURL =
Anybase.new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$-_.+!*'(),")
VERSION =
"0.1.0".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digit_string, ignore_case: false, negative_sign: nil, synonyms: {}) ⇒ Anybase

Returns a new instance of Anybase.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/anybase.rb', line 14

def initialize(digit_string, ignore_case: false, negative_sign: nil, synonyms: {})
  raise NegativeSignTooLongError if negative_sign && negative_sign.size > 1
  raise NegativeSignListedAsDigitError if negative_sign && digit_string.index(negative_sign)

  self.digits = digit_string.dup
  self.ignore_case = ignore_case
  self.negative_sign = negative_sign
  self.synonyms = synonyms
  self.synonyms_tr = [String.new, String.new]
  self.char_map = Hash.new { |_h,k| raise UnrecognizedCharacterError, "the character `#{k}' is not included in #{digits}" }
  self.num_map = {}

  digits.downcase! if ignore_case?

  regexp_str = String.new("[")
  digits.each_char.with_index do |char, i|
    regexp_str << Regexp.quote(char)
    char_map[char] = i
    num_map[i] = char

    next unless synonyms[char]

    synonyms[char].each_char do |synonym|
      regexp_str << Regexp.quote(synonym)
      synonyms_tr[0] << synonym
      synonyms_tr[1] << char
    end
  end
  regexp_str << "]+"
  self.regexp = ignore_case? ? Regexp.new(regexp_str, Regexp::IGNORECASE) : Regexp.new(regexp_str)
end

Instance Attribute Details

#char_mapObject

Returns the value of attribute char_map.



12
13
14
# File 'lib/anybase.rb', line 12

def char_map
  @char_map
end

#digitsObject

Returns the value of attribute digits.



12
13
14
# File 'lib/anybase.rb', line 12

def digits
  @digits
end

#negative_signObject

Returns the value of attribute negative_sign.



12
13
14
# File 'lib/anybase.rb', line 12

def negative_sign
  @negative_sign
end

#num_mapObject

Returns the value of attribute num_map.



12
13
14
# File 'lib/anybase.rb', line 12

def num_map
  @num_map
end

#regexpObject

Returns the value of attribute regexp.



12
13
14
# File 'lib/anybase.rb', line 12

def regexp
  @regexp
end

Instance Method Details

#ignore_case?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/anybase.rb', line 54

def ignore_case?
  ignore_case
end

#match(str) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/anybase.rb', line 46

def match(str)
  if (match = regexp.match(str)) && match.begin(0).zero?
    match[0]
  else
    nil
  end
end

#normalize(val) ⇒ Object



62
63
64
65
# File 'lib/anybase.rb', line 62

def normalize(val)
  val = ignore_case? ? val.downcase : val.dup
  synonyms.empty? ? val : val.tr(*synonyms_tr)
end

#random(length, trim_leading_zeros: false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/anybase.rb', line 67

def random(length, trim_leading_zeros: false)
  number = String.new
  length.times { number << digits[SecureRandom.random_number(digits.size)] }

  if trim_leading_zeros
    number.sub!(/\A#{Regexp.quote(digits[0])}+/, "")
    number = digits[0] if number.empty?
  end

  number
end

#size(length) ⇒ Object



58
59
60
# File 'lib/anybase.rb', line 58

def size(length)
  digits.length**length
end

#to_i(val) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/anybase.rb', line 79

def to_i(val)
  val = normalize(val)
  num = 0
  op = if negative_sign && (val[0] == negative_sign[0])
    val.slice!(0, 1)
    :-
  else
    :+
  end

  (0...val.size).each do |i|
    num = num.send(op, (digits.size**(val.size - i - 1)) * char_map[val[i]])
  end

  num
end

#to_native(val, zero_pad: 1) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/anybase.rb', line 96

def to_native(val, zero_pad: 1)
  zero_pad = 1 if zero_pad < 1
  negative = if val < 0
    raise UnknownNegativeSignError unless negative_sign

    val = val.abs
    true
  else
    false
  end

  str = String.new
  until val.zero?
    digit = val % digits.size
    val /= digits.size
    str[0, 0] = num_map[digit]
  end

  str[0, 0] = num_map[0] * (zero_pad - str.size) if str.size < zero_pad
  str[0, 0] = negative_sign if negative

  str
end