to_insane 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.README.rdoc.swp ADDED
Binary file
data/.Rakefile.swp ADDED
Binary file
data/CHANGELOG ADDED
@@ -0,0 +1,11 @@
1
+ = 0.0.4
2
+ - Gracefully deals with bad input. Thanks to isnotvalid on Github.
3
+
4
+ = 0.0.3
5
+ - Removed accidental dependencies.
6
+
7
+ = 0.0.2
8
+ - Fixed typos.
9
+
10
+ = 0.0.1
11
+ - Initial release.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Douglas F Shearer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = To Insane!
2
+ A small String and Integer extender that allow you to use a base larger than 36 and use custom character sets for base conversion.
3
+ Default max of 67.
4
+ Also include url safe integer conversion.
5
+
6
+ === Donate
7
+
8
+ I did not really make this library I just modified it off this guy. Donate your money to him.
9
+
10
+ If you find this library useful, please consider a donation to show your support!
11
+
12
+ http://www.paypal.com/cgi-bin/webscr?cmd=_send-money
13
+
14
+ Paypal address: mailto:dougal.s@gmail.com
15
+
16
+ === Install
17
+
18
+ $ sudo gem install to_insane
19
+
20
+ == Usage
21
+
22
+ require 'rubygems'
23
+ require 'to_insane'
24
+
25
+ # Int to insane (Default: Base 58)
26
+ 1337.to_insane # => n3
27
+
28
+ # From insane to Int (Default: Base 58)
29
+ 'A2Ph'.from_insane # => 7033619
30
+
31
+ # Different Bases
32
+ 'A2Ph'.from_insane(50) # => 4507467
33
+ 4507467.to_insane(50) # => 'A2Ph'
34
+
35
+ # Different Character Sets
36
+ 'A2Ph'.from_insane(4,'A2Ph') # => 27
37
+ 27.to_insane(4,'A2Ph')
38
+
39
+ # Other Inputs
40
+ # :url_safe uses characters that are RFC URL non reserved
41
+ # :max uses the number of characters
42
+ == Credits
43
+
44
+ Ryan Ong - http://ryangong.net
45
+ Douglas F Shearer - http://douglasfshearer.com
46
+
47
+ Test examples courtesy Fraser Speirs' Base58Encoder Objective-C class, http://gist.github.com/101674.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the to_insane library.'
9
+ Rake::TestTask.new do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/test_*.rb'
12
+ t.verbose = false
13
+ end
14
+
15
+ desc 'Generate RDoc documentation.'
16
+ Rake::RDocTask.new do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'to_insane'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gemspec|
27
+ gemspec.name = "to_insane"
28
+ gemspec.summary = "ToInsane allows you to convert strings and integers to a Base larger than the default max of 36 and use a custom character set"
29
+ gemspec.description = gemspec.summary
30
+ gemspec.email = "ryanong@gmail.com"
31
+ gemspec.homepage = "http://github.com/ryanong/to_insane"
32
+ gemspec.authors = ["Ryan Ong"]
33
+ end
34
+ Jeweler::GemcutterTasks.new
35
+ rescue LoadError
36
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
Binary file
Binary file
data/lib/to_insane.rb ADDED
@@ -0,0 +1,46 @@
1
+ # ToInsane
2
+ # Distributed under the MIT license as included with this plugin.
3
+ #
4
+
5
+ INSANECHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ-._~+[]|$!*(),{}^<>#%;/?:@&='
6
+
7
+ class String
8
+ # Converts a string to a base x integer using y character set.
9
+ def from_insane(base_val = 58, alpha = INSANECHARACTERS)
10
+ base_val = INSANECHARACTERS.size if base_val == :max
11
+ base_val = 63 if base_val == :url_safe
12
+ raise ArgumentError, "Base Value is not valid. Integer, :max, :url_safe" unless base_val.kind_of? Integer
13
+ raise ArgumentError, "Base size is too large for given character set. Please use a Base value larger than 0 and less than #{alpha.length}" if base_val > alpha.length or base_val <= 0
14
+ raise ArgumentError, "Character set needs to be larger than 0" if alpha.size <= 0
15
+ raise ArgumentError, "Character set has duplicate characters" if alpha.size != alpha.split(//).uniq.size
16
+ int_val = 0
17
+ char_ref = alpha[0..base_val]
18
+ self.reverse.split(//).each_with_index do |char,index|
19
+ raise ArgumentError, "Character #{char} at index #{index} is not a valid character for to_insane Base #{base_val} String." unless char_index = char_ref.index(char)
20
+ int_val += (char_index)*(base_val**(index))
21
+ end
22
+ int_val
23
+ end
24
+ end
25
+
26
+ class Integer
27
+ # Converts a base10 integer to a base x string.
28
+ def to_insane(base_val = 58, alpha = INSANECHARACTERS)
29
+ base_val = INSANECHARACTERS.size if base_val == :max
30
+ base_val = 63 if base_val == :url_safe
31
+ raise ArgumentError, "Base Value is not valid. Integer, :max, :url_safe" unless base_val.kind_of? Integer
32
+ raise ArgumentError, "Base size is too large for given character set. Please use a Base value larger than 0 and less than #{alpha.length}" if base_val > alpha.length or base_val <= 0
33
+ raise ArgumentError, "Character set needs to be larger than 0" if alpha.size <= 0
34
+ raise ArgumentError, "Character set has duplicate characters" if alpha.size != alpha.split(//).uniq.size
35
+ char_ref = alpha[0..base_val]
36
+ insane_val = ''
37
+ int_val = self
38
+ while(int_val >= base_val)
39
+ mod = int_val % base_val
40
+ insane_val = char_ref[mod,1] + insane_val
41
+ int_val = (int_val - mod)/base_val
42
+ end
43
+ char_ref[int_val,1] + insane_val
44
+ end
45
+ end
46
+
Binary file
@@ -0,0 +1,52 @@
1
+ require 'test/unit'
2
+ require 'lib/to_insane'
3
+
4
+ class ToInsaneTest < Test::Unit::TestCase
5
+
6
+ def test_integer_to_insane
7
+ 100.times do |integer|
8
+ 2.upto(36) do |base|
9
+ assert_equal integer.to_s(base), integer.to_insane(base)
10
+ end
11
+ end
12
+ 871092374.step(871292374,314) do |integer|
13
+ 2.upto(36) do |base|
14
+ assert_equal integer.to_s(base), integer.to_insane(base)
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ def test_string_from_insane
21
+ 100.times do |integer|
22
+ 2.upto(36) do |base|
23
+ assert_equal integer.to_s(base).to_i(base), integer.to_s(base).from_insane(base)
24
+ end
25
+ end
26
+ 871092374.step(871292374,314) do |integer|
27
+ 2.upto(36) do |base|
28
+ assert_equal integer.to_s(base).to_i(base), integer.to_s(base).from_insane(base)
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ def test_inverse_relationship
35
+ 871092374.step(871292374,314) do |integer|
36
+ 36.upto(67) do |base|
37
+ assert_equal integer.to_insane(base), integer.to_insane(base).from_insane(base)
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ def test_invalid_charset
44
+ e = assert_raise(ArgumentError) { 5.to_insane(5,'aaabe14123') }
45
+ assert_equal "Character set has duplicate characters", e.message
46
+ end
47
+
48
+ def test_invalid_base58_string
49
+ e = assert_raise(ArgumentError) { 'kjasd?'.from_insane }
50
+ end
51
+
52
+ end
data/to_insane.gemspec ADDED
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
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{to_insane}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Ong"]
12
+ s.date = %q{2011-02-08}
13
+ s.description = %q{ToInsane allows you to convert strings and integers to a Base larger than the default max of 36 and use a custom character set}
14
+ s.email = %q{ryanong@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".README.rdoc.swp",
20
+ ".Rakefile.swp",
21
+ "CHANGELOG",
22
+ "MIT-LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/.base58.rb.swp",
27
+ "lib/.to_insane.rb.swp",
28
+ "lib/to_insane.rb",
29
+ "test/.test_to_insane.rb.swp",
30
+ "test/test_to_insane.rb",
31
+ "to_insane.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/ryanong/to_insane}
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.5.0}
36
+ s.summary = %q{ToInsane allows you to convert strings and integers to a Base larger than the default max of 36 and use a custom character set}
37
+ s.test_files = [
38
+ "test/test_to_insane.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
50
+
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_insane
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Ong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-08 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: ToInsane allows you to convert strings and integers to a Base larger than the default max of 36 and use a custom character set
18
+ email: ryanong@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - .README.rdoc.swp
27
+ - .Rakefile.swp
28
+ - CHANGELOG
29
+ - MIT-LICENSE
30
+ - README.rdoc
31
+ - Rakefile
32
+ - VERSION
33
+ - lib/.base58.rb.swp
34
+ - lib/.to_insane.rb.swp
35
+ - lib/to_insane.rb
36
+ - test/.test_to_insane.rb.swp
37
+ - test/test_to_insane.rb
38
+ - to_insane.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/ryanong/to_insane
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.5.0
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: ToInsane allows you to convert strings and integers to a Base larger than the default max of 36 and use a custom character set
67
+ test_files:
68
+ - test/test_to_insane.rb