forked from elliotchance/c2go
/
regexp.go
57 lines (49 loc) · 1.19 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
package util
import (
"regexp"
"strings"
"sync"
)
// GroupsFromRegex gets RegExp groups after matching it on a line
func GroupsFromRegex(rx, line string) map[string]string {
// We remove tabs and newlines from the regex. This is purely cosmetic,
// as the regex input can be quite long and it's nice for the caller to
// be able to format it in a more readable way.
rx = strings.Replace(rx, "\r", "", -1)
rx = strings.Replace(rx, "\n", "", -1)
rx = strings.Replace(rx, "\t", "", -1)
re := GetRegex(rx)
match := re.FindStringSubmatch(line)
if len(match) == 0 {
return nil
}
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 {
result[name] = match[i]
}
}
return result
}
// cachedRegex - structure for saving regexp`s
type cachedRegex struct {
sync.RWMutex
m map[string]*regexp.Regexp
}
// Global variable
var cr = cachedRegex{m: map[string]*regexp.Regexp{}}
// GetRegex return regexp
// added for minimaze regexp compilation
func GetRegex(rx string) *regexp.Regexp {
cr.RLock()
v, ok := cr.m[rx]
cr.RUnlock()
if ok {
return v
}
// if regexp is not in map
cr.Lock()
cr.m[rx] = regexp.MustCompile(rx)
cr.Unlock()
return GetRegex(rx)
}