omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / strip-whitespace-from-a-string / strip-whitespace-from-a-string.om
| 1 | note Rosetta Code task "Strip whitespace from a string/Top and tail": https://rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tail |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text lb "[" |
| 6 | text rb "]" |
| 7 | text msg " \t hello world \t\n " |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note white: space, tab, newline, vertical tab, form feed, carriage return |
| 20 | fn white c uchar to bool |
| 21 | ret or eq c 32 and ge c 9 le c 13 |
| 22 | end |
| 23 | fn lead a view uchar * to ulong |
| 24 | for i ulong len a |
| 25 | if not white get a i |
| 26 | ret i |
| 27 | end |
| 28 | end |
| 29 | ret len a |
| 30 | end |
| 31 | fn trail a view uchar * to ulong |
| 32 | for i ulong len a |
| 33 | if not white get a sub sub len a 1 i |
| 34 | ret sub len a i |
| 35 | end |
| 36 | end |
| 37 | ret 0 |
| 38 | end |
| 39 | fn show a view uchar * to void |
| 40 | emit lb |
| 41 | emit a |
| 42 | emit rb |
| 43 | emit nl |
| 44 | end |
| 45 | fn main to int |
| 46 | l = lead msg |
| 47 | t = trail msg |
| 48 | show msg |
| 49 | show part msg l sub len msg l |
| 50 | show part msg 0 t |
| 51 | show part msg l sub t l |
| 52 | ret 0 |
| 53 | end |