-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathwriter.go
More file actions
45 lines (38 loc) · 809 Bytes
/
writer.go
File metadata and controls
45 lines (38 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package goaway
import "io"
func NewWriter(base io.Writer, detector *ProfanityDetector) *Writer {
return &Writer{
base: base,
detector: detector,
}
}
type Writer struct {
base io.Writer
detector *ProfanityDetector
buf []byte
}
func (w *Writer) Write(payload []byte) (int, error) {
last := 0
for i, char := range payload {
if char != byte('\n') {
continue
}
result := append(w.buf, payload[last:i+1]...)
_, err := w.base.Write([]byte(w.detector.Censor(string(result))))
if err != nil {
return 0, err
}
w.buf = w.buf[:0]
last = i + 1
}
w.buf = payload[last:]
return len(payload), nil
}
func (w *Writer) Flush() error {
if len(w.buf) == 0 {
return nil
}
_, err := w.base.Write([]byte(w.detector.Censor(string(w.buf))))
w.buf = w.buf[:0]
return err
}