interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / url-encoding.wiki

66 lines · 1546 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text pct "%"
8text hexd "0123456789ABCDEF"
9text u1 "http://foo bar/"
10text u2 "mailto:joe@example.com?subject=a&b c~d_e.f-g"
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitc writes one byte
23fn emitc c uchar to void
24b = new uchar 1 0
25put b 0 c
26emit b
27end
28note keep says whether c is written as itself: letters and digits always, - . _ ~ only in the
29note RFC 3986 variant (rfc true); every other byte becomes % and two upper-case hex digits
30fn keep c uchar rfc bool to bool
31if or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122
32ret true
33end
34if and rfc or or eq c 45 eq c 46 or eq c 95 eq c 126
35ret true
36end
37ret false
38end
39fn enc a view uchar * rfc bool to void
40for i ulong len a
41c = get a i
42if keep c rfc
43emitc c
44else
45emit pct
46emitc get hexd shr c 4
47emitc get hexd and c 15
48end
49end
50emit nl
51end
52fn main to int
53enc u1 false
54enc u1 true
55enc u2 false
56enc u2 true
57ret 0
58end
59</syntaxhighlight>
60{{out}}
61<pre>
62http%3A%2F%2Ffoo%20bar%2F
63http%3A%2F%2Ffoo%20bar%2F
64mailto%3Ajoe%40example%2Ecom%3Fsubject%3Da%26b%20c%7Ed%5Fe%2Ef%2Dg
65mailto%3Ajoe%40example.com%3Fsubject%3Da%26b%20c~d_e.f-g
66</pre>