Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Yet Another Base 62 Convertor. This one is written with c extension.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 ext
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.mkdn
Octocat-spinner-32 Rakefile
Octocat-spinner-32 yab62.gemspec
README.mkdn

YAB62 (Yet Another Base 62 Convertor)

The idea of this gem is simple: I want to write a url shortener with base62. Ruby doesn't support it natively. I checked out other gems: radix62, base62 and alphadecimal.

They are all written in Ruby. Then, I have this idea to write this gem in C extension. Hence, YAB62 is born.

Btw, most of the tests are borrowed from alphadecimal.

How to use

Install it with gem install yab62.

require 'yab62'

1.encode62 # => "1"
"1".decode62 # => 1

# Or, you can do this:
YAB62.encode62(1) # => "1"
YAB62.decode62("1") # => 1

Benchmark

Check out the benchmark here:

# gem alphadecimal
time = Benchmark.measure do
  1_000_000.times do |i|
    encode = i.alphadecimal
    decode = encode.alphadecimal
    raise "Assertion error!" unless i == decode
  end
end

puts time # 17.060000   0.050000  17.110000 ( 17.507562)

# gem base62
time = Benchmark.measure do
  1_000_000.times do |i|
    encode = i.base62_encode
    decode = encode.base62_decode
    raise "Assertion error!" unless i == decode
  end
end

puts time # 9.600000   0.020000   9.620000 (  9.802189)

# gem radix62
time = Benchmark.measure do
  1_000_000.times do |i|
    encode = i.encode62
    decode = encode.decode62
    raise "Assertion error!" unless i == decode
  end
end

puts time # 19.070000   0.040000  19.110000 ( 19.483596)

# gem yab62
time = Benchmark.measure do
  1_000_000.times do |i|
    encode = i.encode62
    decode = encode.decode62
    raise "Assertion error!" unless i == decode
  end
end

puts time # 0.550000   0.000000   0.550000 (  0.605562)

Check out the full script here.

LICENSE

(The MIT License)

Copyright (c) 2011 Teng Siong Ong

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.

Something went wrong with that request. Please try again.