commit e36cefc47233b8df1300b0e4d44a5838b733b5ee Author: Andrew Tomaka Date: Mon Sep 24 02:25:19 2012 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c111b33 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.gem diff --git a/README.md b/README.md new file mode 100644 index 0000000..875110d --- /dev/null +++ b/README.md @@ -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. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..debc11c --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs << 'test' +end + +desc "Run tests" +task :default => :test diff --git a/base52.gemspec b/base52.gemspec new file mode 100644 index 0000000..5cadf99 --- /dev/null +++ b/base52.gemspec @@ -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 diff --git a/lib/base52.rb b/lib/base52.rb new file mode 100644 index 0000000..912f500 --- /dev/null +++ b/lib/base52.rb @@ -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 diff --git a/test/test_base52.rb b/test/test_base52.rb new file mode 100644 index 0000000..525a45e --- /dev/null +++ b/test/test_base52.rb @@ -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