Merge pull request #455 from gnever/191229_gfile_add_readline_mst

Add ReadLines and ReadByteLines read file content line by line
This commit is contained in:
John Guo 2020-01-06 17:52:41 +08:00 committed by GitHub
commit 167d58490b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 0 deletions

View File

@ -7,9 +7,12 @@
package gfile
import (
"bufio"
"io"
"io/ioutil"
"os"
"github.com/gogf/gf/util/gconv"
)
var (
@ -160,3 +163,35 @@ func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte {
}
return nil
}
// ReadLines reads file content line by line, which is passed to the callback function <callback> as string.
// It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.
//
// Note that the parameter passed to callback function might be an empty value, and the last non-empty line
// will be passed to callback function <callback> even if it has no newline marker.
func ReadLines(file string, callback func(text string)) error {
cb := func(bytes []byte) {
callback(gconv.UnsafeBytesToStr(bytes))
}
return ReadByteLines(file, cb)
}
// ReadByteLines reads file content line by line, which is passed to the callback function <callback> as []byte.
// It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.
//
// Note that the parameter passed to callback function might be an empty value, and the last non-empty line
// will be passed to callback function <callback> even if it has no newline marker.
func ReadByteLines(file string, callback func(bytes []byte)) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
callback(scanner.Bytes())
}
return nil
}

View File

@ -0,0 +1,59 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gfile_test
import (
"github.com/gogf/gf/debug/gdebug"
"testing"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/test/gtest"
)
func Test_NotFound(t *testing.T) {
gtest.Case(t, func() {
teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/error.log"
callback := func(line string) {
}
err := gfile.ReadLines(teatFile, callback)
gtest.AssertNE(err, nil)
})
}
func Test_ReadLines(t *testing.T) {
gtest.Case(t, func() {
expectList := []string{"a", "b", "c", "d", "e"}
getList := make([]string, 0)
callback := func(line string) {
getList = append(getList, line)
}
teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log"
err := gfile.ReadLines(teatFile, callback)
gtest.AssertEQ(getList, expectList)
gtest.AssertEQ(err, nil)
})
}
func Test_ReadByteLines(t *testing.T) {
gtest.Case(t, func() {
expectList := [][]byte{[]byte("a"), []byte("b"), []byte("c"), []byte("d"), []byte("e")}
getList := make([][]byte, 0)
callback := func(line []byte) {
getList = append(getList, line)
}
teatFile := gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/readline/file.log"
err := gfile.ReadByteLines(teatFile, callback)
gtest.AssertEQ(getList, expectList)
gtest.AssertEQ(err, nil)
})
}

5
os/gfile/testdata/readline/file.log vendored Normal file
View File

@ -0,0 +1,5 @@
a
b
c
d
e