@@ -163,57 +163,82 @@ func BenchmarkRegularReader_50KBLength_1000Targets(b *testing.B) {
163
163
}
164
164
}
165
165
166
- func TestReadLine (t * testing.T ) {
166
+ func TestByteReadLineAndReadLine (t * testing.T ) {
167
167
for _ , test := range []struct {
168
- name string
169
- input string
170
- bufsize int
171
- expectedOutput []string
168
+ name string
169
+ input string
170
+ bufsize int
171
+ expected [][] byte
172
172
}{
173
173
{
174
- name : "empty" ,
175
- input : "" ,
176
- bufsize : 1024 ,
177
- expectedOutput : [] string {} ,
174
+ name : "empty" ,
175
+ input : "" ,
176
+ bufsize : 1024 ,
177
+ expected : nil ,
178
178
},
179
179
{
180
- name : "single-line with no newline" ,
181
- input : " word1, word2 - word3 !@#$%^&*()" ,
182
- bufsize : 1024 ,
183
- expectedOutput : []string {" word1, word2 - word3 !@#$%^&*()" },
180
+ name : "single-line with no newline" ,
181
+ input : " word1, word2 - word3 !@#$%^&*()" ,
182
+ bufsize : 1024 ,
183
+ expected : [][]byte {
184
+ []byte (" word1, word2 - word3 !@#$%^&*()" ),
185
+ },
184
186
},
185
187
{
186
- name : "single-line with ' \\ r' and ' \\ n' " ,
187
- input : "line1\r \n " ,
188
- bufsize : 1024 ,
189
- expectedOutput : []string { "line1" },
188
+ name : "single-line with CR and LF " ,
189
+ input : "line1\r \n " ,
190
+ bufsize : 1024 ,
191
+ expected : [][] byte {[] byte ( "line1" ) },
190
192
},
191
193
{
192
- name : "multi-line - bufsize enough" ,
193
- input : "line1\r \n line2\n line3" ,
194
- bufsize : 1024 ,
195
- expectedOutput : []string { "line1" , "line2" , "line3" },
194
+ name : "multi-line - bufsize enough" ,
195
+ input : "line1\r \n line2\n line3" ,
196
+ bufsize : 1024 ,
197
+ expected : [][] byte {[] byte ( "line1" ), [] byte ( "line2" ), [] byte ( "line3" ) },
196
198
},
197
199
{
198
- name : "multi-line - bufsize not enough; also empty line" ,
199
- input : "line1-0123456789012345\r \n \n line3-0123456789012345" ,
200
- bufsize : 16 , // bufio.minReadBufferSize is 16.
201
- expectedOutput : []string { "line1-0123456789012345" , "" , "line3-0123456789012345" },
200
+ name : "multi-line - bufsize not enough; also empty line" ,
201
+ input : "line1-0123456789012345\r \n \n line3-0123456789012345" ,
202
+ bufsize : 16 , // bufio.minReadBufferSize is 16.
203
+ expected : [][] byte {[] byte ( "line1-0123456789012345" ), nil , [] byte ( "line3-0123456789012345" ) },
202
204
},
203
205
} {
204
206
t .Run (test .name , func (t * testing.T ) {
205
207
r := bufio .NewReaderSize (strings .NewReader (test .input ), test .bufsize )
206
- output := []string {}
208
+ var outputBytes [][]byte
209
+ for {
210
+ line , err := ByteReadLine (r )
211
+ if err != nil {
212
+ assert .Nil (t , line )
213
+ assert .Equal (t , io .EOF , err )
214
+ break
215
+ }
216
+ if line != nil {
217
+ // note the []byte returned by ByteReadLine can be invalidated upon next call, so
218
+ // let's make a copy of it, thus the seemingly unnecessary `[]byte(string(line))`.
219
+ outputBytes = append (outputBytes , []byte (string (line )))
220
+ } else {
221
+ outputBytes = append (outputBytes , line )
222
+ }
223
+ }
224
+ assert .Equal (t , test .expected , outputBytes )
225
+
226
+ r = bufio .NewReaderSize (strings .NewReader (test .input ), test .bufsize )
227
+ outputBytes = outputBytes [:0 ]
207
228
for {
208
229
line , err := ReadLine (r )
209
230
if err != nil {
210
231
assert .Equal (t , "" , line )
211
232
assert .Equal (t , io .EOF , err )
212
233
break
213
234
}
214
- output = append (output , line )
235
+ if line == "" {
236
+ outputBytes = append (outputBytes , nil )
237
+ } else {
238
+ outputBytes = append (outputBytes , []byte (line ))
239
+ }
215
240
}
216
- assert .Equal (t , test .expectedOutput , output )
241
+ assert .Equal (t , test .expected , outputBytes )
217
242
})
218
243
}
219
244
}
0 commit comments