omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / string-matching.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 sp " " |
| 8 | text hay "tacoloco" |
| 9 | text nd "co" |
| 10 | text tst "starts with: " |
| 11 | text tct "found at:" |
| 12 | text ten "ends with: " |
| 13 | text tyes "yes" |
| 14 | text tno "no" |
| 15 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 16 | fn emit a view uchar * to void |
| 17 | o = ulong 0 |
| 18 | while lt o len a bound 4096 |
| 19 | k = write 1 a o sub len a o |
| 20 | if le k 0 |
| 21 | exit 1 |
| 22 | end |
| 23 | o = add o ulong k |
| 24 | end |
| 25 | end |
| 26 | note emitu writes n in decimal; 20 digits hold every ulong |
| 27 | fn emitu n ulong to void |
| 28 | d = new uchar 20 0 |
| 29 | i = ulong 20 |
| 30 | v = n |
| 31 | while true bound 20 |
| 32 | i = sub i 1 |
| 33 | put d i uchar add rem v 10 48 |
| 34 | v = div v 10 |
| 35 | if eq v 0 |
| 36 | break |
| 37 | end |
| 38 | end |
| 39 | emit part d i sub 20 i |
| 40 | end |
| 41 | fn emitb b bool to void |
| 42 | if b |
| 43 | emit tyes |
| 44 | else |
| 45 | emit tno |
| 46 | end |
| 47 | end |
| 48 | note at tells whether t occurs in s at offset i (false when it would run past the end) |
| 49 | fn at2 s view uchar * t view uchar * i ulong to bool |
| 50 | if gt add i len t len s |
| 51 | ret false |
| 52 | end |
| 53 | for j ulong len t |
| 54 | if ne get s add i j get t j |
| 55 | ret false |
| 56 | end |
| 57 | end |
| 58 | ret true |
| 59 | end |
| 60 | fn main to int |
| 61 | emit tst |
| 62 | emitb at2 hay nd 0 |
| 63 | emit nl |
| 64 | emit tct |
| 65 | for i ulong len hay |
| 66 | if at2 hay nd i |
| 67 | emit sp |
| 68 | emitu i |
| 69 | end |
| 70 | end |
| 71 | emit nl |
| 72 | emit ten |
| 73 | emitb at2 hay nd sub len hay len nd |
| 74 | emit nl |
| 75 | ret 0 |
| 76 | end |
| 77 | </syntaxhighlight> |
| 78 | {{out}} |
| 79 | <pre> |
| 80 | starts with: no |
| 81 | found at: 2 6 |
| 82 | ends with: yes |
| 83 | </pre> |