all-your-base 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
Binary file
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Rusty Burchfield
2
+ Copyright (c) 2009 Shane Wolf
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = all-your-base
2
+
3
+ These are extensions to Integer#to_s and String#to_i that allow bases larger than 36.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Rusty Burchfield. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "all-your-base"
8
+ gem.summary = %Q{Numeric base conversions greater than base 36}
9
+ gem.description = %Q{Provides numeric base conversions greater than base 36}
10
+ gem.email = "GICodeWarrior@gmail.com"
11
+ gem.homepage = "http://github.com/GICodeWarrior/all-your-base"
12
+ gem.authors = ["Rusty Burchfield"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "all-your-base #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{all-your-base}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Rusty Burchfield"]
12
+ s.date = %q{2009-10-14}
13
+ s.description = %q{Provides numeric base conversions greater than base 36}
14
+ s.email = %q{GICodeWarrior@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "AllYourBaseAnimated.gif",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "all-your-base.gemspec",
28
+ "lib/all_your_base.rb",
29
+ "lib/all_your_base/are.rb",
30
+ "lib/all_your_base/are/belong_to_us.rb",
31
+ "spec/all_your_base/are/belong_to_us_spec.rb",
32
+ "spec/all_your_base/are_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/GICodeWarrior/all-your-base}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Numeric base conversions greater than base 36}
40
+ s.test_files = [
41
+ "spec/spec_helper.rb",
42
+ "spec/all_your_base/are/belong_to_us_spec.rb",
43
+ "spec/all_your_base/are_spec.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ end
58
+ end
@@ -0,0 +1 @@
1
+ require 'all_your_base/are'
@@ -0,0 +1,91 @@
1
+ module AllYourBase
2
+ class Are
3
+
4
+ # This charset works for "standard" bases 2-36 and 62. It also provides
5
+ # non-standard bases 1 and 37-61 for most uses.
6
+ BASE_62_CHARSET = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
7
+
8
+ # This is the base64 encoding charset, but note that this library does not
9
+ # provide true base64 encoding.
10
+ BASE_64_CHARSET = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a +
11
+ ['+', '/']
12
+
13
+ # This is a _maximum_ URL safe charset (between /'s). Not all sites know
14
+ # or care about the validity of these characters.
15
+ BASE_78_CHARSET = BASE_62_CHARSET + ['!', '$', '&', "'", '(', ')', '*', '+',
16
+ ',', '-', '.', ':', ';', '=', '@', '_']
17
+
18
+ def initialize(charset, options={})
19
+ options[:radix] ||= charset.size
20
+ if charset.size < 1 || charset.size < options[:radix]
21
+ raise ArgumentError.new('charset too small ' << charset.size.to_s)
22
+ elsif options[:radix] < 1
23
+ raise ArgumentError.new('illegal radix ' << options[:radix].to_s)
24
+ elsif charset.include?('-') && options[:honor_negation]
25
+ raise ArgumentError.new('"-" is unsupported in charset when honor_negation is set')
26
+ end
27
+
28
+ @charset = charset
29
+ @options = options
30
+ end
31
+
32
+ def convert_to_base_10(string)
33
+ negate = false
34
+ if @options[:honor_negation]
35
+ negate = string[0...1] == '-'
36
+ string = string[1...string.size] if negate
37
+ end
38
+
39
+ if string.size < 1
40
+ raise ArgumentError.new('string too small ' << string.size.to_s)
41
+ end
42
+ if !string.match(/\A[#{Regexp.escape(@charset[0...@options[:radix]].join(''))}]+\Z/)
43
+ raise ArgumentError.new('invalid characters')
44
+ end
45
+ regexp = Regexp.new(@charset.map{|c| Regexp.escape(c)}.join('|'))
46
+ result = 0
47
+ index = 0
48
+ string.reverse.scan(regexp) do |c|
49
+ result += @charset.index(c) * (@options[:radix] ** index)
50
+ index += 1
51
+ end
52
+ return result * (negate ? -1 : 1)
53
+ end
54
+
55
+ def self.convert_to_base_10(string, charset, options={})
56
+ ayb = self.new(charset, options)
57
+ ayb.convert_to_base_10(string)
58
+ end
59
+
60
+ def self.convert_from_base_10(int, charset, options={})
61
+ ayb = self.new(charset, options)
62
+ ayb.convert_from_base_10(int)
63
+ end
64
+
65
+ def convert_from_base_10(int)
66
+ if !int.to_s.match(/\A-?[0-9]+\Z/)
67
+ raise ArgumentError.new('invalid characters')
68
+ end
69
+ int = int.to_i
70
+ return '0' if int == 0
71
+
72
+ negate = false
73
+ if @options[:honor_negation]
74
+ negate = int < 0
75
+ end
76
+ int = int.abs
77
+
78
+ if @options[:radix] == 1
79
+ result = @charset.first * int
80
+ else
81
+ s = ''
82
+ while int > 0
83
+ s << @charset[int.modulo(@options[:radix])]
84
+ int /= @options[:radix]
85
+ end
86
+ result = s.reverse
87
+ end
88
+ return ((negate ? '-' : '') << result)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,33 @@
1
+ require 'all_your_base'
2
+
3
+ module AllYourBase
4
+ class Are
5
+ module BelongToUs
6
+ module From
7
+ def method_missing(sym, *args, &block)
8
+ if sym.to_s.match(/\Afrom_base_([0-9]+)\Z/)
9
+ AllYourBase::Are.convert_to_base_10(self.to_s, AllYourBase::Are::BASE_78_CHARSET, {:radix => $1.to_i})
10
+ else
11
+ super # NoMethodError
12
+ end
13
+ end
14
+ end
15
+
16
+ module To
17
+ def method_missing(sym, *args, &block)
18
+ if sym.to_s.match(/\Ato_base_([0-9]+)\Z/)
19
+ AllYourBase::Are.convert_from_base_10(self.to_i, AllYourBase::Are::BASE_78_CHARSET, {:radix => $1.to_i})
20
+ else
21
+ super # NoMethodError
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ String.send :include, AllYourBase::Are::BelongToUs::From
30
+ String.send :include, AllYourBase::Are::BelongToUs::To
31
+ Integer.send :include, AllYourBase::Are::BelongToUs::From
32
+ Integer.send :include, AllYourBase::Are::BelongToUs::To
33
+
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'all_your_base/are/belong_to_us'
3
+
4
+ describe AllYourBase::Are::BelongToUs::From do
5
+ it "should convert string to base 10" do
6
+ 'foo'.from_base_61.should eql(155661)
7
+ end
8
+
9
+ it "should convert integer to base 10" do
10
+ 112233.from_base_61.should eql(858903732)
11
+ end
12
+ it "should raise if the base is over 78"
13
+ it "should raise if the base is 0"
14
+ end
15
+
16
+ describe AllYourBase::Are::BelongToUs::To do
17
+ it "should convert string from base 10" do
18
+ '155661'.to_base_61.should eql('foo')
19
+ end
20
+ it "should convert integer from base 10" do
21
+ 155661.to_base_61.should eql('foo')
22
+ end
23
+ it "should raise if the base is over 78"
24
+ it "should raise if the base is 0"
25
+ end
26
+
27
+ describe AllYourBase::Are::BelongToUs do
28
+ it "should do complex conversions" do
29
+ 'foo'.from_base_64.to_base_14.should eql('46558')
30
+ end
31
+ it "should allow you to do stupid stuff" do
32
+ 'foo'.from_base_78.to_base_14.from_base_14.to_base_78.should eql('foo')
33
+ end
34
+ end
@@ -0,0 +1,109 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AllYourBase::Are do
4
+ it "should have consistent value for BASE_62_CHARSET" do
5
+ AllYourBase::Are::BASE_62_CHARSET.join(',').should eql("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z")
6
+ end
7
+ it "should have consistent value for BASE_64_CHARSET" do
8
+ AllYourBase::Are::BASE_64_CHARSET.join(',').should eql("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/")
9
+ end
10
+ it "should have consistent value for BASE_78_CHARSET" do
11
+ AllYourBase::Are::BASE_78_CHARSET.join(',').should eql("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,!,$,&,',(,),*,+,,,-,.,:,;,=,@,_")
12
+ end
13
+
14
+ describe "#initialize" do
15
+ it "should not allow charsets less 1" do
16
+ lambda {ayb = AllYourBase::Are.new([], {:radix =>1})}.should raise_error(ArgumentError, 'charset too small 0')
17
+ end
18
+ it "should not allow radix of less than 1" do
19
+ lambda {ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET, {:radix =>0})}.should raise_error(ArgumentError, 'illegal radix 0')
20
+ end
21
+ it "should set the charset instance variable" do
22
+ ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET, {:radix =>78})
23
+ ayb.instance_variable_get('@charset').should eql(AllYourBase::Are::BASE_78_CHARSET)
24
+ end
25
+ it "should set the options instance variable" do
26
+ ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET, {:radix => 12})
27
+ ayb.instance_variable_get('@options').should == {:radix => 12}
28
+ end
29
+ it "should default radix to size of charset" do
30
+ ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET)
31
+ ayb.instance_variable_get('@options')[:radix].should eql(78)
32
+ end
33
+ it "should not allow a char set with - when :honor_negation is set" do
34
+ lambda {
35
+ AllYourBase::Are.new(['a', 'y', 'b', '-'], {:honor_negation => true})
36
+ }.should raise_error(ArgumentError, '"-" is unsupported in charset when honor_negation is set')
37
+ end
38
+ end
39
+
40
+ describe "#convert_to_base_10" do
41
+ before(:each) do
42
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET)
43
+ end
44
+ it "should raise if input is contains invalid characters" do
45
+ ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET, {:radix => 5})
46
+ lambda {ayb.convert_to_base_10('foo')}.should raise_error("invalid characters")
47
+ end
48
+ it "should raise error if string is too short" do
49
+ lambda {@ayb.convert_to_base_10('')}.should raise_error(ArgumentError, 'string too small 0')
50
+ end
51
+ it "should return the base 10 conversion of string" do
52
+ @ayb.convert_to_base_10('somebody_set_up_us_the_bomb').should eql(855149198991141649141572449638390201857110945891509)
53
+ end
54
+ describe "when honor_negation is true" do
55
+ before(:each) do
56
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_62_CHARSET, {:honor_negation => true})
57
+ end
58
+ it "should raise error if string is too short" do
59
+ lambda {@ayb.convert_to_base_10('-')}.should raise_error(ArgumentError, 'string too small 0')
60
+ end
61
+ it "should return a negative integer if string starts with a -" do
62
+ @ayb.convert_to_base_10('-A').should eql(-10)
63
+ end
64
+ it "should return a positive integer if string does not start with a -" do
65
+ @ayb.convert_to_base_10('A').should eql(10)
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "#convert_from_base_10" do
71
+ it "should raise if input is not base 10" do
72
+ ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET, {:radix => 5})
73
+ lambda {ayb.convert_from_base_10('foo')}.should raise_error("invalid characters")
74
+ end
75
+ it "should convert from base 10" do
76
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET)
77
+ @ayb.convert_from_base_10(855149198991141649141572449638390201857110945891509).should eql('somebody_set_up_us_the_bomb')
78
+ end
79
+ it "should return '0' if value is 0" do
80
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_78_CHARSET)
81
+ @ayb.convert_from_base_10(0).should eql('0')
82
+ end
83
+ it "should return a string with a - at the beginning if value is negative and negation is honored" do
84
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_64_CHARSET, {:honor_negation => true})
85
+ @ayb.convert_from_base_10(-100).should eql('-Bk')
86
+ end
87
+ it "should return value if radix is 1" do
88
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_64_CHARSET, {:radix =>1})
89
+ @ayb.convert_from_base_10(11).should eql('AAAAAAAAAAA')
90
+ end
91
+ it "should return other value if radix is not 1" do
92
+ @ayb = AllYourBase::Are.new(AllYourBase::Are::BASE_64_CHARSET,{:radix =>12})
93
+ @ayb.convert_from_base_10(11).should eql('L')
94
+ end
95
+ end
96
+
97
+ describe ".convert_to_base_10" do
98
+ it "should allow you to convert to base 10 without initializing an instance of AllYourBase::Are" do
99
+ result = AllYourBase::Are.convert_to_base_10('A', AllYourBase::Are::BASE_78_CHARSET)
100
+ result.should eql(10)
101
+ end
102
+ end
103
+ describe ".convert_from_base_10" do
104
+ it "should allow you to convert from base 10 without initializing an instance of AllYourBase::Are" do
105
+ result = AllYourBase::Are.convert_from_base_10(10, AllYourBase::Are::BASE_78_CHARSET)
106
+ result.should eql('A')
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'all_your_base'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: all-your-base
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Rusty Burchfield
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Provides numeric base conversions greater than base 36
26
+ email: GICodeWarrior@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - AllYourBaseAnimated.gif
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - all-your-base.gemspec
43
+ - lib/all_your_base.rb
44
+ - lib/all_your_base/are.rb
45
+ - lib/all_your_base/are/belong_to_us.rb
46
+ - spec/all_your_base/are/belong_to_us_spec.rb
47
+ - spec/all_your_base/are_spec.rb
48
+ - spec/spec_helper.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/GICodeWarrior/all-your-base
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Numeric base conversions greater than base 36
77
+ test_files:
78
+ - spec/spec_helper.rb
79
+ - spec/all_your_base/are/belong_to_us_spec.rb
80
+ - spec/all_your_base/are_spec.rb