// `html.escape` and `http.url_decode` on clean and dirty inputs (the // input stays alive after the `owned` extern call, so the compiler keeps // a reference), `http.url_encode` and its round-trip through // `url_decode`, and `bytes.build_string` on a builder. import std/bytes import std/html import std/http import std/io import std/list fn show_escape :: s: String -> String { "[" <> s <> "] -> [" <> html.escape s <> "]" } fn show_decode :: s: String -> String { "[" <> s <> "] -> [" <> http.url_decode s <> "]" } fn show_encode :: s: String -> String { "[" <> s <> "] -> [" <> http.url_encode s <> "]" } fn round_trip :: s: String -> Bool { http.url_decode (http.url_encode s) == s } pub fn main :: -> () / { io.println (show_escape "") io.println (show_escape "plain") io.println (show_escape "héllo 🦀") io.println (show_escape "Tom & 'Jerry'") io.println (show_escape "&") io.println (show_escape "日本<語") io.println (show_decode "") io.println (show_decode "plain text") io.println (show_decode "a+b%20c") io.println (show_decode "%41%zz%4") io.println (show_decode "%") io.println (show_decode "+a+") io.println (show_decode "%ff") io.println (show_decode "%C3%A9") io.println (show_encode "") io.println (show_encode "plain-text_1.0~x") io.println (show_encode "a b") io.println (show_encode "=&?/#%+") io.println (show_encode "%41") io.println (show_encode "héllo 🦀") // Encoding is not idempotent (`%` becomes `%25`), and decode inverts it. io.println (http.url_encode (http.url_encode "a b")) io.println ( show ( list.all [ "" , "a" , "A Z" , "a+b" , "100%" , "%41" , "=" , "&" , "x=y" , "é" , "☃" , "a/b?c" , " " , "+" , "%" , "\t" , "ünïcodé" ] round_trip ) ) let greeting = bytes.builder () |> bytes.append_string "héllo" |> bytes.append_byte 33 |> bytes.build_string io.println greeting } //> [] -> [] //> [plain] -> [plain] //> [héllo 🦀] -> [héllo 🦀] //> [Tom & 'Jerry'] -> [<a href="x">Tom & 'Jerry'</a>] //> [&] -> [&amp;] //> [日本<語] -> [日本<語] //> [] -> [] //> [plain text] -> [plain text] //> [a+b%20c] -> [a b c] //> [%41%zz%4] -> [A%zz%4] //> [%] -> [%] //> [+a+] -> [ a ] //> [%ff] -> [�] //> [%C3%A9] -> [é] //> [] -> [] //> [plain-text_1.0~x] -> [plain-text_1.0~x] //> [a b] -> [a%20b] //> [=&?/#%+] -> [%3D%26%3F%2F%23%25%2B] //> [%41] -> [%2541] //> [héllo 🦀] -> [h%C3%A9llo%20%F0%9F%A6%80] //> a%2520b //> True //> héllo!