num_coder 1.0.1 → 1.1.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.
Files changed (2) hide show
  1. data/lib/num_coder.rb +61 -14
  2. metadata +1 -1
@@ -1,37 +1,68 @@
1
1
  class NumCoder
2
2
  class << self
3
3
 
4
- def encode(cs, i)
5
- s = ""
6
- t = i
7
- while t > 0
8
- s = cs[t % cs.size] + s
9
- t = t / cs.size
4
+ def fixed_encode(cs, i, l)
5
+ if i.is_a? Array
6
+ r = ""
7
+ i.each { |e| r << fixed_encode(cs, e, l) }
8
+ return r
9
+ else
10
+ raise "Won't fit" unless i < cs.size ** l
11
+ s = cs[0] * l
12
+ l.times do |p|
13
+ s[-1-p] = cs[i / cs.size ** p % cs.size]
14
+ end
15
+ return s
16
+ end
17
+ end
18
+
19
+ def fixed_decode(cs, s, l)
20
+ raise "Can't split" unless s.size % l == 0
21
+ r = []
22
+ (s.size / l).times do |p|
23
+ i = 0
24
+ s[p*l..p*l+l-1].each_char { |c| i = cs.size * i + cs.index(c) }
25
+ r << i
26
+ end
27
+
28
+ if r.size == 1
29
+ return r[0]
30
+ else
31
+ return r
10
32
  end
11
- return s
33
+ end
34
+
35
+ def encode(cs, i)
36
+ fixed_encode(cs, i, Math.log(i, cs.size).ceil)
12
37
  end
13
38
 
14
39
  def decode(cs, s)
15
- i = 0
16
- s.each_char { |c| i = cs.size * i + cs.index(c) }
17
- return i
40
+ fixed_decode(cs, s, s.size)
18
41
  end
19
42
 
20
43
  def cs95
21
- cs = ""
22
- 32.upto(126) { |c| cs << c }
23
- return cs
24
- # " !\"#\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
44
+ # cs = ""
45
+ # 32.upto(126) { |c| cs << c }
46
+ # return cs
47
+ " !\"#\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
25
48
  end
26
49
 
27
50
  def encode95(i)
28
51
  encode(cs95, i)
29
52
  end
30
53
 
54
+ def fixed_encode95(i, l)
55
+ fixed_encode(cs95, i, l)
56
+ end
57
+
31
58
  def decode95(s)
32
59
  decode(cs95, s)
33
60
  end
34
61
 
62
+ def fixed_decode95(s, l)
63
+ fixed_decode(cs95, s, l)
64
+ end
65
+
35
66
  def cs64
36
67
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
37
68
  end
@@ -40,10 +71,18 @@ class NumCoder
40
71
  encode(cs64, i)
41
72
  end
42
73
 
74
+ def fixed_encode64(i, l)
75
+ fixed_encode(cs64, i, l)
76
+ end
77
+
43
78
  def decode64(s)
44
79
  decode(cs64, s)
45
80
  end
46
81
 
82
+ def fixed_decode64(s, l)
83
+ fixed_decode(cs64, s, l)
84
+ end
85
+
47
86
  def cs16
48
87
  "0123456789abcdef"
49
88
  end
@@ -52,9 +91,17 @@ class NumCoder
52
91
  encode(cs16, i)
53
92
  end
54
93
 
94
+ def fixed_encode16(i, l)
95
+ fixed_encode(cs16, i, l)
96
+ end
97
+
55
98
  def decode16(s)
56
99
  decode(cs16, s)
57
100
  end
58
101
 
102
+ def fixed_decode16(s, l)
103
+ fixed_decode(cs16, s, l)
104
+ end
105
+
59
106
  end
60
107
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num_coder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: