@@ -163,57 +163,82 @@ func BenchmarkRegularReader_50KBLength_1000Targets(b *testing.B) {
163163 }
164164}
165165
166- func TestReadLine (t * testing.T ) {
166+ func TestByteReadLineAndReadLine (t * testing.T ) {
167167 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
172172 }{
173173 {
174- name : "empty" ,
175- input : "" ,
176- bufsize : 1024 ,
177- expectedOutput : [] string {} ,
174+ name : "empty" ,
175+ input : "" ,
176+ bufsize : 1024 ,
177+ expected : nil ,
178178 },
179179 {
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+ },
184186 },
185187 {
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" ) },
190192 },
191193 {
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" ) },
196198 },
197199 {
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" ) },
202204 },
203205 } {
204206 t .Run (test .name , func (t * testing.T ) {
205207 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 ]
207228 for {
208229 line , err := ReadLine (r )
209230 if err != nil {
210231 assert .Equal (t , "" , line )
211232 assert .Equal (t , io .EOF , err )
212233 break
213234 }
214- output = append (output , line )
235+ if line == "" {
236+ outputBytes = append (outputBytes , nil )
237+ } else {
238+ outputBytes = append (outputBytes , []byte (line ))
239+ }
215240 }
216- assert .Equal (t , test .expectedOutput , output )
241+ assert .Equal (t , test .expected , outputBytes )
217242 })
218243 }
219244}
0 commit comments