government / cmd / govlisp / lines.go

@ 0a6d0b45c9b0e3f50876896c1c597c24311ceb7b | 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
}