interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / si / rosetta / tasks / longest-common-prefix / longest-common-prefix.om

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