interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / count-occurrences-of-a-substring / count-occurrences-of-a-substring.om

61 lines · 1167 bytes Raw Blame History
1note Rosetta Code task "Count occurrences of a substring": https://rosettacode.org/wiki/Count_occurrences_of_a_substring
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text h1 "the three truths"
6text n1 "th"
7text h2 "ababababab"
8text n2 "abab"
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35note count scans left to right and skips past each match, so matches never overlap
36fn count s view uchar * t view uchar * to ulong
37c = ulong 0
38i = ulong 0
39while le add i len t len s bound 4096
40m = true
41for j ulong len t
42if ne get s add i j get t j
43m = false
44end
45end
46if m
47c = add c 1
48i = add i len t
49else
50i = add i 1
51end
52end
53ret c
54end
55fn main to int
56emitu count h1 n1
57emit nl
58emitu count h2 n2
59emit nl
60ret 0
61end