-
Notifications
You must be signed in to change notification settings - Fork 2
/
strategery.go
62 lines (50 loc) · 1.47 KB
/
strategery.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
package autocorrect
import "regexp"
// Strategery for define rule
type strategery struct {
addSpaceRe *regexp.Regexp
addSpaceReverseRe *regexp.Regexp
removeSpaceRe *regexp.Regexp
removeSpaceReverseRe *regexp.Regexp
space bool
reverse bool
}
func newStrategery(one, other string, space, reverse bool) *strategery {
addSpaceStr := "(" + one + `)(` + other + ")"
addSpaceReverseStr := "(" + other + `)(` + one + ")"
removeSpaceStr := `(` + one + `)` + spaceRe + `(` + other + `)`
removeSpaceReverseStr := "(" + other + `)` + spaceRe + `(` + one + ")"
return &strategery{
addSpaceRe: regexp.MustCompile(addSpaceStr),
addSpaceReverseRe: regexp.MustCompile(addSpaceReverseStr),
removeSpaceRe: regexp.MustCompile(removeSpaceStr),
removeSpaceReverseRe: regexp.MustCompile(removeSpaceReverseStr),
space: space,
reverse: reverse,
}
}
func (s *strategery) format(in string) (out string) {
out = in
if s.space {
out = s.addSpace(out)
} else {
out = s.removeSpace(out)
}
return
}
func (s *strategery) addSpace(in string) (out string) {
out = in
out = s.addSpaceRe.ReplaceAllString(out, "$1 $2")
if s.reverse {
out = s.addSpaceReverseRe.ReplaceAllString(out, "$1 $2")
}
return
}
func (s *strategery) removeSpace(in string) (out string) {
out = in
out = s.removeSpaceRe.ReplaceAllString(out, "$1 $2")
if s.reverse {
out = s.removeSpaceReverseRe.ReplaceAllString(out, "$1 $2")
}
return
}