Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/count-occurrences-of-a-substring/count-occurrences-of-a-substring.om # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 1 note Rosetta Code task "Count occurrences of a substring": https://rosettacode.org/wiki/Count_occurrences_of_a_substring 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text h1 "the three truths" 6 text n1 "th" 7 text h2 "ababababab" 8 text n2 "abab" 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 emitu writes n in decimal; 20 digits hold every ulong 21 fn emitu n ulong to void 22 d = new uchar 20 0 23 i = ulong 20 24 v = n 25 while true bound 20 26 i = sub i 1 27 put d i uchar add rem v 10 48 28 v = div v 10 29 if eq v 0 30 break 31 end 32 end 33 emit part d i sub 20 i 34 end 35 note count scans left to right and skips past each match, so matches never overlap 36 fn count s view uchar * t view uchar * to ulong 37 c = ulong 0 38 i = ulong 0 39 while le add i len t len s bound 4096 40 m = true 41 for j ulong len t 42 if ne get s add i j get t j 43 m = false 44 end 45 end 46 if m 47 c = add c 1 48 i = add i len t 49 else 50 i = add i 1 51 end 52 end 53 ret c 54 end 55 fn main to int 56 emitu count h1 n1 57 emit nl 58 emitu count h2 n2 59 emit nl 60 ret 0 61 end