-
Notifications
You must be signed in to change notification settings - Fork 2
/
simpledict.go
71 lines (59 loc) · 1.57 KB
/
simpledict.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
package dictionary
import (
"fmt"
"strings"
)
// SimpleDict is a string map Dictionary to do simple key-value replacements
type SimpleDict map[string]string
// Resolve walks the dictionary and updates any values that contain macros with static strings.
func (m *SimpleDict) Resolve() {
newm := make(map[string]string)
for k, v := range *m {
if strings.Contains(v, "%%") {
newm[k] = m.Replacer(v)
} else {
newm[k] = v
}
}
*m = newm
}
// Replacer takes a string, and expands any %%-prefixed strings registered as macros, with their corresponding values.
// Note that shortest-prefixes *may* match first, so for dictionaries of %%VERSION1="one" and %%VERSION12="twelve" you may find
// cases where %%VERSION12 is expanded to "twelve" or "one2". WONTFIX
func (m *SimpleDict) Replacer(in string) string {
var (
out = in
replaced bool
c int
)
for {
out, replaced = m.replacer(out)
if !replaced || c > 20 {
// We're done, or circuit breaker popped
break
}
c++
}
return out
}
// replacer takes a string, and expands any %%-prefixed strings registered as macros, with their corresponding values, returning false when there was nothing to replace
func (m *SimpleDict) replacer(in string) (string, bool) {
if !strings.Contains(in, "%%") {
// Don't waste energy
return in, false
}
out := in
rep := false
for k, v := range *m {
before := out
out = strings.Replace(out, fmt.Sprintf("%s%s", "%%", k), v, -1)
if before != out {
rep = true
}
if !strings.Contains(out, "%%") {
// Let's be done here
break
}
}
return out, rep
}