| 18 | } |
| 19 | |
| 20 | func (s *RegexpWriter) Write(data []byte) (int, error) { |
| 21 | if len(data) == 0 { |
| 22 | return 0, nil |
| 23 | } |
| 24 | |
| 25 | filtered := []byte{} |
| 26 | repl := []byte(s.repl) |
| 27 | lines := bytes.SplitAfter(data, []byte("\n")) |
| 28 | |
| 29 | if len(s.buf) > 0 { |
| 30 | lines[0] = append(s.buf, lines[0]...) |
| 31 | } |
| 32 | |
| 33 | for i, line := range lines { |
| 34 | if i == len(lines) { |
| 35 | s.buf = line |
| 36 | } else { |
| 37 | f := s.re.ReplaceAll(line, repl) |
| 38 | if len(f) > 0 { |
| 39 | filtered = append(filtered, f...) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if len(filtered) != 0 { |
| 45 | _, err := s.out.Write(filtered) |
| 46 | if err != nil { |
| 47 | return 0, err |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return len(data), nil |
| 52 | } |
| 53 | |
| 54 | func (s *RegexpWriter) Flush() (int, error) { |
| 55 | if len(s.buf) > 0 { |