omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / determine-if-a-string-is-numeric.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text tq "\"" |
| 8 | text tyes "\" numeric\n" |
| 9 | text tno "\" not numeric\n" |
| 10 | text s0 "0" |
| 11 | text s1 "-12" |
| 12 | text s2 "3.14" |
| 13 | text s3 "+1e5" |
| 14 | text s4 "1e-3" |
| 15 | text s5 ".5" |
| 16 | text s6 "5." |
| 17 | text s7 "abc" |
| 18 | text s9 "12a" |
| 19 | text s10 "-" |
| 20 | text s11 "1.2.3" |
| 21 | text s12 "e5" |
| 22 | text s13 "1e" |
| 23 | text s14 "--1" |
| 24 | text s15 "6.02E+23" |
| 25 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 26 | fn emit a view uchar * to void |
| 27 | o = ulong 0 |
| 28 | while lt o len a bound 4096 |
| 29 | k = write 1 a o sub len a o |
| 30 | if le k 0 |
| 31 | exit 1 |
| 32 | end |
| 33 | o = add o ulong k |
| 34 | end |
| 35 | end |
| 36 | note digits returns the offset after a run of decimal digits starting at i |
| 37 | fn digits a view uchar * i ulong to ulong |
| 38 | j = i |
| 39 | while lt j len a bound 4096 |
| 40 | c = get a j |
| 41 | if or lt c 48 gt c 57 |
| 42 | ret j |
| 43 | end |
| 44 | j = add j 1 |
| 45 | end |
| 46 | ret j |
| 47 | end |
| 48 | note sign returns the offset after an optional + or - at i |
| 49 | fn sign a view uchar * i ulong to ulong |
| 50 | if lt i len a |
| 51 | c = get a i |
| 52 | if or eq c 43 eq c 45 |
| 53 | ret add i 1 |
| 54 | end |
| 55 | end |
| 56 | ret i |
| 57 | end |
| 58 | note numeric accepts [+-] then digits with an optional fraction (at least one digit in all), then an |
| 59 | note optional exponent e or E, [+-], at least one digit; nothing else, no spaces |
| 60 | fn numeric a view uchar * to bool |
| 61 | i = sign a 0 |
| 62 | j = digits a i |
| 63 | m = sub j i |
| 64 | if lt j len a |
| 65 | if eq get a j 46 |
| 66 | i = add j 1 |
| 67 | j = digits a i |
| 68 | m = add m sub j i |
| 69 | end |
| 70 | end |
| 71 | if eq m 0 |
| 72 | ret false |
| 73 | end |
| 74 | if lt j len a |
| 75 | c = get a j |
| 76 | if or eq c 101 eq c 69 |
| 77 | i = sign a add j 1 |
| 78 | j = digits a i |
| 79 | if eq j i |
| 80 | ret false |
| 81 | end |
| 82 | end |
| 83 | end |
| 84 | ret eq j len a |
| 85 | end |
| 86 | fn show a view uchar * to void |
| 87 | emit tq |
| 88 | emit a |
| 89 | if numeric a |
| 90 | emit tyes |
| 91 | else |
| 92 | emit tno |
| 93 | end |
| 94 | end |
| 95 | fn main to int |
| 96 | show s0 |
| 97 | show s1 |
| 98 | show s2 |
| 99 | show s3 |
| 100 | show s4 |
| 101 | show s5 |
| 102 | show s6 |
| 103 | show s7 |
| 104 | note the empty string: an empty text literal would read as unsized at this pin, so slice one |
| 105 | show part s7 0 0 |
| 106 | show s9 |
| 107 | show s10 |
| 108 | show s11 |
| 109 | show s12 |
| 110 | show s13 |
| 111 | show s14 |
| 112 | show s15 |
| 113 | ret 0 |
| 114 | end |
| 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> |