interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / url-decoding / url-decoding.om

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