replacer

package
v3.87.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 5 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 struct {
	// contains filtered or unexported fields
}

Mux contains multiple replacers

func NewMux added in v3.66.0

func NewMux(rs ...*Replacer) *Mux

NewMux returns a new mux with the given replacers.

func (*Mux) Add added in v3.66.0

func (m *Mux) Add(needles ...string)

Add adds needles to all replacers.

func (*Mux) Append added in v3.66.0

func (m *Mux) Append(r *Replacer)

Append adds a replacer to the Mux.

func (*Mux) Flush

func (m *Mux) Flush() error

Flush flushes all replacers.

func (*Mux) Reset

func (m *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 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 strings 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) Add added in v3.66.0

func (r *Replacer) Add(needles ...string)

Add adds more needles to be matched by the replacer. It is not necessary to Flush beforehand, but:

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

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) Needles added in v3.66.0

func (r *Replacer) Needles() []string

Needle returns the current needles

func (*Replacer) Reset

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

Reset removes all current needes and sets new set of needles. It is not necessary to Flush beforehand, but:

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

func (*Replacer) Size added in v3.66.0

func (r *Replacer) Size() int

Size returns the number of needles

func (*Replacer) Write

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

Write searches the stream for needles (e.g. 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