-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcombine.go
104 lines (96 loc) · 1.71 KB
/
combine.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package extract
type combineItem struct {
replace bool
content string
}
type Combine []combineItem
func MakeCombine(s string) Combine {
cb := make([]combineItem, 0)
start := 0
i := 0
length := len(s)
for i < length {
if s[i] == '$' {
if s[i+1] == '{' {
j := i + 1
for ; j < length; j++ {
if s[j] == '}' {
break
}
}
if j == length {
i++
} else {
normal := s[start:i]
if len(normal) > 0 {
cb = append(cb, combineItem{replace:false, content:normal})
}
cb = append(cb, combineItem{replace: true, content: s[i+2:j]})
i = j + 1
start = i
}
} else if s[i+1] >= '0' && s[i+1] <= '9' {
normal := s[start:i]
if len(normal) > 0 {
cb = append(cb, combineItem{replace:false, content:normal})
}
cb = append(cb, combineItem{replace:true, content:s[i+1:i+2]})
i = i + 2
start = i
} else {
i++
}
} else {
i++
}
}
if start < length {
normal := s[start:i]
if len(normal) > 0 {
cb = append(cb, combineItem{replace: false, content:normal})
}
}
return cb
}
func (cb Combine) String() string {
s := ""
for _, p := range(cb) {
content := p.content
if p.replace {
s += "${" + content + "}"
} else {
s += content
}
}
return s
}
func (cb Combine) Exec(data map[string]string) string {
s := ""
for _, p := range(cb) {
content := p.content
if !p.replace {
s += content
continue
}
v, ok := data[content]
if !ok {
return ""
}
s += v
}
return s
}
func (cb Combine) ExecLoose(data map[string]string) string {
s := ""
for _, p := range(cb) {
content := p.content
if !p.replace {
s += content
continue
}
if v, ok := data[content]; ok {
s += v
}
}
return s
}