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