anybase 0.0.3 → 0.0.4

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 +13 -2
  2. data/VERSION +1 -1
  3. data/lib/anybase.rb +8 -7
  4. data/spec/from_spec.rb +4 -0
  5. metadata +2 -2
data/README.rdoc CHANGED
@@ -12,8 +12,19 @@ Here is an example of how you'd do hex in Anybase.
12
12
  base.to_native(123450)
13
13
  => "1e23a"
14
14
 
15
+ As well, you can tell Anybase to ignore case.
16
+
17
+ base = Anybase.new('0123456789abcdef', :ignore_case => true)
18
+ base.to_i('F0A')
19
+ => 3850
20
+
21
+ Anybase can also zeropad your output with whatever your "zero" character is.
22
+
23
+ Anybase.new("012345678").to_native(1234, :zero_pad => 8)
24
+ => '00001621'
25
+
15
26
  Anybase gives you a few built-in:
16
- Anybase::Hex, Anybase::Base64, Anybase::Base64ForURL and Anybase::Base62
27
+ <tt>Anybase::Hex</tt>, <tt>Anybase::Base64</tt>, <tt>Anybase::Base64ForURL</tt> and <tt>Anybase::Base62</tt>
17
28
  (should all be pretty self-explanatory)
18
29
 
19
- w00t, and cheers!
30
+ As well, there is an <tt>AnyBase::Base66ForURL</tt>. This includes <tt>.</tt> and <tt>~</tt> as well as the usual base64 url safe characters.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/lib/anybase.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  class Anybase
2
2
 
3
- attr_reader :chars
3
+ attr_reader :chars, :char_map, :num_map
4
4
 
5
5
  UnrecognizedCharacterError = Class.new(RuntimeError)
6
6
 
7
7
  def initialize(chars, opts = nil)
8
8
  @chars = chars
9
- @ignore_case = opts && opts[:ignore_case] || false
9
+ @ignore_case = opts && opts.key?(:ignore_case) ? opts[:ignore_case] : false
10
10
  @char_map = Hash.new{|h,k| raise UnrecognizedCharacterError.new("the character `#{k.chr}' is not included in #{@chars}")}
11
11
  @num_map = {}
12
12
  @chars.split('').each_with_index do |c, i|
@@ -23,7 +23,7 @@ class Anybase
23
23
  def to_i(val)
24
24
  num = 0
25
25
  (0...val.size).each{|i|
26
- num += (chars.size ** (val.size - i - 1)) * @char_map[val[i]]
26
+ num += (chars.size ** (val.size - i - 1)) * char_map[val[i]]
27
27
  }
28
28
  num
29
29
  end
@@ -33,17 +33,18 @@ class Anybase
33
33
  until val.zero?
34
34
  digit = val % chars.size
35
35
  val /= chars.size
36
- str[0, 0] = @num_map[digit]
36
+ str[0, 0] = num_map[digit]
37
37
  end
38
38
  if options && options[:zero_pad]
39
- str[0, 0] = @num_map[0] * (options[:zero_pad] - str.size)
39
+ str[0, 0] = num_map[0] * (options[:zero_pad] - str.size)
40
40
  end
41
- str == '' ? @num_map[0].dup : str
41
+ str == '' ? num_map[0].dup : str
42
42
  end
43
43
 
44
44
  Hex = Anybase.new('0123456789abcdef', :ignore_case => true)
45
+ Base62 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
45
46
  Base64 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
46
47
  Base64ForURL = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')
47
- Base62 = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
48
+ Base66ForURL = Anybase.new('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~')
48
49
 
49
50
  end
data/spec/from_spec.rb CHANGED
@@ -10,4 +10,8 @@ describe Anybase, "from" do
10
10
  proc { Anybase.new("012345678").to_i('a2350') }.should raise_error(Anybase::UnrecognizedCharacterError)
11
11
  end
12
12
 
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')
15
+ end
16
+
13
17
  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.3
4
+ version: 0.0.4
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: 2009-12-10 00:00:00 -05:00
12
+ date: 2010-01-23 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15