summaryrefslogtreecommitdiff
path: root/linesScanner.go
blob: 6677b459646d2d1cc89de5e826234c7760957d45 (plain)
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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package gitpb

import (
	"fmt"
	sync "sync"
)

// taken from autogenpb. this should be in some generic library or just in GO
// var allTheLines *LinesScanner

// newLinesScanner initializes a new LinesScanner iterator with a slice of strings.
func NewLinesScanner(things []string) *LinesScanner {
	return &LinesScanner{things: things}
}

// LinesScanner provides an iterator over a slice of strings, allowing for
// sequential access and the ability to un-scan (step back).
type LinesScanner struct {
	sync.Mutex

	things []string
	index  int
}

// Scan advances the iterator to the next line. It returns false if there are
// no more lines.
func (it *LinesScanner) Scan() bool {
	if it.index >= len(it.things) {
		return false
	}
	it.Lock()
	it.index++
	it.Unlock()
	return true
}

// UnScan moves the iterator back one line. It returns false if the iterator
// is already at the beginning.
func (it *LinesScanner) UnScan() bool {
	if it.index < 1 {
		it.index = 0
		return false
	}
	it.Lock()
	it.index--
	it.Unlock()
	return true
}

// NextRaw returns the current line from the scanner without any modification.
func (it *LinesScanner) NextRaw() string {
	if it.index-1 == len(it.things) {
		fmt.Println("Next() error in LinesScanner", it.index)
	}
	return it.things[it.index-1]
}

func (it *LinesScanner) Err() error {
	return nil
}

// alias as some scanners use Text()
func (it *LinesScanner) Text() string {
	return it.Next()
}

// Next returns the current line from the scanner with leading and trailing
// whitespace removed.
func (it *LinesScanner) Next() string {
	if it.index-1 == len(it.things) {
		fmt.Println("Next() error in LinesScanner", it.index)
	}
	// out := commentPreprocessor(it.things[it.index-1])
	out := it.things[it.index-1]
	return out
	// return out
}