government / cmd / govlisp / lines.go

@ f98307e213f9f3c11b00c8edc3c367c315b9819a | history


package main

import (
	"bufio"
	"io"
)

// lineReader yields input one line at a time, without the line ending. It
// has no limit on line length, unlike bufio.Scanner's default.
type lineReader struct {
	r *bufio.Reader
}

func newLineReader(in io.Reader) *lineReader {
	return &lineReader{r: bufio.NewReader(in)}
}

func (l *lineReader) next() (string, bool) {
	line, err := l.r.ReadString('\n')
	if err != nil && line == "" {
		return "", false
	}
	if n := len(line); n > 0 && line[n-1] == '\n' {
		line = line[:n-1]
		if n := len(line); n > 0 && line[n-1] == '\r' {
			line = line[:n-1]
		}
	}
	return line, true
}