forked from zhufuyi/sponge
/
regexp.go
46 lines (40 loc) · 916 Bytes
/
regexp.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
package benchmark
import (
"regexp"
"strings"
)
const (
packagePattern = `\npackage (.*);`
servicePattern = `\nservice (\w+)`
methodPattern = `rpc (\w+)`
)
func getName(data []byte, pattern string) string {
re := regexp.MustCompile(pattern)
matchArr := re.FindStringSubmatch(string(data))
if len(matchArr) == 2 {
return strings.ReplaceAll(matchArr[1], " ", "")
}
return ""
}
func getMethodNames(data []byte, methodPattern string) []string {
re := regexp.MustCompile(methodPattern)
matchArr := re.FindAllStringSubmatch(string(data), -1)
names := []string{}
for _, arr := range matchArr {
if len(arr) == 2 {
names = append(names, strings.ReplaceAll(arr[1], " ", ""))
}
}
return names
}
// match name, not case-sensitive
func matchName(names []string, name string) string {
out := ""
for _, s := range names {
if strings.EqualFold(s, name) {
out = s
break
}
}
return out
}