anybase 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -32,6 +32,12 @@ If you just want to translate the number to look at it you can use #normalize
32
32
  base.normalize('FoA')
33
33
  => 'f0a'
34
34
 
35
+ If you want to use negative numbers, assign the sign character using the sign option
36
+
37
+ base = Anybase.new('0123456789abcdef', :sign => '-')
38
+ base.to_native(-12345)
39
+ => '-3039'
40
+
35
41
  Anybase can also zeropad your output with whatever your "zero" character is.
36
42
 
37
43
  Anybase.new("012345678").to_native(1234, :zero_pad => 8)
@@ -1,3 +1,3 @@
1
1
  class Anybase
2
- VERSION = '0.0.10'.freeze
2
+ VERSION = '0.0.11'.freeze
3
3
  end
data/lib/anybase.rb CHANGED
@@ -9,6 +9,8 @@ class Anybase
9
9
  def initialize(chars, opts = nil)
10
10
  @chars = chars
11
11
  @ignore_case = opts && opts.key?(:ignore_case) ? opts[:ignore_case] : false
12
+ @sign = opts && opts.key?(:sign) ? opts[:sign] : nil
13
+ raise if @sign && @chars.index(@sign)
12
14
  @synonyms = opts && opts[:synonyms]
13
15
  @synonyms_tr = ['', ''] if @synonyms
14
16
  @char_map = Hash.new{|h,k| raise UnrecognizedCharacterError.new("the character `#{k.chr}' is not included in #{@chars}")}
@@ -54,14 +56,28 @@ class Anybase
54
56
 
55
57
  def to_i(val)
56
58
  val = normalize(val)
59
+ op = if @sign and val[0] == @sign[0]
60
+ val.slice!(0, 1)
61
+ :-
62
+ else
63
+ :+
64
+ end
57
65
  num = 0
58
66
  (0...val.size).each{|i|
59
- num += (chars.size ** (val.size - i - 1)) * char_map[val[i]]
67
+ num = num.send(op, (chars.size ** (val.size - i - 1)) * char_map[val[i]])
60
68
  }
61
69
  num
62
70
  end
63
71
 
64
72
  def to_native(val, options = nil)
73
+ if val < 0
74
+ if @sign
75
+ val = val.abs
76
+ signed = true
77
+ else
78
+ raise
79
+ end
80
+ end
65
81
  str = ''
66
82
  until val.zero?
67
83
  digit = val % chars.size
@@ -71,7 +87,7 @@ class Anybase
71
87
  if options && options[:zero_pad]
72
88
  str[0, 0] = num_map[0] * (options[:zero_pad] - str.size)
73
89
  end
74
- str == '' ? num_map[0].dup : str
90
+ str == '' ? num_map[0].dup : (signed ? @sign.dup << str : str)
75
91
  end
76
92
 
77
93
  def add_mapping(c, i)
data/spec/from_spec.rb CHANGED
@@ -16,6 +16,10 @@ describe Anybase, "from" do
16
16
  base.to_i(str).should == base.to_i(str.swapcase)
17
17
  end
18
18
 
19
+ it "should translate negative numbers with a sign" do
20
+ Anybase.new("012345678", :sign => '9').to_i('911').should == -10
21
+ end
22
+
19
23
  it "should use synonymous" do
20
24
  Anybase.new("012345678", :synonyms => {'0' => 'oO'}).to_i('O235o').should == 1746
21
25
  proc { Anybase.new("012345678", :synonyms => {'0' => 'o'}).to_i('235O') }.should raise_error(Anybase::UnrecognizedCharacterError)
data/spec/to_spec.rb CHANGED
@@ -10,6 +10,14 @@ describe Anybase, "to" do
10
10
  Anybase.new("012345678").to_native(0).should == "0"
11
11
  end
12
12
 
13
+ it "should normally raise on negative numbers" do
14
+ proc { Anybase.new("012345678").to_native(-10)}.should raise_error
15
+ end
16
+
17
+ it "should translate negative numbers with a sign" do
18
+ Anybase.new("012345678", :sign => '9').to_native(-10).should == '911'
19
+ end
20
+
13
21
  it "should zeropad" do
14
22
  result = Anybase.new("012345678").to_native(1234, :zero_pad => 8)
15
23
  result.size.should == 8
data/spec/util_spec.rb CHANGED
@@ -10,4 +10,8 @@ describe Anybase, "from" do
10
10
  it "should normalize a number" do
11
11
  Anybase.new("01", :synonyms => {'0' => 'o', '1' => 'l'}, :ignore_case => true).normalize("l10oO1o").should == '1100010'
12
12
  end
13
+
14
+ it "raise if the sign is in the chars" do
15
+ proc{ Anybase.new("01", :sign => '0') }.should raise_error
16
+ end
13
17
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anybase
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 10
10
- version: 0.0.10
9
+ - 11
10
+ version: 0.0.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-26 00:00:00 -04:00
18
+ date: 2010-07-26 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21