tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

109 lignes · 2827 octetsBrut · Historique
1// `html.escape` and `http.url_decode` on clean and dirty inputs (the
2// input stays alive after the `owned` extern call, so the compiler keeps
3// a reference), `http.url_encode` and its round-trip through
4// `url_decode`, and `bytes.build_string` on a builder.
5
6import std/bytes
7import std/html
8import std/http
9import std/io
10import std/list
11
12fn show_escape :: s: String -> String
13{
14 "[" <> s <> "] -> [" <> html.escape s <> "]"
15}
16
17fn show_decode :: s: String -> String
18{
19 "[" <> s <> "] -> [" <> http.url_decode s <> "]"
20}
21
22fn show_encode :: s: String -> String
23{
24 "[" <> s <> "] -> [" <> http.url_encode s <> "]"
25}
26
27fn round_trip :: s: String -> Bool
28{
29 http.url_decode (http.url_encode s) == s
30}
31
32pub fn main :: -> () / <io>
33{
34 io.println (show_escape "")
35 io.println (show_escape "plain")
36 io.println (show_escape "héllo 🦀")
37 io.println (show_escape "<a href=\"x\">Tom & 'Jerry'</a>")
38 io.println (show_escape "&amp;")
39 io.println (show_escape "日本<語")
40 io.println (show_decode "")
41 io.println (show_decode "plain text")
42 io.println (show_decode "a+b%20c")
43 io.println (show_decode "%41%zz%4")
44 io.println (show_decode "%")
45 io.println (show_decode "+a+")
46 io.println (show_decode "%ff")
47 io.println (show_decode "%C3%A9")
48 io.println (show_encode "")
49 io.println (show_encode "plain-text_1.0~x")
50 io.println (show_encode "a b")
51 io.println (show_encode "=&?/#%+")
52 io.println (show_encode "%41")
53 io.println (show_encode "héllo 🦀")
54 // Encoding is not idempotent (`%` becomes `%25`), and decode inverts it.
55 io.println (http.url_encode (http.url_encode "a b"))
56 io.println
57 ( show
58 ( list.all
59 [ ""
60 , "a"
61 , "A Z"
62 , "a+b"
63 , "100%"
64 , "%41"
65 , "="
66 , "&"
67 , "x=y"
68 , "é"
69 , "☃"
70 , "a/b?c"
71 , " "
72 , "+"
73 , "%"
74 , "\t"
75 , "ünïcodé"
76 ]
77 round_trip
78 )
79 )
80 let greeting =
81 bytes.builder ()
82 |> bytes.append_string "héllo"
83 |> bytes.append_byte 33
84 |> bytes.build_string
85 io.println greeting
86}
87//> [] -> []
88//> [plain] -> [plain]
89//> [héllo 🦀] -> [héllo 🦀]
90//> [<a href="x">Tom & 'Jerry'</a>] -> [&lt;a href=&quot;x&quot;&gt;Tom &amp; &#39;Jerry&#39;&lt;/a&gt;]
91//> [&amp;] -> [&amp;amp;]
92//> [日本<語] -> [日本&lt;語]
93//> [] -> []
94//> [plain text] -> [plain text]
95//> [a+b%20c] -> [a b c]
96//> [%41%zz%4] -> [A%zz%4]
97//> [%] -> [%]
98//> [+a+] -> [ a ]
99//> [%ff] -> [�]
100//> [%C3%A9] -> [é]
101//> [] -> []
102//> [plain-text_1.0~x] -> [plain-text_1.0~x]
103//> [a b] -> [a%20b]
104//> [=&?/#%+] -> [%3D%26%3F%2F%23%25%2B]
105//> [%41] -> [%2541]
106//> [héllo 🦀] -> [h%C3%A9llo%20%F0%9F%A6%80]
107//> a%2520b
108//> True
109//> héllo!