omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / copy-a-string / copy-a-string.om
| 1 | note Rosetta Code task "Copy a string": https://rosettacode.org/wiki/Copy_a_string |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text src "Hello" |
| 6 | text tsrc "source: " |
| 7 | text tcp "copy: " |
| 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 | fn main to int |
| 27 | note a text is read-only; copying it into a local array gives an independent, writable string |
| 28 | c = new uchar 16 0 |
| 29 | n = copyto c 0 src |
| 30 | put c 0 74 |
| 31 | emit tsrc |
| 32 | emit src |
| 33 | emit nl |
| 34 | emit tcp |
| 35 | emit part c 0 n |
| 36 | emit nl |
| 37 | ret 0 |
| 38 | end |