Class: AllYourBase::Are
- Inherits:
-
Object
- Object
- AllYourBase::Are
- 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
- .convert_from_base_10(int, options = {}) ⇒ Object
- .convert_to_base_10(string, options = {}) ⇒ Object
Instance Method Summary collapse
- #convert_from_base_10(int, options = {}) ⇒ Object
- #convert_to_base_10(string, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Are
constructor
A new instance of Are.
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(={}) @default_options = DEFAULT_OPTIONS.merge() @default_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, ={}) @@ayb ||= self.new @@ayb.convert_from_base_10(int, ) 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, ={}) @@ayb ||= self.new @@ayb.convert_to_base_10(string, ) 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, ={}) = () 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 [:honor_negation] negate = int < 0 end int = int.abs if [:radix] == 1 result = [:charset].first * int else s = '' while int > 0 s << [:charset][int.modulo([:radix])] int /= [: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, ={}) = () negate = false if [: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([:charset][0...[:radix]].join(''))}]+\Z/) raise ArgumentError.new('invalid characters') end regexp = Regexp.new([:charset].map{|c| Regexp.escape(c)}.join('|')) result = 0 index = 0 string.reverse.scan(regexp) do |c| result += [:charset].index(c) * ([:radix] ** index) index += 1 end return result * (negate ? -1 : 1) end |