anybase 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -1
- data/Rakefile +3 -14
- data/anybase.gemspec +31 -0
- data/lib/anybase.rb +18 -2
- data/lib/anybase/version.rb +3 -0
- data/spec/util_spec.rb +4 -1
- metadata +12 -4
data/README.rdoc
CHANGED
@@ -20,12 +20,18 @@ As well, you can tell Anybase to ignore case.
|
|
20
20
|
|
21
21
|
Anybase can also have synonyms.
|
22
22
|
|
23
|
-
base = Anybase.new('0123456789abcdef', :synonyms => {'0' => 'oO'})
|
23
|
+
base = Anybase.new('0123456789abcdef', :synonyms => {'0' => 'oO'}, :ignore_case => true)
|
24
24
|
base.to_i('FoA')
|
25
25
|
=> 3850
|
26
26
|
base.to_i('FOA')
|
27
27
|
=> 3850
|
28
28
|
|
29
|
+
If you just want to translate the number to look at it you can use #normalize
|
30
|
+
|
31
|
+
base = Anybase.new('0123456789abcdef', :synonyms => {'0' => 'oO'}, :ignore_case => true)
|
32
|
+
base.normalize('FoA')
|
33
|
+
=> 'f0a'
|
34
|
+
|
29
35
|
Anybase can also zeropad your output with whatever your "zero" character is.
|
30
36
|
|
31
37
|
Anybase.new("012345678").to_native(1234, :zero_pad => 8)
|
data/Rakefile
CHANGED
@@ -1,17 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
s.name = "anybase"
|
5
|
-
s.description = s.summary = "Numbers from anybase to anybase"
|
6
|
-
s.email = "joshbuddy@gmail.com"
|
7
|
-
s.homepage = "http://github.com/joshbuddy/anybase"
|
8
|
-
s.authors = ["Joshua Hull"]
|
9
|
-
s.files = FileList["[A-Z]*", "{lib,spec}/**/*"]
|
10
|
-
end
|
11
|
-
Jeweler::GemcutterTasks.new
|
12
|
-
rescue LoadError
|
13
|
-
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
14
|
-
end
|
1
|
+
require 'ext/gem_rake'
|
2
|
+
|
3
|
+
Bundler::GemHelper.install_tasks
|
15
4
|
|
16
5
|
require 'spec'
|
17
6
|
require 'spec/rake/spectask'
|
data/anybase.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'anybase', 'version'))
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "anybase"
|
5
|
+
s.version = Anybase::VERSION
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Joshua Hull"]
|
9
|
+
s.date = "2010-07-26"
|
10
|
+
s.description = %q{Numbers from anybase to anybase}
|
11
|
+
s.email = %q{joshbuddy@gmail.com}
|
12
|
+
s.extra_rdoc_files = ['README.rdoc']
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.homepage = %q{http://github.com/joshbuddy/anybase}
|
15
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubygems_version = %q{1.3.6}
|
18
|
+
s.summary = %q{Numbers from anybase to anybase}
|
19
|
+
s.test_files = `git ls-files`.split("\n").select{|f| f[/^spec/]}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
data/lib/anybase.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'anybase/version'
|
2
|
+
|
1
3
|
class Anybase
|
2
4
|
|
3
5
|
UnrecognizedCharacterError = Class.new(RuntimeError)
|
@@ -8,12 +10,21 @@ class Anybase
|
|
8
10
|
@chars = chars
|
9
11
|
@ignore_case = opts && opts.key?(:ignore_case) ? opts[:ignore_case] : false
|
10
12
|
@synonyms = opts && opts[:synonyms]
|
13
|
+
@synonyms_tr = ['', ''] if @synonyms
|
11
14
|
@char_map = Hash.new{|h,k| raise UnrecognizedCharacterError.new("the character `#{k.chr}' is not included in #{@chars}")}
|
12
15
|
@num_map = {}
|
16
|
+
if ignore_case?
|
17
|
+
@chars.downcase!
|
18
|
+
end
|
13
19
|
@chars.split('').each_with_index do |c, i|
|
14
20
|
add_mapping(c, i)
|
15
21
|
@num_map[i] = c
|
16
|
-
|
22
|
+
if @synonyms && @synonyms[c]
|
23
|
+
@synonyms[c].split('').each { |sc|
|
24
|
+
@synonyms_tr[1] << c
|
25
|
+
@synonyms_tr[0] << sc
|
26
|
+
}
|
27
|
+
end
|
17
28
|
end
|
18
29
|
end
|
19
30
|
|
@@ -25,6 +36,11 @@ class Anybase
|
|
25
36
|
chars.length ** digits
|
26
37
|
end
|
27
38
|
|
39
|
+
def normalize(val)
|
40
|
+
val = val.downcase if ignore_case?
|
41
|
+
@synonyms ? val.tr(*@synonyms_tr) : val
|
42
|
+
end
|
43
|
+
|
28
44
|
def random(digits, opts = nil)
|
29
45
|
zero_pad = opts && opts.key?(:zero_pad) ? opts[:zero_pad] : true
|
30
46
|
number = ''
|
@@ -37,6 +53,7 @@ class Anybase
|
|
37
53
|
end
|
38
54
|
|
39
55
|
def to_i(val)
|
56
|
+
val = normalize(val)
|
40
57
|
num = 0
|
41
58
|
(0...val.size).each{|i|
|
42
59
|
num += (chars.size ** (val.size - i - 1)) * char_map[val[i]]
|
@@ -59,7 +76,6 @@ class Anybase
|
|
59
76
|
|
60
77
|
def add_mapping(c, i)
|
61
78
|
char_map[c[0]] = i
|
62
|
-
char_map[c.swapcase[0]] = i if ignore_case?
|
63
79
|
end
|
64
80
|
private :add_mapping
|
65
81
|
|
data/spec/util_spec.rb
CHANGED
@@ -7,4 +7,7 @@ describe Anybase, "from" do
|
|
7
7
|
(Anybase.new("012345678").size(10) * Anybase.new("012345678").size(10)).should == Anybase.new("012345678").size(20)
|
8
8
|
end
|
9
9
|
|
10
|
-
|
10
|
+
it "should normalize a number" do
|
11
|
+
Anybase.new("01", :synonyms => {'0' => 'o', '1' => 'l'}, :ignore_case => true).normalize("l10oO1o").should == '1100010'
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anybase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 10
|
10
|
+
version: 0.0.10
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Joshua Hull
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-07-26 00:00:00 -04:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -30,7 +31,9 @@ files:
|
|
30
31
|
- README.rdoc
|
31
32
|
- Rakefile
|
32
33
|
- VERSION
|
34
|
+
- anybase.gemspec
|
33
35
|
- lib/anybase.rb
|
36
|
+
- lib/anybase/version.rb
|
34
37
|
- spec/from_spec.rb
|
35
38
|
- spec/spec.opts
|
36
39
|
- spec/spec_helper.rb
|
@@ -46,28 +49,33 @@ rdoc_options:
|
|
46
49
|
require_paths:
|
47
50
|
- lib
|
48
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
49
53
|
requirements:
|
50
54
|
- - ">="
|
51
55
|
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
52
57
|
segments:
|
53
58
|
- 0
|
54
59
|
version: "0"
|
55
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
56
62
|
requirements:
|
57
63
|
- - ">="
|
58
64
|
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
59
66
|
segments:
|
60
67
|
- 0
|
61
68
|
version: "0"
|
62
69
|
requirements: []
|
63
70
|
|
64
71
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.3.
|
72
|
+
rubygems_version: 1.3.7
|
66
73
|
signing_key:
|
67
74
|
specification_version: 3
|
68
75
|
summary: Numbers from anybase to anybase
|
69
76
|
test_files:
|
70
77
|
- spec/from_spec.rb
|
78
|
+
- spec/spec.opts
|
71
79
|
- spec/spec_helper.rb
|
72
80
|
- spec/to_spec.rb
|
73
81
|
- spec/util_spec.rb
|