...
1package yaupdrepo
2
3import (
4 "slices"
5 "strings"
6 "sync"
7)
8
9type positionFinder struct {
10 sync.Mutex
11 lines []string
12 line_pos int // only advances forward, points to the next line
13}
14
15func NewPositionFinder(ct []byte) *positionFinder {
16 lines := strings.Split(string(ct), "\n")
17 res := &positionFinder{lines: lines}
18 return res
19}
20func (pf *positionFinder) remaining_lines() []string {
21 if pf.line_pos >= len(pf.lines) {
22 return nil
23 }
24 return pf.lines[pf.line_pos+1:]
25}
26func (pf *positionFinder) find_line_containing(s string) bool {
27 pf.Lock()
28 defer pf.Unlock()
29 for i, l := range pf.remaining_lines() {
30 // fmt.Printf("Line %d: \"%s\"\n", i, l)
31 if strings.Contains(l, s) {
32 pf.line_pos = pf.line_pos + i + 1
33 return true
34 }
35 }
36 return false
37}
38func (pf *positionFinder) add_line(line string) {
39 pf.Lock()
40 defer pf.Unlock()
41 pos := pf.line_pos + 1
42 pf.lines = slices.Insert(pf.lines, pos, line)
43
44}
45func (pf *positionFinder) Content() []byte {
46 s := strings.Join(pf.lines, "\n")
47 return []byte(s)
48}
View as plain text