1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:41:29 +00:00

utils: improve the file grep

This commit is contained in:
astaxie 2014-05-20 14:32:06 +08:00
parent 9f01aeed31
commit 3f4d750dc4

View File

@ -64,24 +64,30 @@ func GrepFile(patten string, filename string) (lines []string, err error) {
lines = make([]string, 0) lines = make([]string, 0)
reader := bufio.NewReader(fd) reader := bufio.NewReader(fd)
prefix := "" prefix := ""
isLongLine := false
for { for {
byteLine, isPrefix, er := reader.ReadLine() byteLine, isPrefix, er := reader.ReadLine()
if er != nil && er != io.EOF { if er != nil && er != io.EOF {
return nil, er return nil, er
} }
if er == io.EOF {
break
}
line := string(byteLine) line := string(byteLine)
if isPrefix { if isPrefix {
prefix += line prefix += line
continue continue
} else {
isLongLine = true
} }
line = prefix + line line = prefix + line
if isLongLine {
prefix = ""
}
if re.MatchString(line) { if re.MatchString(line) {
lines = append(lines, line) lines = append(lines, line)
} }
if er == io.EOF {
break
}
} }
return lines, nil return lines, nil
} }