-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumoreader.go
More file actions
85 lines (66 loc) · 1.34 KB
/
sumoreader.go
File metadata and controls
85 lines (66 loc) · 1.34 KB
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
81
82
83
84
85
package sumoreader
import (
"bufio"
"bytes"
"io"
"strings"
)
type SumoReader struct {
scanner *bufio.Scanner
text string
linesScanned int
}
func NewSumoReader(reader io.Reader) (*SumoReader, error) {
scanner := bufio.NewScanner(reader)
//Skip first line (headers)
scanner.Scan()
return &SumoReader{
scanner: scanner,
linesScanned: 1,
}, nil
}
func (sr *SumoReader) Scan() bool {
var buffer bytes.Buffer
defer func() { sr.text = buffer.String() }()
sr.linesScanned++
if !sr.scanner.Scan() {
return false
}
line := sr.scanner.Text()
buffer.WriteString(line)
if isFullRecord(line) {
return true
}
for scanDone := false; !scanDone; {
sr.linesScanned++
more := sr.scanner.Scan()
if !more {
return false
}
line := sr.scanner.Text()
buffer.WriteString(line)
if recordDone(line) {
scanDone = true
}
}
return true
}
func (sr *SumoReader) Text() string {
return sr.text
}
func (sr *SumoReader) Err() error {
return sr.scanner.Err()
}
func isFullRecord(line string) bool {
parts := strings.Split(line, ".")
if len(parts) < 13 {
return false
}
return strings.LastIndex(parts[len(parts)-1], "\"") == len(parts[len(parts)-1])-1
}
func recordDone(line string) bool {
return strings.LastIndex(line, "\"") == len(line)-1
}
func (sr *SumoReader) Lines() int {
return sr.linesScanned
}