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 / determine-if-a-string-is-numeric.wiki

134 lines · 2315 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 tq "\""
8text tyes "\" numeric\n"
9text tno "\" not numeric\n"
10text s0 "0"
11text s1 "-12"
12text s2 "3.14"
13text s3 "+1e5"
14text s4 "1e-3"
15text s5 ".5"
16text s6 "5."
17text s7 "abc"
18text s9 "12a"
19text s10 "-"
20text s11 "1.2.3"
21text s12 "e5"
22text s13 "1e"
23text s14 "--1"
24text s15 "6.02E+23"
25note emit writes every byte of a to stdout; a short write is retried, an error exits 1
26fn emit a view uchar * to void
27o = ulong 0
28while lt o len a bound 4096
29k = write 1 a o sub len a o
30if le k 0
31exit 1
32end
33o = add o ulong k
34end
35end
36note digits returns the offset after a run of decimal digits starting at i
37fn digits a view uchar * i ulong to ulong
38j = i
39while lt j len a bound 4096
40c = get a j
41if or lt c 48 gt c 57
42ret j
43end
44j = add j 1
45end
46ret j
47end
48note sign returns the offset after an optional + or - at i
49fn sign a view uchar * i ulong to ulong
50if lt i len a
51c = get a i
52if or eq c 43 eq c 45
53ret add i 1
54end
55end
56ret i
57end
58note numeric accepts [+-] then digits with an optional fraction (at least one digit in all), then an
59note optional exponent e or E, [+-], at least one digit; nothing else, no spaces
60fn numeric a view uchar * to bool
61i = sign a 0
62j = digits a i
63m = sub j i
64if lt j len a
65if eq get a j 46
66i = add j 1
67j = digits a i
68m = add m sub j i
69end
70end
71if eq m 0
72ret false
73end
74if lt j len a
75c = get a j
76if or eq c 101 eq c 69
77i = sign a add j 1
78j = digits a i
79if eq j i
80ret false
81end
82end
83end
84ret eq j len a
85end
86fn show a view uchar * to void
87emit tq
88emit a
89if numeric a
90emit tyes
91else
92emit tno
93end
94end
95fn main to int
96show s0
97show s1
98show s2
99show s3
100show s4
101show s5
102show s6
103show s7
104note the empty string: an empty text literal would read as unsized at this pin, so slice one
105show part s7 0 0
106show s9
107show s10
108show s11
109show s12
110show s13
111show s14
112show s15
113ret 0
114end
115</syntaxhighlight>
116{{out}}
117<pre>
118"0" numeric
119"-12" numeric
120"3.14" numeric
121"+1e5" numeric
122"1e-3" numeric
123".5" numeric
124"5." numeric
125"abc" not numeric
126"" not numeric
127"12a" not numeric
128"-" not numeric
129"1.2.3" not numeric
130"e5" not numeric
131"1e" not numeric
132"--1" not numeric
133"6.02E+23" numeric
134</pre>