patternfinder

package
v0.60.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPatternAlreadyExists is returned when trying to add a pattern that already exists.
	ErrPatternAlreadyExists = errors.New("pattern already exists")
	// ErrPatternNotFound is returned when a specified pattern cannot be found.
	ErrPatternNotFound = errors.New("pattern not found")
)

Functions

This section is empty.

Types

type PatternFinder

type PatternFinder[T any] interface {
	// AddPattern adds a pattern to the finder.
	// It returns ErrPatternAlreadyExists if the pattern has already been added.
	AddPattern(pattern string, outcome T) error
	GetPattern(pattern string) (T, error)
	DeletePattern(pattern string) (T, error)

	// Match checks if any registered pattern is a prefix of the searchTarget.
	// It returns the longest valid match and true. If no patterns match, it returns false.
	// The result's Start field will always be 0.
	Match(searchTarget string) (PatternMatchResult[T], bool)
}

PatternFinder provides a way to find the longest registered pattern that is a prefix of a given text.

func NewNaivePatternFinder

func NewNaivePatternFinder[T any]() PatternFinder[T]

NewNaivePatternFinder creates a new instance of naivePatternFinder.

func NewRadixPatternFinder added in v0.58.2

func NewRadixPatternFinder[T any]() PatternFinder[T]

NewRadixPatternFinder creates a new instance of radixPatternFinder backed by a Radix Tree.

type PatternMatchResult

type PatternMatchResult[T any] struct {
	Value T
	Start int
	End   int
}

PatternMatchResult represents a single match found within a larger text. It includes the start and end positions of the match.

func FindAllHexTokens added in v0.60.1

func FindAllHexTokens[T any](searchText string, finder PatternFinder[T]) []PatternMatchResult[T]

FindAllHexTokens finds all occurrences of registered patterns that form a whole lowercase hexadecimal token within searchText.

Container IDs and pod sandbox IDs are lowercase hexadecimal strings, so a genuine occurrence is always delimited by bytes that are not hexadecimal digits, whatever punctuation the emitting component uses, such as `ContainerID:<id>`, `"<id>"`, or `/containers/<id>/`. Requiring that boundary on both ends finds IDs without enumerating delimiters, and keeps a registered ID from matching inside a longer digest that merely shares its prefix.

Returns non-overlapping matches ordered by their position in searchText.

func FindAllWithStarterRunes

func FindAllWithStarterRunes[T any](searchText string, finder PatternFinder[T], includeFirst bool, starterRunes ...rune) []PatternMatchResult[T]

FindAllWithStarterRunes finds all occurrences of patterns within a search text. The search for a pattern only begins after encountering one of the specified starterRunes.

Parameters:

  • searchText: The string to search within.
  • finder: The PatternFinder implementation to use for matching prefixes.
  • includeFirst: If true, a match is attempted from the very beginning of the searchText, without waiting for a starterRune. Useful for cases where the entire string itself could be a valid pattern.
  • starterRunes: A set of runes that act as triggers. When one of these runes is encountered, a pattern search is attempted on the text immediately following the rune.

Returns:

A slice of PatternMatchResult for every non-overlapping match found.

func (*PatternMatchResult[T]) GetMatchedString added in v0.50.0

func (p *PatternMatchResult[T]) GetMatchedString(original string) (string, error)

GetMatchedString extracts the matched string from the original string.

Jump to

Keyboard shortcuts

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