Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/url-encoding.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
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}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text pct "%"
8 text hexd "0123456789ABCDEF"
9 text u1 "http://foo bar/"
10 text u2 "mailto:joe@example.com?subject=a&b c~d_e.f-g"
11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12 fn emit a view uchar * to void
13 o = ulong 0
14 while lt o len a bound 4096
15 k = write 1 a o sub len a o
16 if le k 0
17 exit 1
18 end
19 o = add o ulong k
20 end
21 end
22 note emitc writes one byte
23 fn emitc c uchar to void
24 b = new uchar 1 0
25 put b 0 c
26 emit b
27 end
28 note keep says whether c is written as itself: letters and digits always, - . _ ~ only in the
29 note RFC 3986 variant (rfc true); every other byte becomes % and two upper-case hex digits
30 fn keep c uchar rfc bool to bool
31 if or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122
32 ret true
33 end
34 if and rfc or or eq c 45 eq c 46 or eq c 95 eq c 126
35 ret true
36 end
37 ret false
38 end
39 fn enc a view uchar * rfc bool to void
40 for i ulong len a
41 c = get a i
42 if keep c rfc
43 emitc c
44 else
45 emit pct
46 emitc get hexd shr c 4
47 emitc get hexd and c 15
48 end
49 end
50 emit nl
51 end
52 fn main to int
53 enc u1 false
54 enc u1 true
55 enc u2 false
56 enc u2 true
57 ret 0
58 end
59
60 {{out}}
61
62 http%3A%2F%2Ffoo%20bar%2F
63 http%3A%2F%2Ffoo%20bar%2F
64 mailto%3Ajoe%40example%2Ecom%3Fsubject%3Da%26b%20c%7Ed%5Fe%2Ef%2Dg
65 mailto%3Ajoe%40example.com%3Fsubject%3Da%26b%20c~d_e.f-g
66