This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyet.rb
74 lines (68 loc) · 1.7 KB
/
yet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Base62
@@ranges = [
('0'..'9'),
('a'..'z'),
('A'..'Z')
]
@@base = nil
@@offsets = nil
def self.to_s(number)
if @@base.nil?
@@base = self.calculate_base
end
string = ""
while number > (@@base - 1)
place = number % @@base
string = self.lookup(place) + string
number = number / @@base
end
self.lookup(number) + string
end
def self.to_i(string)
if @@base.nil?
@@base = self.calculate_base
end
number = 0
i = string.length - 1
string.each_byte do |c|
c = c.chr
@@ranges.each_index do |j|
range = @@ranges[j]
if range.member? c
number += (c[0] - range.to_a.first[0] + @@offsets[j]) * (@@base ** i)
break
end
end
i -= 1
end
number
end
def self.lookup(place)
string = ""
if @@base.nil?
@@base = self.calculate_base
end
(0..(@@ranges.length-1)).each do |i|
range_array = @@ranges[i].to_a
start = 0 + @@offsets[i]
stop = range_array.length - 1 + @@offsets[i]
if (start..stop).member? place
string = range_array[place - @@offsets[i]]
break
end
end
string
end
def self.next_integer(integer)
integer + 1
end
def self.calculate_base
i = 0
@@offsets = []
@@ranges.each do |range|
@@offsets << i
i += range.to_a.length
end
i
end
end