omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / string-comparison / string-comparison.om
| 1 | note Rosetta Code task "String comparison": https://rosettacode.org/wiki/String_comparison |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text a1 "alpha" |
| 7 | text b1 "beta" |
| 8 | text a2 "Hello" |
| 9 | text b2 "hello" |
| 10 | text a3 "abc" |
| 11 | text b3 "abcd" |
| 12 | text teq " eq=" |
| 13 | text tlt " lt=" |
| 14 | text tgt " gt=" |
| 15 | text tci " equal-ignoring-case=" |
| 16 | text tyes "yes" |
| 17 | text tno "no" |
| 18 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 19 | fn emit a view uchar * to void |
| 20 | o = ulong 0 |
| 21 | while lt o len a bound 4096 |
| 22 | k = write 1 a o sub len a o |
| 23 | if le k 0 |
| 24 | exit 1 |
| 25 | end |
| 26 | o = add o ulong k |
| 27 | end |
| 28 | end |
| 29 | fn emitb b bool to void |
| 30 | if b |
| 31 | emit tyes |
| 32 | else |
| 33 | emit tno |
| 34 | end |
| 35 | end |
| 36 | note cmp is byte order: -1, 0 or 1; a proper prefix sorts first; ci folds ASCII case first |
| 37 | fn cmp a view uchar * b view uchar * ci bool to int |
| 38 | n = len a |
| 39 | if lt len b n |
| 40 | n = len b |
| 41 | end |
| 42 | for i ulong n |
| 43 | x = get a i |
| 44 | y = get b i |
| 45 | if and ci and ge x 65 le x 90 |
| 46 | x = add x 32 |
| 47 | end |
| 48 | if and ci and ge y 65 le y 90 |
| 49 | y = add y 32 |
| 50 | end |
| 51 | if lt x y |
| 52 | ret -1 |
| 53 | end |
| 54 | if gt x y |
| 55 | ret 1 |
| 56 | end |
| 57 | end |
| 58 | if lt len a len b |
| 59 | ret -1 |
| 60 | end |
| 61 | if gt len a len b |
| 62 | ret 1 |
| 63 | end |
| 64 | ret 0 |
| 65 | end |
| 66 | fn show a view uchar * b view uchar * to void |
| 67 | c = cmp a b false |
| 68 | emit a |
| 69 | emit sp |
| 70 | emit b |
| 71 | emit teq |
| 72 | emitb eq c 0 |
| 73 | emit tlt |
| 74 | emitb lt c 0 |
| 75 | emit tgt |
| 76 | emitb gt c 0 |
| 77 | emit tci |
| 78 | emitb eq cmp a b true 0 |
| 79 | emit nl |
| 80 | end |
| 81 | fn main to int |
| 82 | show a1 b1 |
| 83 | show a2 b2 |
| 84 | show a3 b3 |
| 85 | show b3 a3 |
| 86 | ret 0 |
| 87 | end |