From 3f4d750dc47f8288fb40901f33e498befe1da8d1 Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 20 May 2014 14:32:06 +0800 Subject: [PATCH] utils: improve the file grep --- utils/file.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/file.go b/utils/file.go index 0f08045b..5246d88c 100644 --- a/utils/file.go +++ b/utils/file.go @@ -64,24 +64,30 @@ func GrepFile(patten string, filename string) (lines []string, err error) { lines = make([]string, 0) reader := bufio.NewReader(fd) prefix := "" + isLongLine := false for { byteLine, isPrefix, er := reader.ReadLine() if er != nil && er != io.EOF { return nil, er } + if er == io.EOF { + break + } line := string(byteLine) if isPrefix { prefix += line continue + } else { + isLongLine = true } line = prefix + line + if isLongLine { + prefix = "" + } if re.MatchString(line) { lines = append(lines, line) } - if er == io.EOF { - break - } } return lines, nil }