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