From 372d06a0bbc44c0685477e46b38b9739c4d7574c Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 13 Mar 2025 08:54:00 +0800 Subject: [PATCH] Initial version of the runtime library for Hare --- htmpl/htmpl.ha | 41 +++++++++++++++++++++++++++++++++++++++++ diff --git a/htmpl/htmpl.ha b/htmpl/htmpl.ha new file mode 100644 index 0000000000000000000000000000000000000000..71ac4cc984c8b5d3793067f7dc8b36bcca315dcd --- /dev/null +++ b/htmpl/htmpl.ha @@ -0,0 +1,41 @@ +use io; +use memio; +use strings; + +// export type template = struct { +// writer: *fn(io::handle, const []u8) (size | io::error), +// }; + +export fn write(h: io::handle, s: str) (size | io::error) = { + return io::write(h, strings::toutf8(s))?; +}; + +export fn write_escape_html(h: io::handle, s: str) (size | io::error | nomem) = { + let buf = memio::dynamic(); + defer io::close(&buf)!; // TODO: Might not want to abort for syscall interruptions + + let si = strings::iter(s); + for (true) { + match (strings::next(&si)) { + case done => + break; + case let c: rune => + switch (c) { + case '<' => + memio::concat(&buf, "<")?; + case '>' => + memio::concat(&buf, ">")?; + case '&' => + memio::concat(&buf, "&")?; + case '"' => + memio::concat(&buf, """)?; + case '\'' => + memio::concat(&buf, "'")?; + case => + memio::appendrune(&buf, c)?; + }; + }; + }; + + return io::write(h, memio::buffer(&buf))?; +}; -- 2.48.1