-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstarter.go
50 lines (41 loc) · 1.15 KB
/
starter.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
package pogo
import (
"fmt"
"regexp"
"strings"
)
// Starter is an pair of border and prefix
type Starter interface {
Extract(line string) (border, prefix string, ok bool)
}
type plainStarter struct {
border, prefix string
}
// NewPlainStarter returns new plain starter
func NewPlainStarter(border, prefix string) Starter {
return plainStarter{border, prefix}
}
// Extract implements Starter.Extract
func (ps plainStarter) Extract(line string) (border, prefix string, ok bool) {
if strings.HasPrefix(line, ps.border+ps.prefix) {
return ps.border, ps.prefix, true
}
return "", "", false
}
type regexpStarter struct {
matcher *regexp.Regexp
border, prefix string
}
// NewRegexpStarter returns new regexp starter
func NewRegexpStarter(border, prefix string) Starter {
matcher := regexp.MustCompile(fmt.Sprintf(`^(%s)(%s)`, border, prefix))
return regexpStarter{matcher, border, prefix}
}
// Extract implements Starter.Extract
func (rs regexpStarter) Extract(line string) (border, prefix string, ok bool) {
if rs.matcher.MatchString(line) {
submatch := rs.matcher.FindStringSubmatch(line)
return submatch[1], submatch[2], true
}
return "", "", false
}