forked from byte-mug/fastnntp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
197 lines (184 loc) · 4.31 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
MIT License
Copyright (c) 2017 Simon Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package fastnntp
import "io"
import "sync"
type buffer struct{
b []byte
pos int
limit int
}
func (b *buffer) read() []byte { return b.b[b.pos:b.limit] }
func (b *buffer) write() []byte { return b.b[b.limit:] }
func (b *buffer) advanceRead(i int) *buffer { b.pos+=i ; return b }
func (b *buffer) advanceWrite(i int) *buffer { b.limit+=i ; return b }
func (b *buffer) reset() *buffer {
b.pos = 0
b.limit = 0
return b
}
func (b *buffer) feedFrom(r io.Reader) error {
i,e := r.Read(b.write())
if i>0 { b.advanceWrite(i) }
return e
}
/*
var pool_buffer = sync.Pool{ New : func() interface{} { return &buffer{
b : make([]byte,1<<13),
}} }
func (b *buffer) release() { pool_buffer.Put(b) }
*/
/*
A reader.
*/
type Reader struct {
b *buffer
r io.Reader
e error
}
var pool_Reader = sync.Pool{ New : func() interface{} {
return &Reader{ b: &buffer{b: make([]byte,1<<13)}}
}}
func AcquireReader() *Reader {
return pool_Reader.Get().(*Reader)
}
func (r *Reader) Release() {
pool_Reader.Put(r)
}
func (r *Reader) Init(rdr io.Reader) *Reader{
r.b.reset()
r.r = rdr
r.e = nil
return r
}
func (r *Reader) Read(b []byte) (int,error) {
d := r.b.read()
if len(d) > len(b) {
copy(b,d)
r.b.advanceRead(len(b))
return len(b),nil
}
if len(d) > 0 {
copy(b,d)
r.b.reset()
return len(d),nil
}
if r.e!=nil { return 0,r.e }
return r.r.Read(b)
}
func (r *Reader) ReadLineB(ext []byte) ([]byte,error) {
for {
buf := r.b.read()
for i,b := range buf {
if b=='\n' {
ext = append(ext,buf[:i+1]...)
r.b.advanceRead(i+1)
return ext,nil
}
}
if len(buf) > 0 {
ext = append(ext,buf...)
}
r.b.reset()
e := r.b.feedFrom(r.r)
if e!=nil { return ext,e }
}
panic("unreachable")
}
type DotReader struct{
r *Reader
state uint16
data []byte
end bool
err error
}
func (d *DotReader) innerRead() {
if d.end || len(d.data) > 0 { return }
buf := d.r.b.read()
if len(buf) == 0 {
d.r.b.reset()
e := d.r.b.feedFrom(d.r.r)
buf = d.r.b.read()
if e!=nil { d.err = e }
}
// Put state into local variable. This enhances performance.
state := d.state
for i,b := range buf {
state = nlDotNl_transition(state,b)
if state == nlDotNl_end {
d.data = buf[:i+1]
d.r.b.advanceRead(i+1)
d.end = true
return
}
}
// Put state back.
d.state = state
d.data = buf
d.r.b.reset()
}
func (d *DotReader) Read(b []byte) (int,error) {
d.innerRead()
e := d.err
buf := d.data
if len(buf) > len(b) {
copy(b,buf)
d.data = buf[len(b):]
return len(b),nil
}
if len(buf) > 0 {
copy(b,buf)
d.data = nil
if d.end { return len(buf),io.EOF }
return len(buf),nil
}
if e==nil { e = io.EOF }
return 0,e
}
// Consumes until end.
func (d *DotReader) Consume() {
for {
d.innerRead()
if d.end || len(d.data) == 0 { return }
d.data = nil
}
}
func (r *Reader) DotReader() (d *DotReader) {
d = pool_DotReader.Get().(*DotReader)
*d = DotReader{
r: r,
state: nlDotNl_start,
data: nil,
end: false,
err: nil,
}
return d
}
var pool_DotReader = sync.Pool{ New : func() interface{} { return new(DotReader) }}
//func AcquireDotReader() *DotReader {
// return pool_DotReader.Get().(*DotReader)
//}
func (r *DotReader) Release() {
// Cut-off references to objects, we might want to be garbage-collected.
r.r = nil
r.data = nil
r.err = nil
pool_DotReader.Put(r)
}