fix line count for files that don't end in a newline Signed-off-by: Derek Stevens <nilix@nilfm.cc>
Derek Stevens nilix@nilfm.cc
Wed, 01 Feb 2023 22:08:04 -0700
1 files changed,
8 insertions(+),
0 deletions(-)
jump to
M
routes/template.go
→
routes/template.go
@@ -45,15 +45,23 @@ }
func countLines(r io.Reader) (int, error) { buf := make([]byte, 32*1024) + bufLen := 0 count := 0 nl := []byte{'\n'} for { c, err := r.Read(buf) + if c > 0 { + bufLen += c + } count += bytes.Count(buf[:c], nl) switch { case err == io.EOF: + /* handle last line not having a newline at the end */ + if bufLen >= 1 && buf[bufLen-1] != '\n' { + count++ + } return count, nil case err != nil: return 0, err