omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / strip-comments-from-a-string / strip-comments-from-a-string.om
| 1 | note Rosetta Code task "Strip comments from a string": https://rosettacode.org/wiki/Strip_comments_from_a_string |
| 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 s1 "apples, pears # and bananas" |
| 8 | text s2 "apples, pears ; and bananas" |
| 9 | text s3 " no comment here " |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note cut at the first '#' or ';', then drop whitespace (space, tab) at both ends of what is left |
| 22 | fn strip a view uchar * to void |
| 23 | e = len a |
| 24 | for i ulong len a |
| 25 | c = get a i |
| 26 | if and eq e len a or eq c 35 eq c 59 |
| 27 | e = i |
| 28 | end |
| 29 | end |
| 30 | s = ulong 0 |
| 31 | while lt s e bound 4096 |
| 32 | c = get a s |
| 33 | if and ne c 32 ne c 9 |
| 34 | break |
| 35 | end |
| 36 | s = add s 1 |
| 37 | end |
| 38 | while gt e s bound 4096 |
| 39 | c = get a sub e 1 |
| 40 | if and ne c 32 ne c 9 |
| 41 | break |
| 42 | end |
| 43 | e = sub e 1 |
| 44 | end |
| 45 | emit lq |
| 46 | emit part a s sub e s |
| 47 | emit rq |
| 48 | emit nl |
| 49 | end |
| 50 | fn main to int |
| 51 | strip s1 |
| 52 | strip s2 |
| 53 | strip s3 |
| 54 | ret 0 |
| 55 | end |