omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / string-prepend / string-prepend.om
| 1 | note Rosetta Code task "String prepend": https://rosettacode.org/wiki/String_prepend |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text s0 "World" |
| 6 | text pre "Hello, " |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) |
| 19 | fn copyto d edit uchar * off ulong s view uchar * to ulong |
| 20 | for i ulong len s |
| 21 | put d add off i get s i |
| 22 | end |
| 23 | ret add off len s |
| 24 | end |
| 25 | note prepend moves the n bytes of b right by len p (last byte first), then copies p in front |
| 26 | fn prepend b edit uchar * n ulong p view uchar * to ulong |
| 27 | k = len p |
| 28 | for i ulong n |
| 29 | j = sub sub n 1 i |
| 30 | put b add j k get b j |
| 31 | end |
| 32 | r = copyto b 0 p |
| 33 | ret add n k |
| 34 | end |
| 35 | fn main to int |
| 36 | b = new uchar 64 0 |
| 37 | n = copyto b 0 s0 |
| 38 | n = prepend b n pre |
| 39 | emit part b 0 n |
| 40 | emit nl |
| 41 | ret 0 |
| 42 | end |