b62 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/ext/b62/b62.c +46 -0
  2. data/ext/b62/extconf.rb +3 -0
  3. data/lib/b62.rb +9 -0
  4. metadata +52 -0
@@ -0,0 +1,46 @@
1
+ /*
2
+ * b62.c
3
+ *
4
+ * Original function by Jason Hullinger (2010)
5
+ * Original ruby extension by Teng Siong Ong (2011)
6
+ * Refactored by Adrià Planas (2013)
7
+ *
8
+ */
9
+
10
+ #include <stdlib.h>
11
+ #include <ruby.h>
12
+
13
+ static const char b62_table[] = { "0123456789"
14
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
15
+ "abcdefghijklmnopqrstuvwxyz" };
16
+
17
+ int encode(u_int64_t input, char* ptr)
18
+ {
19
+ short i = 11;
20
+ u_int64_t r = 0;
21
+
22
+ ptr[i] = '\0';
23
+
24
+ do
25
+ {
26
+ i--;
27
+ r = input % 62;
28
+ input = (input - r) / 62;
29
+ ptr[i] = b62_table[r];
30
+ }while(input > 0);
31
+
32
+ return i;
33
+ }
34
+
35
+ static VALUE encode62(VALUE self, VALUE arg)
36
+ {
37
+ char output[12];
38
+
39
+ return rb_str_new2(&output[encode(NUM2ULL(arg), output)]);
40
+ }
41
+
42
+ void Init_b62(void)
43
+ {
44
+ VALUE klass = rb_define_class("B62", rb_cObject);
45
+ rb_define_singleton_method(klass, "to_b62", encode62, 1);
46
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('b62/b62')
@@ -0,0 +1,9 @@
1
+ require "b62/b62"
2
+
3
+ class Integer
4
+ def to_b62
5
+ raise RangeError, "#{self.class} must be positive to be encoded" if self < 0
6
+
7
+ B62.to_b62(self)
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: b62
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adrià Planas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! " Adds a to_b62 function to the Integer class that encodes \n any
15
+ integer between 0 and 18446744073709551615 to base 62.\n The encode function is
16
+ implemented as a C extension so it's\n much faster than a plain ruby implementation.\n"
17
+ email:
18
+ - adriaplanas@liquidcodeworks.com
19
+ executables: []
20
+ extensions:
21
+ - ext/b62/extconf.rb
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/b62.rb
25
+ - ext/b62/b62.c
26
+ - ext/b62/extconf.rb
27
+ homepage: https://github.com/planas/b62
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Dead simple lightning fast base 62 encoder.
51
+ test_files: []
52
+ has_rdoc: