lib/valerii.rb in ariejan-valerii-1.0.0 vs lib/valerii.rb in ariejan-valerii-1.1.0
- old
+ new
@@ -1,29 +1,25 @@
require 'enumerator'
class Valerii
- # The encode character set
+
ENCODE_CHARS =
- %w( 0 1 2 3 4 5 6 7
- 8 9 A B C D E F
- G H J K M N P Q
- R S T V W X Y Z
- a b c d e f h i
- j k l m n o p q
- r s t u v w x y
- z = - + [ ] $ # )
+ %w( B C D F G H J K
+ M N P Q R S T V
+ W Z b c d f h j
+ k m n p r x t v )
DECODE_MAP = ENCODE_CHARS.to_enum(:each_with_index).inject({}) do |h,(c,i)|
h[c] = i; h
end
# Encodes an integer into a string
#
# Valerii.encode(1234) # => "KJ"
#
def self.encode(number, opts = {})
- str = number.to_s(2).reverse.scan(/.{1,6}/).map do |bits|
+ str = number.to_s(2).reverse.scan(/.{1,5}/).map do |bits|
ENCODE_CHARS[bits.reverse.to_i(2)]
end.reverse.join
return str
end
@@ -33,8 +29,8 @@
# Valerii.decode("KJ") # => 1234
#
def self.decode(string)
string.split(//).map { |char|
DECODE_MAP[char] or return nil
- }.inject(0) { |result,val| (result << 6) + val }
+ }.inject(0) { |result,val| (result << 5) + val }
end
end