package yaupdrepo import ( "slices" "strings" "sync" ) type positionFinder struct { sync.Mutex lines []string line_pos int // only advances forward, points to the next line } func NewPositionFinder(ct []byte) *positionFinder { lines := strings.Split(string(ct), "\n") res := &positionFinder{lines: lines} return res } func (pf *positionFinder) remaining_lines() []string { if pf.line_pos >= len(pf.lines) { return nil } return pf.lines[pf.line_pos+1:] } func (pf *positionFinder) find_line_containing(s string) bool { pf.Lock() defer pf.Unlock() for i, l := range pf.remaining_lines() { // fmt.Printf("Line %d: \"%s\"\n", i, l) if strings.Contains(l, s) { pf.line_pos = pf.line_pos + i + 1 return true } } return false } func (pf *positionFinder) add_line(line string) { pf.Lock() defer pf.Unlock() pos := pf.line_pos + 1 pf.lines = slices.Insert(pf.lines, pos, line) } func (pf *positionFinder) Content() []byte { s := strings.Join(pf.lines, "\n") return []byte(s) }