/
str_get.go
159 lines (148 loc) · 4.34 KB
/
str_get.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package tools
import "bytes"
// GetByte 获取字符串的单个字符值
func (s *String) GetByte(index int) byte {
return s.buf[index]
}
// GetStr 获取字符串的某个片段 返回String
func (s *String) GetStr(index, end int) string {
return string(s.buf[index:end])
}
// GetStrString 获取字符串的某个片段 返回String结构
func (s *String) GetStrString(index, end int) *String {
return &String{buf: s.buf[index:end]}
}
// GetStrStringByRune 获取字符串的某个片段 返回String结构
func (s *String) GetStrStringByRune(index, end int) *String {
return &String{buf: runesToBytes(s.runes[index:end])}
}
// Get 此方法用于取出括号中的内容 支持输入字符model需要为2 下标0为左字符 1为右字符 仅取出第一个匹配的结果
func (s *String) Get(model string) *String {
if len(model) < 2 {
return Make()
}
start := -1
for i := 0; i < s.Len(); i++ {
if s.buf[i] == model[0] {
start = i
} else if s.buf[i] == model[1] {
return s.GetStrString(start+1, i)
}
}
return Make()
}
// GetRune 中文支持 此方法用于取出括号中的内容 支持输入字符model需要为2 下标0为左字符 1为右字符 仅取出第一个匹配的结果
func (s *String) GetRune(model string) *String {
if len(model) < 2 {
return Make()
}
s.runes = bytes.Runes(s.buf)
mRune := bytes.Runes([]byte(model))
start := -1
for i := 0; i < s.Len(); i++ {
if s.runes[i] == mRune[0] {
start = i
} else if s.runes[i] == mRune[1] {
return s.GetStrStringByRune(start+1, i)
}
}
return Make()
}
// GetAll 此方法用于取出括号中的内容 支持输入字符model需要为2 下标0为左字符 1为右字符 取出所有匹配的结果
func (s *String) GetAll(model string) []string {
if len(model) < 2 {
return nil
}
var res []string
start := -1
for i := 0; i < s.Len(); i++ {
if s.buf[i] == model[0] {
start = i
} else if s.buf[i] == model[1] {
res = append(res, s.GetStr(start+1, i))
start = -1
}
}
return res
}
// GetAllRune 此方法用于取出括号中的内容 支持输入字符model需要为2 下标0为左字符 1为右字符 取出所有匹配的结果
func (s *String) GetAllRune(model string) []string {
if len(model) < 2 {
return nil
}
var res []string
s.runes = bytes.Runes(s.buf)
mRune := bytes.Runes([]byte(model))
start := -1
for i := 0; i < s.Len(); i++ {
if s.runes[i] == mRune[0] {
start = i
} else if s.runes[i] == mRune[1] {
res = append(res, s.GetStrStringByRune(start+1, i).String())
start = -1
}
}
return res
}
// GetContent 此方法用于取出固定字符串中的内容,例如<a>mess</a>,注意 仅仅取出第一个匹配项,若要取出所有,请使用GetContentAll
// GetContent("<a>","</a>")
func (s *String) GetContent(label ...string) (content string) {
temp := Make(s)
for {
if i, j := bytes.Index(temp.buf, []byte(label[0])), bytes.Index(temp.buf, []byte(label[1])); i != -1 && j != -1 {
if i < j {
content = s.GetStr(i+len(label[0]), j)
return
} else {
temp.RemoveIndexStr(j + len(label[1]))
}
}
}
}
// GetContentAll 此方法用于取出固定字符串中的内容,例如<a>mess</a>,注意 仅仅取出第一个匹配项,若要取出所有,请使用GetContentAll
// GetContentAll("<a>","</a>")
func (s *String) GetContentAll(label ...string) (content, other []string, steps map[int]struct {
Model int
Index int
}) {
temp := Make(s)
var tempOther string
steps = make(map[int]struct {
Model int
Index int
}, 0)
ste := 0
for {
if i, j := bytes.Index(temp.buf, []byte(label[0])), bytes.Index(temp.buf, []byte(label[1])); i != -1 && j != -1 {
if j < i {
other[len(other)-1] = other[len(other)-1] + temp.GetStr(0, j+len(label[1]))
temp.RemoveIndexStr(j + len(label[1]))
tempOther = label[1]
} else {
content = append(content, temp.GetStr(i+len(label[0]), j))
steps[ste] = struct {
Model int
Index int
}{Model: 0, Index: len(content) - 1}
ste++
other = append(other, tempOther+temp.GetStr(0, i+len(label[0])))
steps[ste] = struct {
Model int
Index int
}{Model: 1, Index: len(other) - 1}
ste++
temp.RemoveIndexStr(j + len(label[1]))
tempOther = label[1]
}
} else {
other = append(other, tempOther+temp.string())
steps[ste] = struct {
Model int
Index int
}{Model: 1, Index: len(other) - 1}
ste++
break
}
}
return
}