Class: AllYourBase::Are

Inherits:
Object
  • Object
show all
Defined in:
lib/all_your_base/are.rb,
lib/all_your_base/are/belong_to_us.rb

Defined Under Namespace

Modules: BelongToUs

Constant Summary collapse

BASE_62_CHARSET =

This charset works for “standard” bases 2-36 and 62. It also provides non-standard bases 1 and 37-61 for most uses.

('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
BASE_64_CHARSET =

This is the base64 encoding charset, but note that this library does not provide true base64 encoding.

('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a +
['+', '/']
BASE_78_CHARSET =

This is a maximum URL safe charset (between /‘s). Not all sites know or care about the validity of these characters.

BASE_62_CHARSET + ['!', '$', '&', "'", '(', ')', '*', '+',
',', '-', '.', ':', ';', '=', '@', '_']
DEFAULT_OPTIONS =
{:charset => BASE_78_CHARSET, :honor_negation => false}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Are

Returns a new instance of Are.



20
21
22
23
# File 'lib/all_your_base/are.rb', line 20

def initialize(options={})
  @default_options = DEFAULT_OPTIONS.merge(options)
  @default_options = merge_and_validate_options
end

Class Method Details

.convert_from_base_10(int, options = {}) ⇒ Object



83
84
85
86
# File 'lib/all_your_base/are.rb', line 83

def self.convert_from_base_10(int, options={})
  @@ayb ||= self.new
  @@ayb.convert_from_base_10(int, options)
end

.convert_to_base_10(string, options = {}) ⇒ Object



78
79
80
81
# File 'lib/all_your_base/are.rb', line 78

def self.convert_to_base_10(string, options={})
  @@ayb ||= self.new
  @@ayb.convert_to_base_10(string, options)
end

Instance Method Details

#convert_from_base_10(int, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/all_your_base/are.rb', line 50

def convert_from_base_10(int, options={})
  options = merge_and_validate_options(options)

  if !int.to_s.match(/\A-?[0-9]+\Z/)
    raise ArgumentError.new('invalid characters')
  end
  int = int.to_i
  return '0' if int == 0

  negate = false
  if options[:honor_negation]
    negate = int < 0
  end
  int = int.abs

  if options[:radix] == 1
    result = options[:charset].first * int
  else
    s = ''
    while int > 0
      s << options[:charset][int.modulo(options[:radix])]
      int /= options[:radix]
    end
    result = s.reverse
  end
  return ((negate ? '-' : '') << result)
end

#convert_to_base_10(string, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/all_your_base/are.rb', line 25

def convert_to_base_10(string, options={})
  options = merge_and_validate_options(options)

  negate = false
  if options[:honor_negation]
    negate = string[0...1] == '-'
    string = string[1...string.size] if negate
  end

  if string.size < 1
    raise ArgumentError.new('string too small ' << string.size.to_s)
  end
  if !string.match(/\A[#{Regexp.escape(options[:charset][0...options[:radix]].join(''))}]+\Z/)
    raise ArgumentError.new('invalid characters')
  end
  regexp = Regexp.new(options[:charset].map{|c| Regexp.escape(c)}.join('|'))
  result = 0
  index = 0
  string.reverse.scan(regexp) do |c|
    result += options[:charset].index(c) * (options[:radix] ** index)
    index += 1
  end
  return result * (negate ? -1 : 1)
end