-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringSearchEx.go
130 lines (122 loc) · 2.29 KB
/
StringSearchEx.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ToolGood
import "github.com/CloudStriver/ToolGood/tool"
type StringSearchEx struct {
tool.BaseSearchEx
}
func NewStringSearchEx() *StringSearchEx {
return &StringSearchEx{}
}
func (this *StringSearchEx) FindAll(text string) []string {
root := make([]string, 0)
p := 0
for _, c := range text {
t := this.I_dict[c]
if t == 0 {
p = 0
continue
}
next := this.I_next[p] + t
find := this.I_key[next] == t
if find == false && p != 0 {
p = 0
next = this.I_next[0] + t
find = this.I_key[next] == t
}
if find {
index := this.I_check[next]
if index > 0 {
for _, item := range this.I_guides[index] {
root = append(root, this.I_keywords[item])
}
}
p = next
}
}
return root
}
func (this *StringSearchEx) FindFirst(text string) string {
p := 0
for _, c := range text {
t := this.I_dict[c]
if t == 0 {
p = 0
continue
}
next := this.I_next[p] + t
find := this.I_key[next] == t
if find == false && p != 0 {
p = 0
next = this.I_next[0] + t
find = this.I_key[next] == t
}
if find {
index := this.I_check[next]
if index > 0 {
return this.I_keywords[this.I_guides[index][0]]
}
p = next
}
}
return ""
}
func (this *StringSearchEx) ContainsAny(text string) bool {
p := 0
for _, c := range text {
t := this.I_dict[c]
if t == 0 {
p = 0
continue
}
next := this.I_next[p] + t
if this.I_key[next] == t {
if this.I_check[next] > 0 {
return true
}
p = next
} else {
p = 0
next = this.I_next[p] + t
if this.I_key[next] == t {
if this.I_check[next] > 0 {
return true
}
p = next
}
}
}
return false
}
func (this *StringSearchEx) Replace(text string, replaceChar rune) string {
result := []rune(text)
p := 0
var i int
for _, c := range text {
t := this.I_dict[c]
if t == 0 {
p = 0
i++
continue
}
next := this.I_next[p] + t
find := this.I_key[next] == t
if find == false && p != 0 {
p = 0
next = this.I_next[0] + t
find = this.I_key[next] == t
}
if find {
index := this.I_check[next]
if index > 0 {
r := this.I_keywords[this.I_guides[index][0]]
maxLength := len([]rune(r))
start := i + 1 - maxLength
for j := start; j <= i; j++ {
result[j] = replaceChar
}
}
p = next
}
i++
}
return string(result)
}