Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/run-length-encoding/run-length-encoding.om # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / run-length-encoding / run-length-encoding.om` 85 lines ยท 1632 bytes Raw Blame History 1 note Rosetta Code task "Run-length encoding": https://rosettacode.org/wiki/Run-length_encoding 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text msg "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" 6 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 7 fn emit a view uchar * to void 8 o = ulong 0 9 while lt o len a bound 4096 10 k = write 1 a o sub len a o 11 if le k 0 12 exit 1 13 end 14 o = add o ulong k 15 end 16 end 17 note putnum writes n in decimal into r at p and returns the offset after it 18 fn putnum r edit uchar * p ulong n ulong to ulong 19 d = new uchar 20 0 20 i = ulong 20 21 v = n 22 while true bound 20 23 i = sub i 1 24 put d i uchar add rem v 10 48 25 v = div v 10 26 if eq v 0 27 break 28 end 29 end 30 q = p 31 for j ulong sub 20 i 32 put r q get d add i j 33 q = add q 1 34 end 35 ret q 36 end 37 note encode writes count then byte for every run of equal bytes; returns the length 38 fn encode a view uchar * r edit uchar * to ulong 39 p = ulong 0 40 i = ulong 0 41 while lt i len a bound 4096 42 c = get a i 43 j = i 44 while lt j len a bound 4096 45 if ne get a j c 46 break 47 end 48 j = add j 1 49 end 50 p = putnum r p sub j i 51 put r p c 52 p = add p 1 53 i = j 54 end 55 ret p 56 end 57 note decode reads a decimal count then one byte, and repeats the byte; returns the length 58 fn decode a view uchar * r edit uchar * to ulong 59 p = ulong 0 60 v = ulong 0 61 for i ulong len a 62 c = get a i 63 if and ge c 48 le c 57 64 v = add mul v 10 ulong sub c 48 65 else 66 for k ulong v 67 put r p c 68 p = add p 1 69 end 70 v = 0 71 end 72 end 73 ret p 74 end 75 fn main to int 76 e = new uchar 256 0 77 d = new uchar 256 0 78 n = encode msg e 79 emit part e 0 n 80 emit nl 81 m = decode part e 0 n d 82 emit part d 0 m 83 emit nl 84 ret 0 85 end