interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / run-length-encoding.wiki

93 lines · 1841 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text msg "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note putnum writes n in decimal into r at p and returns the offset after it
20fn putnum r edit uchar * p ulong n ulong to ulong
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32q = p
33for j ulong sub 20 i
34put r q get d add i j
35q = add q 1
36end
37ret q
38end
39note encode writes count then byte for every run of equal bytes; returns the length
40fn encode a view uchar * r edit uchar * to ulong
41p = ulong 0
42i = ulong 0
43while lt i len a bound 4096
44c = get a i
45j = i
46while lt j len a bound 4096
47if ne get a j c
48break
49end
50j = add j 1
51end
52p = putnum r p sub j i
53put r p c
54p = add p 1
55i = j
56end
57ret p
58end
59note decode reads a decimal count then one byte, and repeats the byte; returns the length
60fn decode a view uchar * r edit uchar * to ulong
61p = ulong 0
62v = ulong 0
63for i ulong len a
64c = get a i
65if and ge c 48 le c 57
66v = add mul v 10 ulong sub c 48
67else
68for k ulong v
69put r p c
70p = add p 1
71end
72v = 0
73end
74end
75ret p
76end
77fn main to int
78e = new uchar 256 0
79d = new uchar 256 0
80n = encode msg e
81emit part e 0 n
82emit nl
83m = decode part e 0 n d
84emit part d 0 m
85emit nl
86ret 0
87end
88</syntaxhighlight>
89{{out}}
90<pre>
9112W1B12W3B24W1B14W
92WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
93</pre>