forked from laytan/temple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.odin
52 lines (46 loc) · 1.52 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
43
44
45
46
47
48
49
50
51
52
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) {
start := 0
for ; start < len(str); {
replacement := ""
stop := start
for ; stop < len(str); stop += 1 {
switch str[stop] {
case '&':
replacement = "&"
break
case '"':
replacement = """
break
case '\'':
replacement = "'"
break
case '<':
replacement = "<"
break
case '>':
replacement = ">"
break
}
}
n += io.write_string(w, str[start:stop]) or_return
n += io.write_string(w, replacement) or_return
start = stop + 1
}
return
}