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-mv/blob/main/si/rosetta/tasks/longest-common-prefix/longest-common-prefix.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / longest-common-prefix / longest-common-prefix.om` 86 lines ยท 1658 bytes Raw Blame History 1 note Rosetta Code task "Longest common prefix": https://rosettacode.org/wiki/Longest_common_prefix 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text tarr " -> \"" 6 text tq "\"\n" 7 text lb "[" 8 text rb "]" 9 text l0 "interspecies|interstellar|interstate" 10 text l1 "throne|throne" 11 text l2 "throne|dungeon" 12 text l3 "throne||throne" 13 text l4 "cheese" 14 text l6 "prefix|suffix" 15 text l7 "foo|foobar" 16 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 17 fn emit a view uchar * to void 18 o = ulong 0 19 while lt o len a bound 4096 20 k = write 1 a o sub len a o 21 if le k 0 22 exit 1 23 end 24 o = add o ulong k 25 end 26 end 27 note a list is its elements separated by '|'; an empty text is the empty list. 28 note split records each element as (start, length) and returns how many 29 fn split a view uchar * st edit ulong 16 ln edit ulong 16 to ulong 30 k = ulong 0 31 s = ulong 0 32 for i ulong add len a 1 33 e = eq i len a 34 if not e 35 e = eq get a i 124 36 end 37 if e 38 put st k s 39 put ln k sub i s 40 k = add k 1 41 s = add i 1 42 end 43 end 44 ret k 45 end 46 note common walks the first element and stops where any other element ends or differs 47 fn common a view uchar * st view ulong 16 ln view ulong 16 k ulong to ulong 48 p = ulong 0 49 while lt p get ln 0 bound 4096 50 for w ulong k 51 if eq p get ln w 52 ret p 53 end 54 c = get a add get st 0 p 55 if ne get a add get st w p c 56 ret p 57 end 58 end 59 p = add p 1 60 end 61 ret p 62 end 63 fn lcp a view uchar * to void 64 st = new ulong 16 0 65 ln = new ulong 16 0 66 emit lb 67 emit a 68 emit rb 69 emit tarr 70 if gt len a 0 71 k = split a st ln 72 emit part a get st 0 common a st ln k 73 end 74 emit tq 75 end 76 fn main to int 77 lcp l0 78 lcp l1 79 lcp l2 80 lcp l3 81 lcp l4 82 lcp part l0 0 0 83 lcp l6 84 lcp l7 85 ret 0 86 end