matcher

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package matcher finds which of a fixed set of literals occur in a text, in a single pass.

Rules commonly gate an expensive regular expression behind a set of cheap literal checks: if none of the words a pattern requires appear, the pattern cannot match and need not run. Done naively that is one full scan of the content per literal — and the scans all run to completion precisely in the common case where nothing matches.

An Aho-Corasick automaton answers the whole question in one pass: O(n) in the text length regardless of how many literals are being searched for, after an O(total literal length) build that happens once at package initialisation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

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

Matcher is an Aho-Corasick automaton over a fixed literal set.

A Matcher is immutable once built and safe for concurrent use.

func New

func New(literals []string) *Matcher

New builds an automaton for the given literals.

Literal IDs correspond to positions in the slice. Empty literals are skipped, since they would match everywhere and carry no information.

Matching is byte-exact: callers wanting case-insensitive behaviour should supply lowercase literals and lowercase the text.

func (*Matcher) Count

func (m *Matcher) Count() int

Count returns the number of literals the automaton was built with.

func (*Matcher) Scan

func (m *Matcher) Scan(text string) Set

Scan returns the set of literals occurring in the text.

Runs in O(len(text)) with one array index per byte, independent of how many literals the automaton holds.

type Set

type Set []bool

Set is the result of a scan: membership by literal ID.

func (Set) Has

func (s Set) Has(id int) bool

Has reports whether the literal with the given ID was found.

func (Set) HasAny

func (s Set) HasAny(ids []int) bool

HasAny reports whether any of the given literal IDs was found.

Jump to

Keyboard shortcuts

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