Skip to content

Commit fc72a8b

Browse files
committed
utils: vlq
1 parent 83fa97a commit fc72a8b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/utils/vlq.cr

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,43 @@
1919

2020
module MoonScript
2121
module VLQ
22-
VLQ_BASE_SHIFT = 5
23-
VLQ_BASE = 1 << VLQ_BASE_SHIFT
24-
VLQ_BASE_MASK = VLQ_BASE - 1
22+
VLQ_BASE_SHIFT = 5
23+
24+
VLQ_BASE = 1 << VLQ_BASE_SHIFT
25+
26+
VLQ_BASE_MASK = VLQ_BASE - 1
27+
2528
VLQ_CONTINUATION_BIT = VLQ_BASE
2629

30+
BASE64_DIGITS =
31+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".chars
32+
2733
def self.encode(int)
2834
vlq = to_vlq_signed(int)
2935
encoded = ""
30-
cond = True
36+
cond = true
3137

3238
while cond
3339
digit = vlq & VLQ_BASE_MASK
3440
vlq >>= VLQ_BASE_SHIFT
3541
digit |= VLQ_CONTINUATION_BIT if vlq > 0
42+
encoded += base64_encode(digit)
3643
cond = vlq > 0
3744
end
45+
46+
encoded
3847
end
3948

4049
private def self.base64_encode(int)
41-
BASE64_DIGITS[int]? || raise ArgumentError.new "#{int} is not valid base64 digit"
50+
BASE64_DIGITS[int]? || raise ArgumentError.new "#{int} is not a valid base64 digit"
4251
end
4352

4453
private def self.to_vlq_signed(int)
45-
if int < 0
46-
((-int) << 1) + 1
47-
else
48-
int << 1
54+
if int < 0
55+
((-int) << 1) + 1
56+
else
57+
int << 1
58+
end
4959
end
5060
end
5161
end

0 commit comments

Comments
 (0)