public
Description: Base62 encoding and decoding to turn numbers into very compact strings
Homepage: http://github.com/JackDanger/Alphadecimal
Clone URL: git://github.com/JackDanger/alphadecimal.git
Search Repo:
Initial gem creation.  Modular structure by Jack Danger, algorithm by Mike 
Mondragon.
Jack Danger Canty (author)
Tue Mar 11 13:08:17 -0700 2008
commit  5ccb31b2bd5655ad9a353ec1269f85f10b04653d
tree    94dedbbde7f3fa57c399504f09ee1eee8bbc563e
...
 
 
 
 
 
 
...
1
2
3
4
5
6
0
@@ -0,0 +1,6 @@
0
+=== 1.0.0 / 2008-03-11
0
+
0
+* 1 major enhancement
0
+
0
+ * Birthday!
0
+
...
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
0
@@ -0,0 +1,7 @@
0
+History.txt
0
+Manifest.txt
0
+README.txt
0
+Rakefile
0
+bin/alphadecimal
0
+lib/alphadecimal.rb
0
+test/test_alphadecimal.rb
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
0
@@ -0,0 +1,45 @@
0
+= alphadecimal
0
+
0
+* http://github.com/JackDanger/alphadecimal
0
+
0
+== DESCRIPTION:
0
+
0
+Convert integers to base62 strings (A-Za-z0-9) and back. A handy
0
+way to shorten long numbers.
0
+
0
+== SYNOPSIS:
0
+
0
+ # 45.to_alphadecimal
0
+ # => 'j'
0
+ # 5234233.alphadecimal
0
+ # 'Lxf7'
0
+ #
0
+
0
+== INSTALL:
0
+
0
+* sudo gem install alphadecimal
0
+
0
+== LICENSE:
0
+
0
+(The MIT License)
0
+
0
+Copyright (c) 2008 FIX
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+'Software'), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -0,0 +1,13 @@
0
+# -*- ruby -*-
0
+
0
+require 'rubygems'
0
+require 'hoe'
0
+require './lib/alphadecimal.rb'
0
+
0
+Hoe.new('alphadecimal', Alphadecimal::VERSION) do |p|
0
+ # p.rubyforge_name = 'alphadecimal' # if different than lowercase project name
0
+ p.developer('Mike Mondragon', 'mikemondragon@gmail.com')
0
+ p.developer('Jack Danger Canty', 'jdanger@adpickles.com')
0
+end
0
+
0
+# vim: syntax=Ruby
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
0
@@ -0,0 +1,64 @@
0
+module Alphadecimal
0
+ VERSION = '1.0.0'
0
+
0
+ B62_0, B62_9 = '0'[0], '9'[0]
0
+ B62_A, B62_Z = 'A'[0], 'Z'[0]
0
+ B62_a, B62_z = 'a'[0], 'z'[0]
0
+ B62_CHRS = [(B62_0..B62_9).map, (B62_A..B62_Z).map, (B62_a..B62_z).map].flatten
0
+
0
+ module Number
0
+ def alphadecimal
0
+ return self if nil? || self < 0
0
+ return to_s if self == 0
0
+ string = ""
0
+ value = self
0
+ until value == 0
0
+ case mod = value % 62
0
+ when 0..9 then string << (mod + B62_0)
0
+ when 10..35 then string << (mod - 10 + B62_A)
0
+ when 36...62 then string << (mod - 36 + B62_a)
0
+ end
0
+ value = value / 62
0
+ end
0
+ string.reverse
0
+ end
0
+ end
0
+
0
+ module String
0
+ def alphadecimal
0
+ return self unless is_alphadecimal?
0
+ val = 0
0
+ key = reverse
0
+ (0...key.size).each do |i|
0
+ char = key[i]
0
+ case
0
+ when (B62_0..B62_9).include?(char) then norm = char - B62_0
0
+ when (B62_A..B62_Z).include?(char) then norm = char - B62_A + 10
0
+ when (B62_a..B62_z).include?(char) then norm = char - B62_a + 36
0
+ end
0
+ val = val + (norm * (62 ** i))
0
+ end
0
+ val
0
+ end
0
+
0
+ def is_alphadecimal?
0
+ return false if nil?
0
+ string = self.dup
0
+ return false if string.length <= 0
0
+ (0...string.size).each do |i|
0
+ return false unless B62_CHRS.include?(string[i])
0
+ end
0
+ true
0
+ end
0
+ end
0
+end
0
+
0
+class String
0
+ include Alphadecimal::String
0
+end
0
+class Fixnum
0
+ include Alphadecimal::Number
0
+end
0
+class Bignum
0
+ include Alphadecimal::Number
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
0
@@ -0,0 +1,69 @@
0
+require 'test/unit'
0
+require File.dirname(__FILE__) + "/../lib/alphadecimal"
0
+
0
+class TestAlphadecimal < Test::Unit::TestCase
0
+
0
+ include Alphadecimal
0
+
0
+ def test_fixnum_becomes_string
0
+ assert_kind_of String, 45.alphadecimal
0
+ end
0
+
0
+ def test_bignum_becomes_string
0
+ assert_kind_of String, 231087934571049032.alphadecimal
0
+ end
0
+
0
+ def test_string_becomes_bignum
0
+ assert_kind_of Bignum, 'aHealk43JZy'.alphadecimal
0
+ end
0
+
0
+ def test_string_becomes_fixnum
0
+ assert_kind_of Fixnum, 'j'.alphadecimal
0
+ end
0
+
0
+ def test_invalid_string_returns_self
0
+ string = 'Whoah there!'
0
+ assert_equal string, string.alphadecimal
0
+ end
0
+
0
+ def test_identities
0
+ [123, 90000008, 733, '12331', 'abYzj', '0', 'A', 'z',
0
+ 79823898923232, 'ZZZZZZZZZZZZZZZZZZZ', 'Yz82J3Ng5Nj9M1',
0
+ '!!!', '@$%!^', 'abc ', 'HP', 4, 'ǟ'].each do |object|
0
+ assert_equal object, object.alphadecimal.alphadecimal
0
+ end
0
+ end
0
+
0
+ def test_alphabet_characters_should_only_validate
0
+ alphabet = (('0'[0]..'9'[0]).collect <<
0
+ ('A'[0]..'Z'[0]).collect <<
0
+ ('a'[0]..'z'[0]).collect).flatten
0
+ (0..255).each do |i|
0
+ case
0
+ when alphabet.include?(i)
0
+ assert i.chr.is_alphadecimal?, "char #{i} is not valid as string #{i.chr}"
0
+ else
0
+ assert !i.chr.is_alphadecimal?, "char #{i} is valid as string #{i.chr}"
0
+ end
0
+ end
0
+ end
0
+
0
+ def test_edge_62_to_the_0_convertions_should_be_valid
0
+ (0...62).each do |i|
0
+ encode = i.alphadecimal
0
+ decode = encode.alphadecimal
0
+ assert_equal i, decode, "integer #{i} was encoded as #{encode} and was decoded to #{decode}"
0
+ end
0
+ end
0
+
0
+ def test_edge_62_to_the_n_convertions_should_be_valid
0
+ (0...3).each do |p|
0
+ n = 62 ** p
0
+ (0...62).each do |i|
0
+ encode = (i+n).alphadecimal
0
+ decode = encode.alphadecimal
0
+ assert_equal i+n, decode, "interger #{i+n} was encoded as #{encode} and was decoded to #{decode}"
0
+ end
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.