@@ -2,10 +2,9 @@ package redact
22
33import (
44 "bufio"
5- "fmt "
5+ "bytes "
66 "io"
77 "regexp"
8- "strings"
98)
109
1110type MultiLineRedactor struct {
@@ -20,19 +19,19 @@ type MultiLineRedactor struct {
2019
2120func NewMultiLineRedactor (re1 LineRedactor , re2 string , maskText , path , name string , isDefault bool ) (* MultiLineRedactor , error ) {
2221 var scanCompiled * regexp.Regexp
23- compiled1 , err := regexp . Compile (re1 .regex )
22+ compiled1 , err := compileRegex (re1 .regex )
2423 if err != nil {
2524 return nil , err
2625 }
2726
2827 if re1 .scan != "" {
29- scanCompiled , err = regexp . Compile (re1 .scan )
28+ scanCompiled , err = compileRegex (re1 .scan )
3029 if err != nil {
3130 return nil , err
3231 }
3332 }
3433
35- compiled2 , err := regexp . Compile (re2 )
34+ compiled2 , err := compileRegex (re2 )
3635 if err != nil {
3736 return nil , err
3837 }
@@ -48,14 +47,18 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
4847 writer .CloseWithError (err )
4948 }()
5049
51- substStr := getReplacementPattern (r .re2 , r .maskText )
50+ substStr := [] byte ( getReplacementPattern (r .re2 , r .maskText ) )
5251
5352 reader := bufio .NewReader (input )
5453 line1 , line2 , err := getNextTwoLines (reader , nil )
5554 if err != nil {
5655 // this will print 2 blank lines for empty input...
57- fmt .Fprintf (writer , "%s\n " , line1 )
58- fmt .Fprintf (writer , "%s\n " , line2 )
56+ // Append newlines since scanner strips them
57+ err = writeBytes (writer , line1 , NEW_LINE , line2 , NEW_LINE )
58+ if err != nil {
59+ return
60+ }
61+
5962 return
6063 }
6164
@@ -66,33 +69,41 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
6669
6770 // is scan is not nil, then check if line1 matches scan by lowercasing it
6871 if r .scan != nil {
69- lowerLine1 := strings .ToLower (line1 )
70- if ! r .scan .MatchString (lowerLine1 ) {
71- fmt .Fprintf (writer , "%s\n " , line1 )
72- line1 , line2 , err = getNextTwoLines (reader , & line2 )
72+ lowerLine1 := bytes .ToLower (line1 )
73+ if ! r .scan .Match (lowerLine1 ) {
74+ // Append newline since scanner strips it
75+ err = writeBytes (writer , line1 , NEW_LINE )
76+ if err != nil {
77+ return
78+ }
79+ line1 , line2 , err = getNextTwoLines (reader , line2 )
7380 flushLastLine = true
7481 continue
7582 }
7683 }
7784
7885 // If line1 matches re1, then transform line2 using re2
79- if ! r .re1 .MatchString (line1 ) {
80- fmt .Fprintf (writer , "%s\n " , line1 )
81- line1 , line2 , err = getNextTwoLines (reader , & line2 )
86+ if ! r .re1 .Match (line1 ) {
87+ // Append newline since scanner strips it
88+ err = writeBytes (writer , line1 , NEW_LINE )
89+ if err != nil {
90+ return
91+ }
92+ line1 , line2 , err = getNextTwoLines (reader , line2 )
8293 flushLastLine = true
8394 continue
8495 }
8596 flushLastLine = false
86- clean := r .re2 .ReplaceAllString (line2 , substStr )
97+ clean := r .re2 .ReplaceAll (line2 , substStr )
8798
88- // io.WriteString would be nicer, but reader strips new lines
89- fmt . Fprintf (writer , "%s \n %s \n " , line1 , clean )
99+ // Append newlines since scanner strips them
100+ err = writeBytes (writer , line1 , NEW_LINE , clean , NEW_LINE )
90101 if err != nil {
91102 return
92103 }
93104
94105 // if clean is not equal to line2, a redaction was performed
95- if clean != line2 {
106+ if ! bytes . Equal ( clean , line2 ) {
96107 addRedaction (Redaction {
97108 RedactorName : r .redactName ,
98109 CharactersRemoved : len (line2 ) - len (clean ),
@@ -106,15 +117,18 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
106117 }
107118
108119 if flushLastLine {
109- fmt .Fprintf (writer , "%s\n " , line1 )
120+ // Append newline since scanner strip it
121+ err = writeBytes (writer , line1 , NEW_LINE )
122+ if err != nil {
123+ return
124+ }
110125 }
111126 }()
112127 return out
113128}
114129
115- func getNextTwoLines (reader * bufio.Reader , curLine2 * string ) (line1 string , line2 string , err error ) {
116- line1 = ""
117- line2 = ""
130+ func getNextTwoLines (reader * bufio.Reader , curLine2 []byte ) (line1 []byte , line2 []byte , err error ) {
131+ line2 = []byte {}
118132
119133 if curLine2 == nil {
120134 line1 , err = readLine (reader )
@@ -126,11 +140,23 @@ func getNextTwoLines(reader *bufio.Reader, curLine2 *string) (line1 string, line
126140 return
127141 }
128142
129- line1 = * curLine2
143+ line1 = curLine2
130144 line2 , err = readLine (reader )
131145 if err != nil {
132146 return
133147 }
134148
135149 return
136150}
151+
152+ // writeBytes writes all byte slices to the writer
153+ // in the order they are passed in the variadic argument
154+ func writeBytes (w io.Writer , bs ... []byte ) error {
155+ for _ , b := range bs {
156+ _ , err := w .Write (b )
157+ if err != nil {
158+ return err
159+ }
160+ }
161+ return nil
162+ }
0 commit comments