Skip to content

Commit

Permalink
resolve comment
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Apr 10, 2023
1 parent c7426ad commit 3f93344
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions internal/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,30 @@ limitations under the License.
package io

import (
"errors"
"bytes"
"io"
)

// ReadLine reads a line from the reader with trailing \r dropped.
func ReadLine(reader io.Reader) ([]byte, error) {
var line []byte
var buffer [1]byte
drop := 0
for {
n, err := reader.Read(buffer[:])
if err != nil {
if err == io.EOF {
// a line ends if reader is closed
// drop \r if it is the last character
return line[:len(line)-drop], nil
break
}
return nil, err
}
if n != 1 {
return nil, errors.New("failed to read with 1-byte buffer")
if n == 0 {
continue
}
switch c := buffer[0]; c {
case '\r':
drop = 1
line = append(line, c)
case '\n':
// a line ends with \n
return line[:len(line)-drop], nil
default:
drop = 0
line = append(line, c)
c := buffer[0]
if c == '\n' {
break
}
line = append(line, c)
}
return bytes.TrimSuffix(line, []byte{'\r'}), nil
}

0 comments on commit 3f93344

Please sign in to comment.