omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / palindrome-detection / palindrome-detection.om
| 1 | note Rosetta Code task "Palindrome detection": https://rosettacode.org/wiki/Palindrome_detection |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text tq "\"" |
| 6 | text tex "\" exact " |
| 7 | text tin " inexact " |
| 8 | text tyes "yes" |
| 9 | text tno "no" |
| 10 | text p1 "rotor" |
| 11 | text p2 "level" |
| 12 | text p3 "A man, a plan, a canal: Panama!" |
| 13 | text p4 "In girum imus nocte et consumimur igni" |
| 14 | text p5 "hello" |
| 15 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 16 | fn emit a view uchar * to void |
| 17 | o = ulong 0 |
| 18 | while lt o len a bound 4096 |
| 19 | k = write 1 a o sub len a o |
| 20 | if le k 0 |
| 21 | exit 1 |
| 22 | end |
| 23 | o = add o ulong k |
| 24 | end |
| 25 | end |
| 26 | fn emitb b bool to void |
| 27 | if b |
| 28 | emit tyes |
| 29 | else |
| 30 | emit tno |
| 31 | end |
| 32 | end |
| 33 | note exact compares bytes from both ends |
| 34 | fn exact a view uchar * to bool |
| 35 | n = len a |
| 36 | for i ulong div n 2 |
| 37 | if ne get a i get a sub sub n 1 i |
| 38 | ret false |
| 39 | end |
| 40 | end |
| 41 | ret true |
| 42 | end |
| 43 | note inexact keeps letters only, lower-cased, then compares (strings up to 256 bytes) |
| 44 | fn inexact a view uchar * to bool |
| 45 | b = new uchar 256 0 |
| 46 | n = ulong 0 |
| 47 | for i ulong len a |
| 48 | c = get a i |
| 49 | if and ge c 65 le c 90 |
| 50 | c = add c 32 |
| 51 | end |
| 52 | if and ge c 97 le c 122 |
| 53 | put b n c |
| 54 | n = add n 1 |
| 55 | end |
| 56 | end |
| 57 | ret exact part b 0 n |
| 58 | end |
| 59 | fn show a view uchar * to void |
| 60 | emit tq |
| 61 | emit a |
| 62 | emit tex |
| 63 | emitb exact a |
| 64 | emit tin |
| 65 | emitb inexact a |
| 66 | emit nl |
| 67 | end |
| 68 | fn main to int |
| 69 | show p1 |
| 70 | show p2 |
| 71 | show p3 |
| 72 | show p4 |
| 73 | show p5 |
| 74 | ret 0 |
| 75 | end |