omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / url-decoding / url-decoding.om
| 1 | note Rosetta Code task "URL decoding": https://rosettacode.org/wiki/URL_decoding |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text u1 "http%3A%2F%2Ffoo%20bar%2F" |
| 6 | text u2 "google.com/search?q=%60Abdu%27l-Bah%C3%A1" |
| 7 | text u3 "%25%32%35" |
| 8 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 9 | fn emit a view uchar * to void |
| 10 | o = ulong 0 |
| 11 | while lt o len a bound 4096 |
| 12 | k = write 1 a o sub len a o |
| 13 | if le k 0 |
| 14 | exit 1 |
| 15 | end |
| 16 | o = add o ulong k |
| 17 | end |
| 18 | end |
| 19 | note hexv gives the value of one hex digit, either case; anything else traps 9 |
| 20 | fn hexv c uchar to uchar |
| 21 | if and ge c 48 le c 57 |
| 22 | ret sub c 48 |
| 23 | end |
| 24 | if and ge c 65 le c 70 |
| 25 | ret sub c 55 |
| 26 | end |
| 27 | if and ge c 97 le c 102 |
| 28 | ret sub c 87 |
| 29 | end |
| 30 | trap 9 |
| 31 | end |
| 32 | note dec turns every %XY into the byte 0xXY; the result bytes are written as they are (here UTF-8) |
| 33 | fn dec a view uchar * to void |
| 34 | o = new uchar 256 0 |
| 35 | p = ulong 0 |
| 36 | i = ulong 0 |
| 37 | while lt i len a bound 4096 |
| 38 | c = get a i |
| 39 | if eq c 37 |
| 40 | c = or shl hexv get a add i 1 4 hexv get a add i 2 |
| 41 | i = add i 3 |
| 42 | else |
| 43 | i = add i 1 |
| 44 | end |
| 45 | put o p c |
| 46 | p = add p 1 |
| 47 | end |
| 48 | emit part o 0 p |
| 49 | emit nl |
| 50 | end |
| 51 | fn main to int |
| 52 | dec u1 |
| 53 | dec u2 |
| 54 | dec u3 |
| 55 | ret 0 |
| 56 | end |