Methods
convert_base
decode
encode
new
Constants
BASE62 | = | ["0".."9", "a".."z", "A".."Z"].map { |r| r.to_a }.flatten |
Attributes
[RW] | chars |
Public Class methods
[ + ]
# File lib/more/facets/basex.rb, line 7 def initialize(chars=BASE62) @chars = chars @base = @chars.size @values = Hash[*(0...@base).map { |i| [ @chars[i], i ] }.flatten] end
Public Instance methods
[ + ]
# File lib/more/facets/basex.rb, line 25 def convert_base(digits, from_base, to_base) bignum = 0 digits.each { |digit| bignum = bignum * from_base + digit } converted = [] until bignum.zero? bignum, digit = bignum.divmod to_base converted.push digit end converted.reverse end
[ + ]
# File lib/more/facets/basex.rb, line 19 def decode(encoded) convert_base(encoded.split('').map { |c| @values[c] }, @base, 256).pack("C*") end
[ + ]
# File lib/more/facets/basex.rb, line 13 def encode(byte_string) convert_base(byte_string.unpack("C*"), 256, @base).map { |d| @chars[d] }.join('') end