patternfinder

package
v0.49.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

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. If no patterns match, it returns nil.
	// The result's Start field will always be 0.
	Match(searchTarget []rune) *PatternMatchResult[T]
}

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 NewTriePatternFinder

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

NewTriePatternFinder creates a new instance of triePatternFinder.

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

Jump to

Keyboard shortcuts

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