anybase 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +8 -0
  2. data/VERSION +1 -1
  3. data/lib/anybase.rb +26 -21
  4. data/spec/from_spec.rb +12 -1
  5. metadata +2 -2
data/README.rdoc CHANGED
@@ -18,6 +18,14 @@ As well, you can tell Anybase to ignore case.
18
18
  base.to_i('F0A')
19
19
  => 3850
20
20
 
21
+ Anybase can also have synonyms.
22
+
23
+ base = Anybase.new('0123456789abcdef', :synonyms => {'0' => 'oO'})
24
+ base.to_i('FoA')
25
+ => 3850
26
+ base.to_i('FOA')
27
+ => 3850
28
+
21
29
  Anybase can also zeropad your output with whatever your "zero" character is.
22
30
 
23
31
  Anybase.new("012345678").to_native(1234, :zero_pad => 8)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
data/lib/anybase.rb CHANGED
@@ -1,33 +1,39 @@
1
1
  class Anybase
2
2
 
3
- attr_reader :chars, :char_map, :num_map
4
-
5
3
  UnrecognizedCharacterError = Class.new(RuntimeError)
6
-
4
+ Hex = Anybase.new('0123456789abcdef', :ignore_case => true)
5
+ Base62 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
6
+ Base64 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
7
+ Base64ForURL = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
8
+ Base73ForURL = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$-_.+!*\'(),')
9
+
10
+ attr_reader :chars, :char_map, :num_map
11
+
7
12
  def initialize(chars, opts = nil)
8
13
  @chars = chars
9
14
  @ignore_case = opts && opts.key?(:ignore_case) ? opts[:ignore_case] : false
15
+ @synonyms = opts && opts[:synonyms]
10
16
  @char_map = Hash.new{|h,k| raise UnrecognizedCharacterError.new("the character `#{k.chr}' is not included in #{@chars}")}
11
17
  @num_map = {}
12
18
  @chars.split('').each_with_index do |c, i|
13
- if @ignore_case
14
- @char_map[c[0]] = i
15
- @char_map[c.swapcase[0]] = i
16
- else
17
- @char_map[c[0]] = i
18
- end
19
+ add_mapping(c, i)
19
20
  @num_map[i] = c
21
+ @synonyms[c].split('').each { |sc| add_mapping(sc, i) } if @synonyms && @synonyms[c]
20
22
  end
21
23
  end
22
-
24
+
25
+ def ignore_case?
26
+ @ignore_case
27
+ end
28
+
23
29
  def to_i(val)
24
30
  num = 0
25
- (0...val.size).each{|i|
31
+ (0...val.size).each{|i|
26
32
  num += (chars.size ** (val.size - i - 1)) * char_map[val[i]]
27
33
  }
28
34
  num
29
35
  end
30
-
36
+
31
37
  def to_native(val, options = nil)
32
38
  str = ''
33
39
  until val.zero?
@@ -40,12 +46,11 @@ class Anybase
40
46
  end
41
47
  str == '' ? num_map[0].dup : str
42
48
  end
43
-
44
- Hex = Anybase.new('0123456789abcdef', :ignore_case => true)
45
- Base62 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
46
- Base64 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
47
- Base64ForURL = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
48
- Base73ForURL = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$-_.+!*\'(),')
49
-
50
-
51
- end
49
+
50
+ def add_mapping(c, i)
51
+ char_map[c[0]] = i
52
+ char_map[c.swapcase[0]] = i if ignore_case?
53
+ end
54
+ private :add_mapping
55
+
56
+ end
data/spec/from_spec.rb CHANGED
@@ -11,7 +11,18 @@ describe Anybase, "from" do
11
11
  end
12
12
 
13
13
  it "should fold case" do
14
- Anybase.new("012345678abcd", :ignore_case => true).to_i('a23D5d0AbBc').should == Anybase.new("012345678abcd", :ignore_case => true).to_i('A23d5D0aBbC')
14
+ base = Anybase.new("012345678abcd", :ignore_case => true)
15
+ str = 'a23D5d0AbBc'
16
+ base.to_i(str).should == base.to_i(str.swapcase)
17
+ end
18
+
19
+ it "should use synonymous" do
20
+ Anybase.new("012345678", :synonyms => {'0' => 'oO'}).to_i('O235o').should == 1746
21
+ proc { Anybase.new("012345678", :synonyms => {'0' => 'o'}).to_i('235O') }.should raise_error(Anybase::UnrecognizedCharacterError)
22
+ end
23
+
24
+ it "should use synonymous & ignore case" do
25
+ Anybase.new("012345678", :synonyms => {'0' => 'o'}, :ignore_case => true).to_i('235O').should == 1746
15
26
  end
16
27
 
17
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anybase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-24 00:00:00 -05:00
12
+ date: 2010-02-02 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15