interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / run-length-encoding / run-length-encoding.om

85 lines · 1632 bytes Raw Blame History
1note Rosetta Code task "Run-length encoding": https://rosettacode.org/wiki/Run-length_encoding
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text msg "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
6note emit writes every byte of a to stdout; a short write is retried, an error exits 1
7fn emit a view uchar * to void
8o = ulong 0
9while lt o len a bound 4096
10k = write 1 a o sub len a o
11if le k 0
12exit 1
13end
14o = add o ulong k
15end
16end
17note putnum writes n in decimal into r at p and returns the offset after it
18fn putnum r edit uchar * p ulong n ulong to ulong
19d = new uchar 20 0
20i = ulong 20
21v = n
22while true bound 20
23i = sub i 1
24put d i uchar add rem v 10 48
25v = div v 10
26if eq v 0
27break
28end
29end
30q = p
31for j ulong sub 20 i
32put r q get d add i j
33q = add q 1
34end
35ret q
36end
37note encode writes count then byte for every run of equal bytes; returns the length
38fn encode a view uchar * r edit uchar * to ulong
39p = ulong 0
40i = ulong 0
41while lt i len a bound 4096
42c = get a i
43j = i
44while lt j len a bound 4096
45if ne get a j c
46break
47end
48j = add j 1
49end
50p = putnum r p sub j i
51put r p c
52p = add p 1
53i = j
54end
55ret p
56end
57note decode reads a decimal count then one byte, and repeats the byte; returns the length
58fn decode a view uchar * r edit uchar * to ulong
59p = ulong 0
60v = ulong 0
61for i ulong len a
62c = get a i
63if and ge c 48 le c 57
64v = add mul v 10 ulong sub c 48
65else
66for k ulong v
67put r p c
68p = add p 1
69end
70v = 0
71end
72end
73ret p
74end
75fn main to int
76e = new uchar 256 0
77d = new uchar 256 0
78n = encode msg e
79emit part e 0 n
80emit nl
81m = decode part e 0 n d
82emit part d 0 m
83emit nl
84ret 0
85end