utils: improve the file grep

This commit is contained in:
astaxie 2014-05-20 14:32:06 +08:00
parent 9f01aeed31
commit 3f4d750dc4
1 changed files with 9 additions and 3 deletions

View File

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