1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This package implements a Reader which handles reading \r and \r\n
14 // Reader reads lines from an io.Reader (which may use either '\n' or
23 func NewReader(in io.Reader, maxLineLength int) *Reader {
25 buf: make([]byte, 0, maxLineLength),
31 // ReadLine tries to return a single line, not including the end-of-line bytes.
32 // If the line was found to be longer than the maximum length then isPrefix is
33 // set and the beginning of the line is returned. The rest of the line will be
34 // returned from future calls. isPrefix will be false when returning the last
35 // fragment of the line. The returned buffer points into the internal state of
36 // the Reader and is only valid until the next call to ReadLine. ReadLine
37 // either returns a non-nil line or it returns an error, never both.
38 func (l *Reader) ReadLine() (line []byte, isPrefix bool, err os.Error) {
40 n := copy(l.buf, l.buf[l.consumed:])
45 if len(l.buf) == 0 && l.err != nil {
54 for ; i < len(l.buf); i++ {
55 if l.buf[i] == '\r' && len(l.buf) > i+1 && l.buf[i+1] == '\n' {
59 } else if l.buf[i] == '\n' {
79 // We don't want to rescan the input that we just scanned.
80 // However, we need to back up one byte because the last byte
81 // could have been a '\r' and we do need to rescan that.
87 l.buf = l.buf[:cap(l.buf)]
88 n, readErr := l.in.Read(l.buf[oldLen:])
89 l.buf = l.buf[:oldLen+n]