From f7b3f083aaf2303553009bcbcbb412200aa726ab Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Mon, 11 Sep 2023 18:44:52 +0100 Subject: [PATCH] Prefer bytes to strings Strings are immutable and hence we need to create a new one all the time when operation on them --- pkg/redact/literal.go | 12 ++++++++---- pkg/redact/multi_line.go | 26 +++++++++++++------------- pkg/redact/redact.go | 6 +++--- pkg/redact/single_line.go | 22 ++++++++++------------ 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/pkg/redact/literal.go b/pkg/redact/literal.go index f507e9ede..4ad7088d8 100644 --- a/pkg/redact/literal.go +++ b/pkg/redact/literal.go @@ -2,9 +2,9 @@ package redact import ( "bufio" + "bytes" "fmt" "io" - "strings" ) type literalRedactor struct { @@ -35,17 +35,21 @@ func (r literalRedactor) Redact(input io.Reader, path string) io.Reader { } }() + // TODO: Convert to bytes at source + mask := []byte(MASK_TEXT) + match := []byte(r.matchString) + reader := bufio.NewReader(input) lineNum := 0 for { lineNum++ - var line string + var line []byte line, err = readLine(reader) if err != nil { return } - clean := strings.ReplaceAll(line, r.matchString, MASK_TEXT) + clean := bytes.ReplaceAll(line, match, mask) // io.WriteString would be nicer, but scanner strips new lines fmt.Fprintf(writer, "%s\n", clean) @@ -53,7 +57,7 @@ func (r literalRedactor) Redact(input io.Reader, path string) io.Reader { return } - if clean != line { + if !bytes.Equal(clean, line) { addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(line) - len(clean), diff --git a/pkg/redact/multi_line.go b/pkg/redact/multi_line.go index 8bd50a4e5..6a4c4eb5d 100644 --- a/pkg/redact/multi_line.go +++ b/pkg/redact/multi_line.go @@ -2,10 +2,10 @@ package redact import ( "bufio" + "bytes" "fmt" "io" "regexp" - "strings" ) type MultiLineRedactor struct { @@ -48,7 +48,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader { writer.CloseWithError(err) }() - substStr := getReplacementPattern(r.re2, r.maskText) + substStr := []byte(getReplacementPattern(r.re2, r.maskText)) reader := bufio.NewReader(input) line1, line2, err := getNextTwoLines(reader, nil) @@ -66,24 +66,24 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader { // is scan is not nil, then check if line1 matches scan by lowercasing it if r.scan != nil { - lowerLine1 := strings.ToLower(line1) - if !r.scan.MatchString(lowerLine1) { + lowerLine1 := bytes.ToLower(line1) + if !r.scan.Match(lowerLine1) { fmt.Fprintf(writer, "%s\n", line1) - line1, line2, err = getNextTwoLines(reader, &line2) + line1, line2, err = getNextTwoLines(reader, line2) flushLastLine = true continue } } // If line1 matches re1, then transform line2 using re2 - if !r.re1.MatchString(line1) { + if !r.re1.Match(line1) { fmt.Fprintf(writer, "%s\n", line1) - line1, line2, err = getNextTwoLines(reader, &line2) + line1, line2, err = getNextTwoLines(reader, line2) flushLastLine = true continue } flushLastLine = false - clean := r.re2.ReplaceAllString(line2, substStr) + clean := r.re2.ReplaceAll(line2, substStr) // io.WriteString would be nicer, but reader strips new lines fmt.Fprintf(writer, "%s\n%s\n", line1, clean) @@ -92,7 +92,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader { } // if clean is not equal to line2, a redaction was performed - if clean != line2 { + if !bytes.Equal(clean, line2) { addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(line2) - len(clean), @@ -112,9 +112,9 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader { return out } -func getNextTwoLines(reader *bufio.Reader, curLine2 *string) (line1 string, line2 string, err error) { - line1 = "" - line2 = "" +func getNextTwoLines(reader *bufio.Reader, curLine2 []byte) (line1 []byte, line2 []byte, err error) { + line1 = []byte{} + line2 = []byte{} if curLine2 == nil { line1, err = readLine(reader) @@ -126,7 +126,7 @@ func getNextTwoLines(reader *bufio.Reader, curLine2 *string) (line1 string, line return } - line1 = *curLine2 + line1 = curLine2 line2, err = readLine(reader) if err != nil { return diff --git a/pkg/redact/redact.go b/pkg/redact/redact.go index 5dcad5b37..bcd173b5a 100644 --- a/pkg/redact/redact.go +++ b/pkg/redact/redact.go @@ -482,13 +482,13 @@ func getReplacementPattern(re *regexp.Regexp, maskText string) string { return substStr } -func readLine(r *bufio.Reader) (string, error) { +func readLine(r *bufio.Reader) ([]byte, error) { var completeLine []byte for { var line []byte line, isPrefix, err := r.ReadLine() if err != nil { - return "", err + return []byte{}, err } completeLine = append(completeLine, line...) @@ -496,7 +496,7 @@ func readLine(r *bufio.Reader) (string, error) { break } } - return string(completeLine), nil + return completeLine, nil } func addRedaction(redaction Redaction) { diff --git a/pkg/redact/single_line.go b/pkg/redact/single_line.go index 7ddf36f1b..fa2c5f67b 100644 --- a/pkg/redact/single_line.go +++ b/pkg/redact/single_line.go @@ -2,12 +2,10 @@ package redact import ( "bufio" + "bytes" "fmt" "io" "regexp" - "strings" - - "github.com/replicatedhq/troubleshoot/pkg/constants" ) type SingleLineRedactor struct { @@ -49,33 +47,33 @@ func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader { } }() - substStr := getReplacementPattern(r.re, r.maskText) + substStr := []byte(getReplacementPattern(r.re, r.maskText)) - buf := make([]byte, constants.MAX_BUFFER_CAPACITY) + buf := make([]byte, 4096) scanner := bufio.NewScanner(input) - scanner.Buffer(buf, constants.MAX_BUFFER_CAPACITY) + scanner.Buffer(buf, 1024*1024) lineNum := 0 for scanner.Scan() { lineNum++ - line := scanner.Text() + line := scanner.Bytes() // is scan is not nil, then check if line matches scan by lowercasing it if r.scan != nil { - lowerLine := strings.ToLower(line) - if !r.scan.MatchString(lowerLine) { + lowerLine := bytes.ToLower(line) + if !r.scan.Match(lowerLine) { fmt.Fprintf(writer, "%s\n", line) continue } } // if scan matches, but re does not, do not redact - if !r.re.MatchString(line) { + if !r.re.Match(line) { fmt.Fprintf(writer, "%s\n", line) continue } - clean := r.re.ReplaceAllString(line, substStr) + clean := r.re.ReplaceAll(line, substStr) // io.WriteString would be nicer, but scanner strips new lines fmt.Fprintf(writer, "%s\n", clean) @@ -85,7 +83,7 @@ func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader { } // if clean is not equal to line, a redaction was performed - if clean != line { + if !bytes.Equal(clean, line) { addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(line) - len(clean),