-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_template.go
82 lines (70 loc) · 1.42 KB
/
message_template.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
package laravalidate
type templateVariableT struct {
from int
to int
}
type messageTemplateParserT struct {
msg []byte
idx int
variables []templateVariableT
}
func parseMsgTemplate(msg []byte) []templateVariableT {
if len(msg) == 0 {
return []templateVariableT{}
}
parser := &messageTemplateParserT{
msg: msg,
idx: 0,
variables: []templateVariableT{},
}
parser.MightParseNextVariable()
return parser.variables
}
func (p *messageTemplateParserT) Next() bool {
return p.idx < len(p.msg)
}
func (p *messageTemplateParserT) C() byte {
return p.msg[p.idx]
}
func (p *messageTemplateParserT) MightParseVariable() {
startIdx := p.idx
for p.Next() {
c := p.C()
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' {
p.idx++
continue
}
break
}
endIdx := p.idx
if startIdx != endIdx {
p.variables = append(p.variables, templateVariableT{from: startIdx - 1, to: endIdx})
}
p.SearchForNextVariable()
}
func (p *messageTemplateParserT) MightParseNextVariable() {
for p.Next() {
c := p.C()
p.idx++
switch c {
case ':':
p.MightParseVariable()
case ' ', '\t', '\n', '\r':
// Do nothing
default:
p.SearchForNextVariable()
return
}
}
}
func (p *messageTemplateParserT) SearchForNextVariable() {
for p.Next() {
c := p.C()
p.idx++
switch c {
case ' ', '\t', '\n', '\r':
p.MightParseNextVariable()
return
}
}
}