/
parse.go
85 lines (81 loc) · 2 KB
/
parse.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
74
75
76
77
78
79
80
81
82
83
84
85
package sllm
import (
"bytes"
"io"
"io/ioutil"
"strings"
)
// Parse parses a sllm message create by Expand and calls onArg for every
// `name:value` parameter it finds in the message. When a non-nil buffer is
// passed as tmpl Parse will also reconstruct the original template into the
// buffer. Note that the template is appended to tmpl's content.
func Parse(msg string, tmpl *bytes.Buffer, onArg func(name, value string) error) error {
for len(msg) > 0 {
idx := strings.IndexByte(msg, tmplEsc)
if idx < 0 {
if tmpl != nil {
tmpl.WriteString(msg)
}
return nil
}
if tmpl != nil {
tmpl.WriteString(msg[:idx])
}
msg = msg[idx+1:]
idx = strings.IndexByte(msg, nmSep)
if idx < 0 {
return nil
}
name := msg[:idx]
msg = msg[idx+1:]
idx = strings.IndexByte(msg, tmplEsc)
for {
if idx < 0 {
return nil
}
if idx+1 >= len(msg) || msg[idx+1] != tmplEsc {
break
}
nidx := strings.IndexByte(msg[idx+2:], tmplEsc)
if nidx < 0 {
return nil
}
idx += nidx + 2
}
value := msg[:idx]
err := onArg(name, value)
if err != nil {
return err
}
if tmpl != nil {
tmpl.WriteRune('`')
tmpl.WriteString(name)
tmpl.WriteRune('`')
}
msg = msg[idx+1:]
}
return nil
}
// ParseMap uses Parse to create a map with all parameters assigned to an
// argument in the passed message msg. ParseMap can also reconstruct the
// template when passing a Buffer to tmpl.
func ParseMap(msg string, tmpl *bytes.Buffer) (map[string][]string, error) {
res := make(map[string][]string)
err := Parse(msg, tmpl, func(nm, val string) error {
vls := res[nm]
res[nm] = append(vls, val)
return nil
})
return res, err
}
// ExtractParams extracs the parameter names from template tmpl and appends them
// to appendTo.
func ExtractParams(appendTo []string, tmpl string) ([]string, error) {
_, err := Expand(ioutil.Discard, tmpl,
func(wr io.Writer, idx int, name string) (int, error) {
appendTo = append(appendTo, name)
return len(name), nil
},
)
return appendTo, err
}