interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / longest-common-prefix.wiki

100 lines · 1975 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text tarr " -> \""
8text tq "\"\n"
9text lb "["
10text rb "]"
11text l0 "interspecies|interstellar|interstate"
12text l1 "throne|throne"
13text l2 "throne|dungeon"
14text l3 "throne||throne"
15text l4 "cheese"
16text l6 "prefix|suffix"
17text l7 "foo|foobar"
18note emit writes every byte of a to stdout; a short write is retried, an error exits 1
19fn emit a view uchar * to void
20o = ulong 0
21while lt o len a bound 4096
22k = write 1 a o sub len a o
23if le k 0
24exit 1
25end
26o = add o ulong k
27end
28end
29note a list is its elements separated by '|'; an empty text is the empty list.
30note split records each element as (start, length) and returns how many
31fn split a view uchar * st edit ulong 16 ln edit ulong 16 to ulong
32k = ulong 0
33s = ulong 0
34for i ulong add len a 1
35e = eq i len a
36if not e
37e = eq get a i 124
38end
39if e
40put st k s
41put ln k sub i s
42k = add k 1
43s = add i 1
44end
45end
46ret k
47end
48note common walks the first element and stops where any other element ends or differs
49fn common a view uchar * st view ulong 16 ln view ulong 16 k ulong to ulong
50p = ulong 0
51while lt p get ln 0 bound 4096
52for w ulong k
53if eq p get ln w
54ret p
55end
56c = get a add get st 0 p
57if ne get a add get st w p c
58ret p
59end
60end
61p = add p 1
62end
63ret p
64end
65fn lcp a view uchar * to void
66st = new ulong 16 0
67ln = new ulong 16 0
68emit lb
69emit a
70emit rb
71emit tarr
72if gt len a 0
73k = split a st ln
74emit part a get st 0 common a st ln k
75end
76emit tq
77end
78fn main to int
79lcp l0
80lcp l1
81lcp l2
82lcp l3
83lcp l4
84lcp part l0 0 0
85lcp l6
86lcp l7
87ret 0
88end
89</syntaxhighlight>
90{{out}}
91<pre>
92[interspecies|interstellar|interstate] -> "inters"
93[throne|throne] -> "throne"
94[throne|dungeon] -> ""
95[throne||throne] -> ""
96[cheese] -> "cheese"
97[] -> ""
98[prefix|suffix] -> ""
99[foo|foobar] -> "foo"
100</pre>