omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / string-prepend.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text s0 "World" |
| 8 | text pre "Hello, " |
| 9 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 10 | fn emit a view uchar * to void |
| 11 | o = ulong 0 |
| 12 | while lt o len a bound 4096 |
| 13 | k = write 1 a o sub len a o |
| 14 | if le k 0 |
| 15 | exit 1 |
| 16 | end |
| 17 | o = add o ulong k |
| 18 | end |
| 19 | end |
| 20 | note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) |
| 21 | fn copyto d edit uchar * off ulong s view uchar * to ulong |
| 22 | for i ulong len s |
| 23 | put d add off i get s i |
| 24 | end |
| 25 | ret add off len s |
| 26 | end |
| 27 | note prepend moves the n bytes of b right by len p (last byte first), then copies p in front |
| 28 | fn prepend b edit uchar * n ulong p view uchar * to ulong |
| 29 | k = len p |
| 30 | for i ulong n |
| 31 | j = sub sub n 1 i |
| 32 | put b add j k get b j |
| 33 | end |
| 34 | r = copyto b 0 p |
| 35 | ret add n k |
| 36 | end |
| 37 | fn main to int |
| 38 | b = new uchar 64 0 |
| 39 | n = copyto b 0 s0 |
| 40 | n = prepend b n pre |
| 41 | emit part b 0 n |
| 42 | emit nl |
| 43 | ret 0 |
| 44 | end |
| 45 | </syntaxhighlight> |
| 46 | {{out}} |
| 47 | <pre> |
| 48 | Hello, World |
| 49 | </pre> |