forked from wader/fq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_detector.go
33 lines (28 loc) · 960 Bytes
/
loop_detector.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
package apple
import (
"golang.org/x/exp/constraints"
)
// PosLoopDetector is used for detecting loops when writing decoders, and can
// short-circuit infinite recursion that can cause stack overflows.
type PosLoopDetector[T constraints.Integer] []T
// Push adds the current offset to the stack and executes the supplied
// detection function
func (pld *PosLoopDetector[T]) Push(offset T, detect func()) {
for _, o := range *pld {
if offset == o {
detect()
}
}
*pld = append(*pld, offset)
}
// Pop removes the most recently added offset from the stack.
func (pld *PosLoopDetector[T]) Pop() {
*pld = (*pld)[:len(*pld)-1]
}
// PushAndPop adds the current offset to the stack, executes the supplied
// detection function, and returns the Pop method. A good usage of this is to
// pair this method call with a defer statement.
func (pld *PosLoopDetector[T]) PushAndPop(offset T, detect func()) func() {
pld.Push(offset, detect)
return pld.Pop
}