-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif.go
56 lines (52 loc) · 1.3 KB
/
if.go
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
53
54
55
56
package xtpl
import (
"bytes"
"unicode"
)
type ifStruct struct {
equal func(vars *xVarCollection) bool
tree []treeNode
}
func (x *xtpl) execIf(src []rune) func(vars *xVarCollection) []byte {
var variants []ifStruct
for len(src) > 0 {
var variant ifStruct
bracketBegin := -1
bracketEnd := -1
if !unicode.IsSpace(src[0]) {
bracketBegin = getOffset(src, "(", "", true, true)
bracketEnd = getOffset(src, ")", "", true, true)
}
if bracketBegin > -1 && bracketEnd > bracketBegin {
var equal = x.exec(src[bracketBegin : bracketEnd+1])
variant.equal = func(vars *xVarCollection) bool {
return equal(vars).toBool()
}
} else {
variant.equal = func(vars *xVarCollection) bool {
return true
}
}
contentBegin := bracketEnd + 1
if contentEnd := getOffset(src, "@else", "@if", false, false); contentEnd < 0 {
variant.tree = x.buildTree(src[contentBegin:], true)
src = nil
} else {
variant.tree = x.buildTree(src[contentBegin:contentEnd], true)
src = src[contentEnd+5:]
}
variants = append(variants, variant)
}
return func(vars *xVarCollection) []byte {
for _, variant := range variants {
if variant.equal(vars) {
var buff = &bytes.Buffer{}
for _, f := range variant.tree {
buff.Write(f(vars))
}
return buff.Bytes()
}
}
return nil
}
}