-
Notifications
You must be signed in to change notification settings - Fork 3
/
match.go
53 lines (47 loc) · 1.02 KB
/
match.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
package logqlpattern
import (
"strings"
"github.com/go-faster/oteldb/internal/logql"
)
// Match matches given pattern against input string.
func Match(p Pattern, input string, match func(label logql.Label, value string)) bool {
parts := p.Parts
if len(parts) == 0 && input == "" {
return true
}
var ok bool
for i, part := range parts {
switch part.Type {
case Literal:
// Consume literal from input.
input, ok = strings.CutPrefix(input, part.Value)
if !ok {
return false
}
case Capture:
var (
label = logql.Label(part.Value)
value string
)
if i+1 < len(parts) {
// Capture everything until next part.
next := parts[i+1]
value, _, ok = strings.Cut(input, next.Value)
} else {
// Capture remaining string.
value = input
ok = true
}
input = input[len(value):]
// Don't capture `_` labels.
if label != "_" {
match(label, value)
}
// If match failed, capture value anyway.
if !ok || value == "" {
return false
}
}
}
return input == ""
}