Skip to content

Commit 74e677e

Browse files
committed
utils: vlq
1 parent 2028962 commit 74e677e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/utils/vlq.cr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -----------------------------------------------------------------------
2+
# This file is part of MoonScript
3+
#
4+
# MoonSript is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MoonSript is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MoonSript. If not, see <https://www.gnu.org/licenses/>.
16+
#
17+
# Copyright (C) 2025 Krisna Pranav, MoonScript Developers
18+
# -----------------------------------------------------------------------
19+
20+
module MoonScript
21+
module VLQ
22+
VLQ_BASE_SHIFT = 5
23+
VLQ_BASE = 1 << VLQ_BASE_SHIFT
24+
VLQ_BASE_MASK = VLQ_BASE - 1
25+
VLQ_CONTINUATION_BIT = VLQ_BASE
26+
27+
def self.encode(int)
28+
vlq = to_vlq_signed(int)
29+
encoded = ""
30+
cond = True
31+
32+
while cond
33+
digit = vlq & VLQ_BASE_MASK
34+
vlq >>= VLQ_BASE_SHIFT
35+
digit |= VLQ_CONTINUATION_BIT if vlq > 0
36+
cond = vlq > 0
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)