-
Notifications
You must be signed in to change notification settings - Fork 3
/
runtime.odin
42 lines (37 loc) · 1.18 KB
/
runtime.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package temple
import "core:io"
/*
A compiled template, the `with` proc will render the template into the writer using the given `this`.
Use 'Compiled.with' to render the template into the given (recommended to be buffered) writer with 'this' set to the given T in the template.
Use 'Compiled.approx_bytes' to resize your buffered writer before calling.
The returned n is the number of bytes written to the writer.
The returned err is any error that was returned by the writer.
*/
Compiled :: struct($T: typeid) {
with: proc(w: io.Writer, this: T) -> (n: int, err: io.Error),
approx_bytes: int,
}
/*
Writes a string to the writer with special characters that can be used in XSS escaped.
*/
__temple_write_escaped_string :: proc(w: io.Writer, str: string) -> (n: int, err: io.Error) {
for i in 0 ..< len(str) {
b := str[i]
switch b {
case '&':
n += io.write_string(w, "&") or_return
case '"':
n += io.write_string(w, """) or_return
case '\'':
n += io.write_string(w, "'") or_return
case '<':
n += io.write_string(w, "<") or_return
case '>':
n += io.write_string(w, ">") or_return
case:
io.write_byte(w, b) or_return
n += 1
}
}
return
}