Skip to content

Commit

Permalink
Prefer bytes to strings
Browse files Browse the repository at this point in the history
Strings are immutable and hence we need to create a new one
all the time when operation on them
  • Loading branch information
banjoh committed Sep 11, 2023
1 parent 930ed8e commit f7b3f08
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
12 changes: 8 additions & 4 deletions pkg/redact/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package redact

import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)

type literalRedactor struct {
Expand Down Expand Up @@ -35,25 +35,29 @@ 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)
if err != nil {
return
}

if clean != line {
if !bytes.Equal(clean, line) {
addRedaction(Redaction{
RedactorName: r.redactName,
CharactersRemoved: len(line) - len(clean),
Expand Down
26 changes: 13 additions & 13 deletions pkg/redact/multi_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package redact

import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strings"
)

type MultiLineRedactor struct {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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),
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/redact/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,21 +482,21 @@ 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...)
if !isPrefix {
break
}
}
return string(completeLine), nil
return completeLine, nil
}

func addRedaction(redaction Redaction) {
Expand Down
22 changes: 10 additions & 12 deletions pkg/redact/single_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package redact

import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strings"

"github.com/replicatedhq/troubleshoot/pkg/constants"
)

type SingleLineRedactor struct {
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand Down

0 comments on commit f7b3f08

Please sign in to comment.