remove: countlines from textreader

This commit is contained in:
Inanc Gumus
2019-08-08 17:19:46 +03:00
parent 43d25a4180
commit 5d5c656413
2 changed files with 24 additions and 33 deletions

View File

@@ -13,6 +13,7 @@ import (
"errors"
"fmt"
"io"
"os"
)
// this could be made faster.
@@ -106,3 +107,23 @@ func atoi(input []byte) (int, error) {
}
return val, nil
}
func countLines(r io.Reader) (int, error) {
var (
lines int
buf = make([]byte, os.Getpagesize()) // read via 16 KB blocks
)
for {
n, err := r.Read(buf)
lines += bytes.Count(buf[:n], []byte{'\n'})
if err == io.EOF {
return lines, nil
}
if err != nil {
return lines, err
}
}
}