rebase 1.0.0
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.
- checksums.yaml +7 -0
- data/lib/rebase/ext/integer.rb +5 -0
- data/lib/rebase/ext/string.rb +5 -0
- data/lib/rebase/ext.rb +3 -0
- data/lib/rebase.rb +74 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2e7840d33f7ac0a5f5d8239eab3a41d819812ee8
|
4
|
+
data.tar.gz: 331193f87697682d788e41d903f9f95d022693d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 639c131400b69b240130c78db6f4d36b14187375aaed8d92ff1c144ff1b4e3ecce19c4ef1c1ebfbcc40c9086820fa503a8bf3aa675fae5c5c8ff964cd87a0d22
|
7
|
+
data.tar.gz: ec333a6d2b33fc5bb49f00282de4ff6218b3c482e73048960926c4767b930f5f8181da6f1c8ca7bab13dac72b09b4a77e79433d6013e8b006beb28772c02e069
|
data/lib/rebase/ext.rb
ADDED
data/lib/rebase.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
class Rebase
|
2
|
+
attr_reader :alphabet, :base
|
3
|
+
|
4
|
+
def initialize(alphabet)
|
5
|
+
@alphabet =
|
6
|
+
case alphabet
|
7
|
+
when :binary then get_base(2)
|
8
|
+
when :hex then get_base(16)
|
9
|
+
when Fixnum then get_base(alphabet)
|
10
|
+
when Array then build_alphabet(alphabet)
|
11
|
+
else raise ArgumentError, 'Invalid input'
|
12
|
+
end
|
13
|
+
@base = @alphabet.size
|
14
|
+
end
|
15
|
+
|
16
|
+
def encode(int)
|
17
|
+
raise ArgumentError, 'Integer cant be negative' if int < 0
|
18
|
+
return alphabet[int] if int.zero?
|
19
|
+
encode_rec('', int)
|
20
|
+
end
|
21
|
+
|
22
|
+
def decode(str)
|
23
|
+
raise ArgumentError, 'String cant be empty' if str.empty?
|
24
|
+
decode_rec(0, 0, str)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def encode_rec(acc, int)
|
30
|
+
return acc if int.zero?
|
31
|
+
encode_rec(alphabet[int % base] + acc,
|
32
|
+
int / base)
|
33
|
+
end
|
34
|
+
|
35
|
+
def decode_rec(acc, pow, str)
|
36
|
+
return acc if str.empty?
|
37
|
+
decode_rec(base ** pow * alphabet_index[str[-1]] + acc,
|
38
|
+
pow.next,
|
39
|
+
str[0...-1])
|
40
|
+
end
|
41
|
+
|
42
|
+
def alphabet_index
|
43
|
+
@alphabet_index ||= begin
|
44
|
+
Hash[alphabet.each_with_index.to_a].tap do |h|
|
45
|
+
h.default_proc = proc do |_,k|
|
46
|
+
raise ArgumentError, "Invalid character in input: #{k}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_alphabet(array)
|
53
|
+
raise ArgumentError, "Alphabet to short" if array.size < 2
|
54
|
+
array.map(&:to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_base(int)
|
58
|
+
raise ArgumentError, "Invalid base: #{int}" if int < 2 || int > 62
|
59
|
+
self.class.const_get("B#{int}")
|
60
|
+
end
|
61
|
+
|
62
|
+
B62 = %w[
|
63
|
+
0 1 2 3 4 5 6 7 8 9
|
64
|
+
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
65
|
+
a b c d e f g h i j k l m n o p q r s t u v w x y z
|
66
|
+
]
|
67
|
+
|
68
|
+
(2...62).map { |i| const_set("B#{i}", B62[0...i]) }
|
69
|
+
end
|
70
|
+
|
71
|
+
def Rebase(alphabet, int=nil)
|
72
|
+
base = Rebase.new(alphabet)
|
73
|
+
int ? base.encode(int) : base
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rebase
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Larry Fox
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Encode and decode from base 10 to string in the given base. Accepts bases
|
14
|
+
2—62, or an array of an arbitrary alphabet.
|
15
|
+
email:
|
16
|
+
- l@rryfox.us
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/rebase.rb
|
22
|
+
- lib/rebase/ext.rb
|
23
|
+
- lib/rebase/ext/integer.rb
|
24
|
+
- lib/rebase/ext/string.rb
|
25
|
+
homepage: https://github.com/larryfox/rebase-ruby
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.4.1
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Convert integers to a given base.
|
49
|
+
test_files: []
|