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 + ['!', '$', '&', "'", '(', ')', '*', '+', ',', '-', '.', ':', ';', '=', '@', '_']
Class Method Summary collapse
- .convert_from_base_10(int, charset, options = {}) ⇒ Object
- .convert_to_base_10(string, charset, options = {}) ⇒ Object
Instance Method Summary collapse
- #convert_from_base_10(int) ⇒ Object
- #convert_to_base_10(string) ⇒ Object
-
#initialize(charset, options = {}) ⇒ Are
constructor
A new instance of Are.
Constructor Details
#initialize(charset, options = {}) ⇒ Are
Returns a new instance of Are.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/all_your_base/are.rb', line 18 def initialize(charset, ={}) [:radix] ||= charset.size if charset.size < 1 || charset.size < [:radix] raise ArgumentError.new('charset too small ' << charset.size.to_s) elsif [:radix] < 1 raise ArgumentError.new('illegal radix ' << [:radix].to_s) elsif charset.include?('-') && [:honor_negation] raise ArgumentError.new('"-" is unsupported in charset when honor_negation is set') end @charset = charset @options = end |
Class Method Details
.convert_from_base_10(int, charset, options = {}) ⇒ Object
60 61 62 63 |
# File 'lib/all_your_base/are.rb', line 60 def self.convert_from_base_10(int, charset, ={}) ayb = self.new(charset, ) ayb.convert_from_base_10(int) end |
.convert_to_base_10(string, charset, options = {}) ⇒ Object
55 56 57 58 |
# File 'lib/all_your_base/are.rb', line 55 def self.convert_to_base_10(string, charset, ={}) ayb = self.new(charset, ) ayb.convert_to_base_10(string) end |
Instance Method Details
#convert_from_base_10(int) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/all_your_base/are.rb', line 65 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 @options[:honor_negation] negate = int < 0 end int = int.abs if @options[:radix] == 1 result = @charset.first * int else s = '' while int > 0 s << @charset[int.modulo(@options[:radix])] int /= @options[:radix] end result = s.reverse end return ((negate ? '-' : '') << result) end |
#convert_to_base_10(string) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/all_your_base/are.rb', line 32 def convert_to_base_10(string) 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(@charset[0...@options[: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) * (@options[:radix] ** index) index += 1 end return result * (negate ? -1 : 1) end |