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
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 ¶
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
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 ¶
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 ¶
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.