Commit 3f4d750d authored by astaxie's avatar astaxie

utils: improve the file grep

parent 9f01aeed
...@@ -64,24 +64,30 @@ func GrepFile(patten string, filename string) (lines []string, err error) { ...@@ -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
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment