omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / count-occurrences-of-a-substring.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 h1 "the three truths" |
| 8 | text n1 "th" |
| 9 | text h2 "ababababab" |
| 10 | text n2 "abab" |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note count scans left to right and skips past each match, so matches never overlap |
| 38 | fn count s view uchar * t view uchar * to ulong |
| 39 | c = ulong 0 |
| 40 | i = ulong 0 |
| 41 | while le add i len t len s bound 4096 |
| 42 | m = true |
| 43 | for j ulong len t |
| 44 | if ne get s add i j get t j |
| 45 | m = false |
| 46 | end |
| 47 | end |
| 48 | if m |
| 49 | c = add c 1 |
| 50 | i = add i len t |
| 51 | else |
| 52 | i = add i 1 |
| 53 | end |
| 54 | end |
| 55 | ret c |
| 56 | end |
| 57 | fn main to int |
| 58 | emitu count h1 n1 |
| 59 | emit nl |
| 60 | emitu count h2 n2 |
| 61 | emit nl |
| 62 | ret 0 |
| 63 | end |
| 64 | </syntaxhighlight> |
| 65 | {{out}} |
| 66 | <pre> |
| 67 | 3 |
| 68 | 2 |
| 69 | </pre> |