Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
171 lines (141 loc) · 4.17 KB
/
Copy pathutil.go
File metadata and controls
171 lines (141 loc) · 4.17 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package match
import (
"fmt"
"strings"
"github.com/ethereum-optimism/optimism/op-devstack/stack"
)
func First[I comparable, E stack.Identifiable[I]]() stack.Matcher[I, E] {
return ByIndex[I, E](0)
}
func Second[I comparable, E stack.Identifiable[I]]() stack.Matcher[I, E] {
return ByIndex[I, E](1)
}
func byID[I comparable, E stack.Identifiable[I]](id I) stack.Matcher[I, E] {
return MatchElemFn[I, E](func(elem E) bool {
return elem.ID() == id
})
}
type byIndexMatcher[I comparable, E stack.Identifiable[I]] struct {
index int
}
func (ma byIndexMatcher[I, E]) Match(elems []E) []E {
if ma.index < 0 {
return nil
}
if ma.index >= len(elems) {
return nil
}
return elems[ma.index : ma.index+1]
}
func (ma byIndexMatcher[I, E]) String() string {
return fmt.Sprintf("ByIndex(%d)", ma.index)
}
// ByIndex matches element i (zero-indexed).
func ByIndex[I comparable, E stack.Identifiable[I]](index int) stack.Matcher[I, E] {
return byIndexMatcher[I, E]{index: index}
}
type lastMatcher[I comparable, E stack.Identifiable[I]] struct{}
func (ma lastMatcher[I, E]) Match(elems []E) []E {
if len(elems) == 0 {
return nil
}
return elems[len(elems)-1:]
}
func (ma lastMatcher[I, E]) String() string {
return "Last"
}
// Last matches the last element.
func Last[I comparable, E stack.Identifiable[I]]() stack.Matcher[I, E] {
return lastMatcher[I, E]{}
}
type onlyMatcher[I comparable, E stack.Identifiable[I]] struct{}
func (ma onlyMatcher[I, E]) Match(elems []E) []E {
if len(elems) != 1 {
return nil
}
return elems
}
func (ma onlyMatcher[I, E]) String() string {
return "Only"
}
// Only matches the only value. If there are none, or more than one, then no value is matched.
func Only[I comparable, E stack.Identifiable[I]]() stack.Matcher[I, E] {
return onlyMatcher[I, E]{}
}
type andMatcher[I comparable, E stack.Identifiable[I]] struct {
inner []stack.Matcher[I, E]
}
func (ma andMatcher[I, E]) Match(elems []E) []E {
for _, matcher := range ma.inner {
elems = matcher.Match(elems)
}
return elems
}
func (ma andMatcher[I, E]) String() string {
return fmt.Sprintf("And(%s)", joinStr(ma.inner))
}
// And combines all the matchers, by running them all, narrowing down the set with each application.
// If none are provided, all inputs are matched.
func And[I comparable, E stack.Identifiable[I]](matchers ...stack.Matcher[I, E]) stack.Matcher[I, E] {
return andMatcher[I, E]{inner: matchers}
}
type orMatcher[I comparable, E stack.Identifiable[I]] struct {
inner []stack.Matcher[I, E]
}
func (ma orMatcher[I, E]) Match(elems []E) []E {
seen := make(map[I]struct{})
for _, matcher := range ma.inner {
for _, elem := range matcher.Match(elems) {
seen[elem.ID()] = struct{}{}
}
}
// preserve sort order and duplicates by iterating the original list
out := make([]E, 0, len(seen))
for _, elem := range elems {
if _, ok := seen[elem.ID()]; ok {
out = append(out, elem)
}
}
return out
}
func (ma orMatcher[I, E]) String() string {
return fmt.Sprintf("Or(%s)", joinStr(ma.inner))
}
func joinStr[V fmt.Stringer](elems []V) string {
var out strings.Builder
for i, e := range elems {
out.WriteString(e.String())
if i < len(elems)-1 {
out.WriteString(", ")
}
}
return out.String()
}
// Or returns each of the inputs that have a match with any of the matchers.
// All inputs are applied to all matchers, even if matched previously.
func Or[I comparable, E stack.Identifiable[I]](matchers ...stack.Matcher[I, E]) stack.Matcher[I, E] {
return orMatcher[I, E]{inner: matchers}
}
type notMatcher[I comparable, E stack.Identifiable[I]] struct {
inner stack.Matcher[I, E]
}
func (ma notMatcher[I, E]) Match(elems []E) []E {
matched := make(map[I]struct{})
for _, elem := range ma.inner.Match(elems) {
matched[elem.ID()] = struct{}{}
}
out := make([]E, 0, len(elems))
for _, elem := range elems {
if _, ok := matched[elem.ID()]; !ok {
out = append(out, elem)
}
}
return out
}
func (ma notMatcher[I, E]) String() string {
return fmt.Sprintf("Not(%s)", ma.inner)
}
// Not matches the elements that do not match the given matcher.
func Not[I comparable, E stack.Identifiable[I]](matcher stack.Matcher[I, E]) stack.Matcher[I, E] {
return notMatcher[I, E]{inner: matcher}
}