Skip to content

Commit

Permalink
Remove scanner in favour of readline
Browse files Browse the repository at this point in the history
  • Loading branch information
banjoh committed Sep 13, 2023
1 parent a53acb4 commit 202acc1
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions pkg/redact/single_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ func NewSingleLineRedactor(re LineRedactor, maskText, path, name string, isDefau
return &SingleLineRedactor{scan: scanCompiled, re: compiled, maskText: maskText, filePath: path, redactName: name, isDefault: isDefault}, nil
}

const (
// This is the initial size of the buffer allocated.
// Under the hood, an array of size N is allocated in memory
BUF_INIT_SIZE = 4096 // 4KB

// This is the muximum size the buffer can grow to
SCANNER_MAX_SIZE = 1024 * 1024 // 1MB
)

func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader {
out, writer := io.Pipe()

Expand All @@ -58,14 +49,15 @@ func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader {

substStr := []byte(getReplacementPattern(r.re, r.maskText))

buf := make([]byte, BUF_INIT_SIZE)
scanner := bufio.NewScanner(input)
scanner.Buffer(buf, SCANNER_MAX_SIZE)

reader := bufio.NewReader(input)
lineNum := 0
for scanner.Scan() {
for {
lineNum++
line := scanner.Bytes()
var line []byte
line, err = readLine(reader)
if err != nil {
return
}

// is scan is not nil, then check if line matches scan by lowercasing it
if r.scan != nil {
Expand Down Expand Up @@ -102,9 +94,6 @@ func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader {
})
}
}
if scanErr := scanner.Err(); scanErr != nil {
err = scanErr
}
}()
return out
}

0 comments on commit 202acc1

Please sign in to comment.