interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / strip-whitespace-from-a-string / strip-whitespace-from-a-string.om

53 lines · 1070 bytes Raw Blame History
1note Rosetta Code task "Strip whitespace from a string/Top and tail": https://rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tail
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text lb "["
6text rb "]"
7text msg " \t hello world \t\n "
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note white: space, tab, newline, vertical tab, form feed, carriage return
20fn white c uchar to bool
21ret or eq c 32 and ge c 9 le c 13
22end
23fn lead a view uchar * to ulong
24for i ulong len a
25if not white get a i
26ret i
27end
28end
29ret len a
30end
31fn trail a view uchar * to ulong
32for i ulong len a
33if not white get a sub sub len a 1 i
34ret sub len a i
35end
36end
37ret 0
38end
39fn show a view uchar * to void
40emit lb
41emit a
42emit rb
43emit nl
44end
45fn main to int
46l = lead msg
47t = trail msg
48show msg
49show part msg l sub len msg l
50show part msg 0 t
51show part msg l sub t l
52ret 0
53end