Initial commit

This commit is contained in:
Andrew Tomaka 2012-09-24 02:25:19 -04:00
commit e36cefc472
6 changed files with 121 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.gem

51
README.md Normal file
View file

@ -0,0 +1,51 @@
#Base52
Encode and Decode Base 52 Values
##Description
Implement functionality to the base Integer class to convert an integer into an equivalent base 52 string using the character set 0-9, A-Z, a-z excluding
vowels. Additionally, implement functionality to the base String class to
convert a base 52 string back to an integer.
##Install
```
$ gem install base52
```
## Usage
```
require 'base52'
1000000000.to_52
=> "2clyTD"
"2clyTD".from_52
=> 1000000000
```
##License
The MIT License
Copyright (c) 2012 Andrew Tomaka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

8
Rakefile Normal file
View file

@ -0,0 +1,8 @@
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
end
desc "Run tests"
task :default => :test

15
base52.gemspec Normal file
View file

@ -0,0 +1,15 @@
Gem::Specification.new do |s|
s.name = 'Base 52'
s.version = '0.0.1'
s.date = '2012-09-24'
s.summary = 'Encode and Decode Base 52 Values'
s.description = 'Implement functionality to the base Integer class to convert
an integer into an equivalent base 52 string using the
character set 0-9, A-Z, a-z excluding vowels. Additionally,
implement functionality to the base String class to convert a
base 52 string back to an integer.'
s.authors = ['Andrew Tomaka']
s.email = 'atomaka@gmail.com'
s.files = ['lib/base52.rb']
s.homepage = 'https://github.com/atomaka/base52'
end

30
lib/base52.rb Normal file
View file

@ -0,0 +1,30 @@
BASE52_CHARACTERS = Array('0'..'9') + Array('A'..'Z') + Array('a'..'z') - 'AEIOUaeiou'.split(//)
class String
def from_52
i = 0
decoded = 0
self.split(//).reverse.each do |a|
decoded += BASE52_CHARACTERS.index(a) * (52 ** i)
i += 1
end
decoded
end
end
class Integer
def to_52
return "0" if self == 0
input = self
encoded = ''
while input > 0
encoded = BASE52_CHARACTERS[input % 52].to_s + encoded
input /= 52
end
encoded
end
end

16
test/test_base52.rb Normal file
View file

@ -0,0 +1,16 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/base52'
class Base52Test < Test::Unit::TestCase
def test_to_52
assert_equal '0', 0.to_52
assert_equal '2clyTD', 1000000000.to_52
assert_equal 'zzzzzz', 19770609663.to_52
end
def test_from_52
assert_equal 0, '0'.from_52
assert_equal 1000000000, '2clyTD'.from_52
assert_equal 19770609663, 'zzzzzz'.from_52
end
end