radix62 1.0.0 → 1.0.1

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.
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ ## v1.0.1
4
+
5
+ * Performance improvements (courtesy of [sfcgeorge](https://github.com/sfcgeorge))
6
+ * No functional changes
7
+
8
+ ## v1.0.0
9
+
10
+ * Testing and gem structure improvements
11
+ * No functional changes
12
+
13
+ ## v0.1.0
14
+
15
+ * Initial release
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Matias Korhonen
1
+ Copyright © 2010-2013 Matias Korhonen & contributors
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -30,10 +30,10 @@ Radix62 also adds the `decode62` and `encode62` convenience methods to the Strin
30
30
 
31
31
  ## Tests and compatibility
32
32
 
33
- The tests pass on 1.8.7, 1.9.2, Rubinius 1.0.1, and JRuby 1.5.2/1.6.0.
33
+ [![Build Status](https://travis-ci.org/k33l0r/radix62.png)](https://travis-ci.org/k33l0r/radix62) [![Code Climate](https://codeclimate.com/github/k33l0r/radix62.png)](https://codeclimate.com/github/k33l0r/radix62)
34
34
 
35
- ## License and copyright
35
+ The gem is tested against MRI 1.8/1.9/2.0, JRuby, and Rubinius. See [Travis CI](https://travis-ci.org/k33l0r/radix62) for details.
36
36
 
37
- Copyright © 2010-2012 Matias Korhonen
37
+ ## License
38
38
 
39
39
  Licensed under the MIT license, see the LICENSE file for details.
@@ -5,6 +5,15 @@ require "radix62/core_ext/integer"
5
5
  # Convert integers to base 62 strings and back.
6
6
  module Radix62
7
7
  ALPHABET = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
8
+ ALPHABET_HASH = {}
9
+ ALPHABET.each_with_index { |v,k| ALPHABET_HASH[v] = k }
10
+
11
+ KeyError = begin
12
+ KeyError
13
+ rescue NameError
14
+ IndexError
15
+ end
16
+
8
17
 
9
18
  # Encode an Integer to a base 62 string. The input value *must* be a positive
10
19
  # integer.
@@ -15,36 +24,34 @@ module Radix62
15
24
  unless number.is_a? Integer
16
25
  raise TypeError.new "number not an integer"
17
26
  end
18
-
19
- if number < 0
20
- raise RangeError.new "number must be greater than or equal to 0"
27
+
28
+ if number > 0
29
+ base62 = ""
30
+ while number > 0
31
+ base62 << ALPHABET[number % 62]
32
+ number /= 62
33
+ end
34
+ base62.reverse
21
35
  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
36
+ "0"
37
+ else
38
+ raise RangeError.new "number must be greater than or equal to 0"
30
39
  end
31
-
32
- base62.reverse
33
40
  end
41
+
34
42
 
35
43
  # Decode a base 62 String to a base 10 Integer. The input value
36
44
  # <b>must not</b> contain illegal characters.
37
45
  #
38
46
  # If the input is not alphanumeric, an ArgumentError will be raised.
39
47
  def self.decode62(string)
40
- if !!string.match(/^([a-z0-9]+)$/i)
48
+ begin
41
49
  integer = 0
42
- string.split(//).reverse.each_with_index do |char,index|
43
- place = ALPHABET.size ** index
44
- integer += ALPHABET.find_index(char) * place
50
+ string.split(//).each do |char|
51
+ integer = integer * 62 + ALPHABET_HASH.fetch(char)
45
52
  end
46
53
  integer
47
- else
54
+ rescue KeyError
48
55
  raise ArgumentError.new "Input is not alphanumeric."
49
56
  end
50
57
  end
@@ -1,3 +1,3 @@
1
1
  module Radix62
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -18,6 +18,21 @@ describe Radix62 do
18
18
  end
19
19
  end
20
20
 
21
+ context "alphabet_hash" do
22
+ it "has the ALPHABET_HASH hash" do
23
+ Radix62::ALPHABET_HASH.should be_kind_of Hash
24
+ end
25
+
26
+ it "has 62 items" do
27
+ Radix62::ALPHABET_HASH.should_not be_empty
28
+ Radix62::ALPHABET_HASH.count.should == 62
29
+ end
30
+
31
+ it "has the right contents" do
32
+ Radix62::ALPHABET_HASH.should == {"0"=>0, "1"=>1, "2"=>2, "3"=>3, "4"=>4, "5"=>5, "6"=>6, "7"=>7, "8"=>8, "9"=>9, "a"=>10, "b"=>11, "c"=>12, "d"=>13, "e"=>14, "f"=>15, "g"=>16, "h"=>17, "i"=>18, "j"=>19, "k"=>20, "l"=>21, "m"=>22, "n"=>23, "o"=>24, "p"=>25, "q"=>26, "r"=>27, "s"=>28, "t"=>29, "u"=>30, "v"=>31, "w"=>32, "x"=>33, "y"=>34, "z"=>35, "A"=>36, "B"=>37, "C"=>38, "D"=>39, "E"=>40, "F"=>41, "G"=>42, "H"=>43, "I"=>44, "J"=>45, "K"=>46, "L"=>47, "M"=>48, "N"=>49, "O"=>50, "P"=>51, "Q"=>52, "R"=>53, "S"=>54, "T"=>55, "U"=>56, "V"=>57, "W"=>58, "X"=>59, "Y"=>60, "Z"=>61}
33
+ end
34
+ end
35
+
21
36
  context "encode62" do
22
37
  it "raises an exception with negative numbers" do
23
38
  (-1000..-1).step(100).each do |n|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radix62
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,14 +9,14 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-09 00:00:00.000000000 Z
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - '>='
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
22
  type: :development
@@ -24,7 +24,7 @@ dependencies:
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - '>='
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - '>='
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
38
  type: :development
@@ -40,7 +40,7 @@ dependencies:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - '>='
43
+ - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
@@ -69,6 +69,7 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
71
  - .travis.yml
72
+ - CHANGELOG.md
72
73
  - Gemfile
73
74
  - LICENSE
74
75
  - README.md
@@ -92,16 +93,13 @@ require_paths:
92
93
  required_ruby_version: !ruby/object:Gem::Requirement
93
94
  none: false
94
95
  requirements:
95
- - - '>='
96
+ - - ! '>='
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
- segments:
99
- - 0
100
- hash: -3220946439689542333
101
99
  required_rubygems_version: !ruby/object:Gem::Requirement
102
100
  none: false
103
101
  requirements:
104
- - - '>='
102
+ - - ! '>='
105
103
  - !ruby/object:Gem::Version
106
104
  version: 1.3.6
107
105
  requirements: []