-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtagmatcher.go
76 lines (63 loc) · 1.38 KB
/
tagmatcher.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
package ansitags
func NewTagMatcher(start byte, mid []byte, end byte, unknownLength bool) *tagMatcher {
t := tagMatcher{
startByte: start,
midBytes: mid,
endByte: end,
exactMatch: !unknownLength,
totalSize: uint8(len(mid) + 1), // total size without the end byte
position: 0,
}
return &t
}
type tagMatcher struct {
exactMatch bool // lets unknown bytes keep matching before the end character is found
totalSize uint8
position uint8
startByte byte
endByte byte
midBytes []byte
}
func (t *tagMatcher) Seek(pos uint8) {
t.position = pos
}
func (t *tagMatcher) MatchNext(char byte) (matched bool, complete bool) {
if t.position == 0 {
// Look for starting byte
if char == t.startByte {
t.position++
// Still matching
return true, false
}
// Failed to match.
t.position = 0
return false, true
}
if t.position >= t.totalSize {
// Look for ending byte
if char == t.endByte {
t.position++
// Matched and done.
return true, true
}
if t.exactMatch {
// Failed to match and required exact
t.position = 0
return false, true
}
// allows more characters before finishing.
return true, false
}
// Look for mid bytes match
if char == t.midBytes[t.position-1] {
t.position++
// Still matching
return true, false
}
// Failed to match
t.position = 0
return false, true
}
func (t *tagMatcher) Reset() {
t.Seek(0)
}