omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / determine-if-a-string-is-squeezable / determine-if-a-string-is-squeezable.om
| 1 | note Rosetta Code task "Determine if a string is squeezable": https://rosettacode.org/wiki/Determine_if_a_string_is_squeezable |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text lq "<<<" |
| 6 | text rq ">>>" |
| 7 | text tlen " length " |
| 8 | text tch "squeeze '" |
| 9 | text tq "'\n" |
| 10 | text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " |
| 11 | text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888" |
| 12 | text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. " |
| 13 | text c3 " --- Harry S Truman " |
| 14 | text chars "-7. -r" |
| 15 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 16 | fn emit a view uchar * to void |
| 17 | o = ulong 0 |
| 18 | while lt o len a bound 4096 |
| 19 | k = write 1 a o sub len a o |
| 20 | if le k 0 |
| 21 | exit 1 |
| 22 | end |
| 23 | o = add o ulong k |
| 24 | end |
| 25 | end |
| 26 | note emitu writes n in decimal; 20 digits hold every ulong |
| 27 | fn emitu n ulong to void |
| 28 | d = new uchar 20 0 |
| 29 | i = ulong 20 |
| 30 | v = n |
| 31 | while true bound 20 |
| 32 | i = sub i 1 |
| 33 | put d i uchar add rem v 10 48 |
| 34 | v = div v 10 |
| 35 | if eq v 0 |
| 36 | break |
| 37 | end |
| 38 | end |
| 39 | emit part d i sub 20 i |
| 40 | end |
| 41 | note emitc writes one byte |
| 42 | fn emitc c uchar to void |
| 43 | b = new uchar 1 0 |
| 44 | put b 0 c |
| 45 | emit b |
| 46 | end |
| 47 | fn line a view uchar * to void |
| 48 | emit lq |
| 49 | emit a |
| 50 | emit rq |
| 51 | emit tlen |
| 52 | emitu len a |
| 53 | emit nl |
| 54 | end |
| 55 | note squeeze drops a byte equal to ch when the byte before it is also ch |
| 56 | fn squeeze a view uchar * ch uchar to void |
| 57 | o = new uchar 256 0 |
| 58 | n = ulong 0 |
| 59 | for i ulong len a |
| 60 | c = get a i |
| 61 | k = true |
| 62 | if and gt i 0 eq c ch |
| 63 | k = ne get a sub i 1 ch |
| 64 | end |
| 65 | if k |
| 66 | put o n c |
| 67 | n = add n 1 |
| 68 | end |
| 69 | end |
| 70 | emit tch |
| 71 | emitc ch |
| 72 | emit tq |
| 73 | line a |
| 74 | line part o 0 n |
| 75 | end |
| 76 | fn main to int |
| 77 | squeeze c1 get chars 0 |
| 78 | squeeze c1 get chars 1 |
| 79 | squeeze c2 get chars 2 |
| 80 | squeeze c3 get chars 3 |
| 81 | squeeze c3 get chars 4 |
| 82 | squeeze c3 get chars 5 |
| 83 | ret 0 |
| 84 | end |