-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
102 lines (86 loc) · 1.33 KB
/
reader.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package clf
import (
"bufio"
"io"
)
type Reader struct {
br *bufio.Reader
err error
}
// NewReader returns a new Reader
func NewReader(r io.Reader) *Reader {
return &Reader{br: bufio.NewReader(r)}
}
// Read reads the next log from the input.
func (r *Reader) Read() (Log, error) {
var l Log
var err error
for {
l.Raw, err = r.br.ReadString('\n')
if err != nil {
return l, err
}
// check for blank lines
if len(l.Raw) > 1 {
break
}
}
scan(&l)
return l, nil
}
type state int
const (
start state = iota
plain
quote
bracket
)
func scan(l *Log) {
var state state
var f int
for i, c := range l.Raw {
switch state {
case start:
switch c {
case ' ':
continue
case '"':
f = i + 1
state = quote
case '[':
f = i + 1
state = bracket
default:
f = i
state = plain
}
case plain:
switch c {
case ' ', '\n':
l.Fields = append(l.Fields, l.Raw[f:i])
state = start
default:
continue
}
case quote:
switch c {
case '"', '\n':
l.Fields = append(l.Fields, l.Raw[f:i])
state = start
default:
continue
}
case bracket:
switch c {
case ']', '\n':
l.Fields = append(l.Fields, l.Raw[f:i])
state = start
default:
continue
}
}
}
if state != start {
l.Fields = append(l.Fields, l.Raw[f:len(l.Raw)])
}
}