radix62 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in radix62.gemspec
4
+ gemspec
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ radix62 (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.0.0.beta.20)
11
+ rspec-core (= 2.0.0.beta.20)
12
+ rspec-expectations (= 2.0.0.beta.20)
13
+ rspec-mocks (= 2.0.0.beta.20)
14
+ rspec-core (2.0.0.beta.20)
15
+ rspec-expectations (2.0.0.beta.20)
16
+ diff-lcs (>= 1.1.2)
17
+ rspec-mocks (2.0.0.beta.20)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ bundler (~> 1.0.0)
24
+ radix62!
25
+ rspec (~> 2.0.0.beta20)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Matias Korhonen
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.
@@ -0,0 +1,35 @@
1
+ Radix62
2
+ =======
3
+
4
+ Convert integers to base 62 strings and back.
5
+
6
+ Base 62 includes the numbers 0-9 and characters A-Z (both lower and upper case). This can be useful for applications such as URL shorteners.
7
+
8
+ ## Installation
9
+
10
+ Just like any other gem:
11
+
12
+ gem install radix62
13
+
14
+ ## Usage
15
+
16
+ Two methods are provided, Radix62#encode62 and Radix62#decode62
17
+
18
+ require "radix62"
19
+ Radix62.encode62(1000) #=> "g8"
20
+ Radix62.encode62(9999999) #=> "FXsj"
21
+ Radix62.decode62("a") #=> "10"
22
+ Radix62.decode62("A") #=> "36"
23
+ Radix62.decode62("Abc123") #=> "33146185555"
24
+ Radix62.decode62(Radix62.encode62(1234567890)) #=> 1234567890
25
+
26
+ Radix62 also adds the `decode62` and `encode62` convenience methods to the String and Integer classes, respectively.
27
+
28
+ 1000.encode62 #=> "g8"
29
+ "Abc123".decode62 #=> "33146185555"
30
+
31
+ ## License and copyright
32
+
33
+ Copyright (c) 2010 Matias Korhonen
34
+
35
+ Licensed under the MIT license, see the LICENSE file for details.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,51 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "radix62/core_ext/string"
3
+ require "radix62/core_ext/integer"
4
+
5
+ # Convert integers to base 62 strings and back.
6
+ module Radix62
7
+ ALPHABET = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
8
+
9
+ # Encode an Integer to a base 62 string. The input value *must* be a positive
10
+ # integer.
11
+ #
12
+ # If the input number is not an Integer, a TypeError will be raised and if the
13
+ # input number is negative, a RangeError will be raised.
14
+ def self.encode62(number)
15
+ unless number.is_a? Integer
16
+ raise TypeError.new "number not an integer"
17
+ end
18
+
19
+ if number < 0
20
+ raise RangeError.new "number must be greater than or equal to 0"
21
+ elsif number == 0
22
+ return "0"
23
+ end
24
+
25
+ base62 = ""
26
+
27
+ while number > 0
28
+ base62 << ALPHABET[number.modulo(62)]
29
+ number /= 62
30
+ end
31
+
32
+ base62.reverse
33
+ end
34
+
35
+ # Decode a base 62 String to a base 10 Integer. The input value
36
+ # <b>must not</b> contain illegal characters.
37
+ #
38
+ # If the input is not alphanumeric, an ArgumentError will be raised.
39
+ def self.decode62(string)
40
+ if !!string.match(/^([a-z0-9]+)$/i)
41
+ integer = 0
42
+ string.split(//).reverse.each_with_index do |char,index|
43
+ place = ALPHABET.size ** index
44
+ integer += ALPHABET.find_index(char) * place
45
+ end
46
+ integer
47
+ else
48
+ raise ArgumentError.new "Input is not alphanumeric."
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ class Integer
3
+ def encode62
4
+ Radix62.encode62(self)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ class String
3
+ def decode62
4
+ Radix62.decode62(self)
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Radix62
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/radix62/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "radix62"
6
+ s.version = Radix62::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Matias Korhonen"]
9
+ s.email = ["matias@kiskolabs.com"]
10
+ s.homepage = "http://github.com/k33l0r/radix62"
11
+ s.summary = "Convert integers to base 62 strings and back."
12
+ s.description = "Convert base 10 integers to base 62 strings (or base 62 strings to base 10 integers)."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "radix62"
16
+
17
+ s.add_development_dependency "bundler", "~> 1.0.0"
18
+ s.add_development_dependency "rspec", "~> 2.0.0.beta20"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
22
+ s.require_path = 'lib'
23
+ end
@@ -0,0 +1,84 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe Radix62 do
5
+ context "alphabet" do
6
+ it "has the ALPHABET array" do
7
+ Radix62::ALPHABET.should be_kind_of Array
8
+ end
9
+
10
+ it "has 62 items" do
11
+ Radix62::ALPHABET.should_not be_empty
12
+ Radix62::ALPHABET.count.should == 62
13
+ end
14
+
15
+ it "has the right contents" do
16
+ Radix62::ALPHABET.should == ["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"]
17
+ end
18
+ end
19
+
20
+ context "encode62" do
21
+ it "raises an exception with negative numbers" do
22
+ (-1000..-1).step(100).each do |n|
23
+ lambda { Radix62.encode62(n) }.should raise_error(RangeError)
24
+ end
25
+ (-99999..-9999).step(1000).each do |n|
26
+ lambda { Radix62.encode62(n) }.should raise_error(RangeError)
27
+ end
28
+ (-9999999..-999999).step(10000).each do |n|
29
+ lambda { Radix62.encode62(n) }.should raise_error(RangeError)
30
+ end
31
+ (-333333333..-222222222).step(1000000).each do |n|
32
+ lambda { Radix62.encode62(n) }.should raise_error(RangeError)
33
+ end
34
+ end
35
+
36
+ it "fails if the input number is not an Integer" do
37
+ lambda { Radix62.encode62("123") }.should raise_error(TypeError)
38
+ lambda { Radix62.encode62(true) }.should raise_error(TypeError)
39
+ lambda { Radix62.encode62(:symbol) }.should raise_error(TypeError)
40
+ lambda { Radix62.encode62([1]) }.should raise_error(TypeError)
41
+ lambda { Radix62.encode62(0.1) }.should raise_error(TypeError)
42
+ end
43
+
44
+ it "has output which matches known good examples" do
45
+ KnownGood::ENCODE62.each do |key, value|
46
+ Radix62.encode62(key).should == value
47
+ end
48
+ end
49
+ end
50
+
51
+ context "decode62" do
52
+ it "raises an exception with invalid characters" do
53
+ lambda { Radix62.decode62("/") }.should raise_error(ArgumentError)
54
+ lambda { Radix62.decode62("123.0") }.should raise_error(ArgumentError)
55
+ lambda { Radix62.decode62("-100") }.should raise_error(ArgumentError)
56
+ lambda { Radix62.decode62("Ä") }.should raise_error(ArgumentError)
57
+ lambda { Radix62.decode62("ö") }.should raise_error(ArgumentError)
58
+ lambda { Radix62.decode62(" ") }.should raise_error(ArgumentError)
59
+ lambda { Radix62.decode62("123 000") }.should raise_error(ArgumentError)
60
+ lambda { Radix62.decode62("123,9") }.should raise_error(ArgumentError)
61
+ lambda { Radix62.decode62("123\t123") }.should raise_error(ArgumentError)
62
+ lambda { Radix62.decode62("1&1") }.should raise_error(ArgumentError)
63
+ lambda { Radix62.decode62("1%") }.should raise_error(ArgumentError)
64
+ end
65
+
66
+ it "has output which matches known good examples" do
67
+ KnownGood::DECODE62.each do |key, value|
68
+ Radix62.decode62(key).should == value
69
+ end
70
+ end
71
+ end
72
+
73
+ context "core_ext" do
74
+ it "extends the Integer class with #encode62" do
75
+ 9999.encode62.should be_kind_of String
76
+ 9999.encode62.should == "2Bh"
77
+ end
78
+
79
+ it "extends the String class with #decode62" do
80
+ "aBC".decode62.should be_kind_of Integer
81
+ "aBc".decode62.should == 40746
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require "bundler"
3
+ Bundler.setup
4
+
5
+ require "rspec"
6
+ require "support/known_good"
7
+ require "radix62"
8
+
9
+ RSpec.configure do |config|
10
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module KnownGood
3
+ ENCODE62 = Marshal.load(File.open("./spec/support/encode62_examples", "r"))
4
+ DECODE62 = Marshal.load(File.open("./spec/support/decode62_examples", "r"))
5
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: radix62
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Matias Korhonen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-04 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ - beta20
48
+ version: 2.0.0.beta20
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Convert base 10 integers to base 62 strings (or base 62 strings to base 10 integers).
52
+ email:
53
+ - matias@kiskolabs.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - .gitignore
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/radix62.rb
68
+ - lib/radix62/core_ext/integer.rb
69
+ - lib/radix62/core_ext/string.rb
70
+ - lib/radix62/version.rb
71
+ - radix62.gemspec
72
+ - spec/radix62_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/support/decode62_examples
75
+ - spec/support/encode62_examples
76
+ - spec/support/known_good.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/k33l0r/radix62
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 1
101
+ - 3
102
+ - 6
103
+ version: 1.3.6
104
+ requirements: []
105
+
106
+ rubyforge_project: radix62
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Convert integers to base 62 strings and back.
111
+ test_files: []
112
+