-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
73 lines (59 loc) · 1.68 KB
/
util.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package easytpl
import (
"bytes"
"fmt"
"sync"
)
func panicf(format string, args ...any) {
if len(args) > 0 {
panic("easyTpl: [ERROR] " + fmt.Sprintf(format, args...))
}
// only error message
panic("easyTpl: [ERROR] " + format)
}
func panicErr(err error) {
if err != nil {
panic("easyTpl: [ERROR] " + err.Error())
}
}
// match '{{ extend "parent.tpl" }}'
// var extendsRegex = regexp.MustCompile(`{{ *?extends +?"(.+?)" *?}}`)
var extendsBytes = []byte("extends ")
// parse line '{{ extend "parent.tpl" }}' and get "parent.tpl"
func getExtendsTplName(line []byte, td TplDelims) (string, bool) {
line = bytes.TrimRight(line, " \t")
if bytes.HasPrefix(line, []byte(td.Left)) &&
bytes.HasSuffix(line, []byte(td.Right)) &&
bytes.Contains(line, extendsBytes) {
leftLen, rightLen := len(td.Left), len(td.Right)
// remove left and right delimiters and spaces
content := bytes.Trim(line[leftLen:len(line)-rightLen], " \"'")
// remove "extends " prefix
return string(bytes.TrimLeft(content[8:], " \"'")), true
}
return "", false
}
/*************************************************************
* buffer Pool
*************************************************************/
// bufferPool A bufferPool is a type-safe wrapper around a sync.Pool.
type bufferPool struct {
p *sync.Pool
}
// newBufferPool constructs a new bufferPool.
func newBufferPool() *bufferPool {
return &bufferPool{&sync.Pool{
New: func() any {
return &bytes.Buffer{}
},
}}
}
// Get retrieves a Buffer from the pool, creating one if necessary.
func (bp bufferPool) get() *bytes.Buffer {
buf := bp.p.Get().(*bytes.Buffer)
return buf
}
func (bp bufferPool) put(buf *bytes.Buffer) {
buf.Reset()
bp.p.Put(buf)
}