3f4d750d by astaxie

utils: improve the file grep

1 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) {
64 lines = make([]string, 0) 64 lines = make([]string, 0)
65 reader := bufio.NewReader(fd) 65 reader := bufio.NewReader(fd)
66 prefix := "" 66 prefix := ""
67 isLongLine := false
67 for { 68 for {
68 byteLine, isPrefix, er := reader.ReadLine() 69 byteLine, isPrefix, er := reader.ReadLine()
69 if er != nil && er != io.EOF { 70 if er != nil && er != io.EOF {
70 return nil, er 71 return nil, er
71 } 72 }
73 if er == io.EOF {
74 break
75 }
72 line := string(byteLine) 76 line := string(byteLine)
73 if isPrefix { 77 if isPrefix {
74 prefix += line 78 prefix += line
75 continue 79 continue
80 } else {
81 isLongLine = true
76 } 82 }
77 83
78 line = prefix + line 84 line = prefix + line
85 if isLongLine {
86 prefix = ""
87 }
79 if re.MatchString(line) { 88 if re.MatchString(line) {
80 lines = append(lines, line) 89 lines = append(lines, line)
81 } 90 }
82 if er == io.EOF {
83 break
84 }
85 } 91 }
86 return lines, nil 92 return lines, nil
87 } 93 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!