omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / empty-string / empty-string.om
| 1 | note Rosetta Code task "Empty string": https://rosettacode.org/wiki/Empty_string |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text tempty "s is empty\n" |
| 6 | text tfull "s is not empty: " |
| 7 | text word "abc" |
| 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 copyto copies s into d at offset off and returns the offset after it (past the end traps 3) |
| 20 | fn copyto d edit uchar * off ulong s view uchar * to ulong |
| 21 | for i ulong len s |
| 22 | put d add off i get s i |
| 23 | end |
| 24 | ret add off len s |
| 25 | end |
| 26 | note a string here is a buffer b and its length n; the empty string is length 0 |
| 27 | fn check b view uchar * to void |
| 28 | if eq len b 0 |
| 29 | emit tempty |
| 30 | else |
| 31 | emit tfull |
| 32 | emit b |
| 33 | emit nl |
| 34 | end |
| 35 | end |
| 36 | fn main to int |
| 37 | b = new uchar 16 0 |
| 38 | n = ulong 0 |
| 39 | check part b 0 n |
| 40 | n = copyto b 0 word |
| 41 | check part b 0 n |
| 42 | n = 0 |
| 43 | check part b 0 n |
| 44 | ret 0 |
| 45 | end |