replacer

package
v3.63.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package replacer provides an efficient configurable string replacer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mux

type Mux []*Replacer

Mux contains multiple replacers

func (Mux) Flush

func (mux Mux) Flush() error

Flush flushes all replacers.

func (Mux) Reset

func (mux Mux) Reset(needles []string)

Reset resets all replacers with new needles (secrets).

type Replacer

type Replacer struct {
	// contains filtered or unexported fields
}

Replacer is a straightforward streaming string replacer and replacer, suitable for detecting or redacting secrets in a stream.

The algorithm is intended to be easier to maintain than certain high-performance multi-string search algorithms, and also geared towards ensuring secrets don't escape (for instance, by matching overlaps), at the expense of ultimate efficiency.

func New

func New(dst io.Writer, needles []string, replacement func([]byte) []byte) *Replacer

New returns a new Replacer.

dst is the writer to which output is forwarded. needles is the list of strings to search for.

replacement is called when one or more _overlapping_ needles are found. Non-overlapping matches (including adjacent matches) cause more callbacks. replacement is given the subslice of the internal buffer that matched one or more overlapping needles. The return value from replacement is used as a replacement for the range it was given. To forward the stream unaltered, simply return the argument. replacement can also scribble over the contents of the slice it gets (and return it), or return an entirely different slice of bytes to use for replacing the original in the forwarded stream. Because the callback semantics are "zero copy", replacement should _not_ keep a reference to the argument after it returns, since that will prevent garbage-collecting old buffers. replacement should also avoid calling append on its input, or otherwise extend the slice, as this can overwrite more of the buffer than intended.

func (*Replacer) Flush

func (r *Replacer) Flush() error

Flush writes all buffered data to the destination. It assumes there is no more data in the stream, and so any incomplete matches are non-matches.

func (*Replacer) Reset

func (r *Replacer) Reset(needles []string)

Reset replaces the secrets to redact with a new set of secrets. It is not necessary to Flush beforehand, but:

  • any previous secrets which have begun matching will continue matching (until they reach a terminal state), and
  • any new secrets will not be compared against existing buffer content, only data passed to Write calls after Reset.

func (*Replacer) Write

func (r *Replacer) Write(b []byte) (int, error)

Write searches the stream for needles (strings, secrets, ...), calls the replacement callback to obtain any replacements, and forwards the output to the destination writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL