-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparsed_lines.go
64 lines (56 loc) · 1.21 KB
/
parsed_lines.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
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
package table
// Parsed represents parsed aligned table
type Parsed []parsedLine
type parsedLine struct {
original string
parsed []string
}
// FindLine returns parsed version of the first line matching predicate
func (p Parsed) FindLine(predicate func(string) bool) []string {
for _, line := range p {
if predicate(line.original) {
return line.parsed
}
}
return nil
}
// Lines returns parsed line
func (p Parsed) Lines() [][]string {
result := make([][]string, len(p))
for i, line := range p {
result[i] = line.parsed
}
return result
}
// Head returns first parsed line
func (p Parsed) Head() ([]string, bool) {
if len(p) == 0 {
return nil, false
}
return p[0].parsed, true
}
// SkipTo line matching predicate
func (p Parsed) SkipTo(predicate func(string) bool) Parsed {
for i, s := range p {
if predicate(s.original) {
return p[i:]
}
}
return nil
}
// TakeTo removes everything after the first match of the predicate
func (p Parsed) TakeTo(predicate func(string) bool) Parsed {
for i, s := range p {
if predicate(s.original) {
return p[:i]
}
}
return p
}
// SkipOneLine or none if text is already empty
func (p Parsed) SkipOneLine() Parsed {
if len(p) == 0 {
return p
}
return p[1:]
}