interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / url-encoding / url-encoding.om

56 lines · 1234 bytes Raw Blame History
1note Rosetta Code task "URL encoding": https://rosettacode.org/wiki/URL_encoding
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text pct "%"
6text hexd "0123456789ABCDEF"
7text u1 "http://foo bar/"
8text u2 "mailto:joe@example.com?subject=a&b c~d_e.f-g"
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitc writes one byte
21fn emitc c uchar to void
22b = new uchar 1 0
23put b 0 c
24emit b
25end
26note keep says whether c is written as itself: letters and digits always, - . _ ~ only in the
27note RFC 3986 variant (rfc true); every other byte becomes % and two upper-case hex digits
28fn keep c uchar rfc bool to bool
29if or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122
30ret true
31end
32if and rfc or or eq c 45 eq c 46 or eq c 95 eq c 126
33ret true
34end
35ret false
36end
37fn enc a view uchar * rfc bool to void
38for i ulong len a
39c = get a i
40if keep c rfc
41emitc c
42else
43emit pct
44emitc get hexd shr c 4
45emitc get hexd and c 15
46end
47end
48emit nl
49end
50fn main to int
51enc u1 false
52enc u1 true
53enc u2 false
54enc u2 true
55ret 0
56end