Documentation
¶
Overview ¶
Package gofind provides highly optimized functions for prefix matching strings against patterns containing wildcards. It features a dual-stream architecture that delivers maximum performance for both ASCII-only and Unicode scenarios.
Performance Architecture: ¶
The package uses two specialized implementations:
- ASCII-only stream: Zero UTF-8 overhead, direct byte operations (2-5x faster)
- Unicode stream: Full UTF-8 support with case-insensitive matching
Core Functions: ¶
- Match: ASCII-optimized case-sensitive prefix matching
- MatchFold: Unicode-aware case-insensitive prefix matching
The functions automatically route to the appropriate implementation for optimal performance.
Supported Wildcards: ¶
- `*`: Matches any sequence of characters (including zero characters)
- `?`: Matches zero or one character (any character)
- `.`: Matches any single character except newline
- `[abc]`: Matches any character in the set (a, b, or c)
- `[!abc]` or `[^abc]`: Matches any character not in the set
- `[a-z]`: Matches any character in the range a to z
- `\*`, `\?`, `\.`, `\[`: Matches the literal character
Type Support: ¶
The package supports two input types through Go generics:
- `string`: Automatic routing to ASCII or Unicode implementation
- `[]byte`: Zero-allocation matching for binary data and performance-critical code
Character Classes: ¶
Character classes ([abc], [a-z], [!xyz]) are always case-sensitive, even when using MatchFold. This maintains compatibility with standard glob behavior.
Performance Guidance: ¶
- Use Match() for ASCII-only patterns when maximum speed is needed - Use MatchFold() for Unicode patterns or when case-insensitive matching is required - ASCII-only matching provides 2-5x performance improvement over Unicode-aware matching
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Match ¶
Match returns the position where the pattern was fully consumed from the input data using case-sensitive comparison with prefix matching behavior. It supports two types:
- string: Optimized ASCII-only string matching for maximum performance
- []byte: Zero-allocation matching for byte slices
Return values:
- (position, nil): Position where the pattern was consumed (prefix match successful)
- (0, ErrBadPattern): Pattern is malformed (syntax error)
- (0, ErrNoMatch): Pattern does not match the input
This function performs prefix matching, meaning it succeeds if the beginning of the input matches the pattern, even if there are extra characters at the end.
This function uses an optimized ASCII-only algorithm for maximum performance. For Unicode support, use MatchFold instead.
Examples:
Match("hello", "hello world") // Returns (5, nil) - prefix match
Match([]byte("GET "), []byte("GET /api")) // Returns (4, nil) - prefix match
Match("file?.txt", "file.txt") // Returns (8, nil) - ? matches zero characters
Match("file?.txt", "fileX.txt") // Returns (9, nil) - ? matches one character
Match("a*c", "axcde") // Returns (3, nil) - prefix match successful
func MatchFold ¶
MatchFold returns the position where the pattern was fully consumed from the input data using case-insensitive comparison with Unicode folding and prefix matching behavior. It supports two types:
- string: UTF-8 aware case-insensitive matching with Unicode folding
- []byte: Zero-allocation case-insensitive matching for byte slices
Return values:
- (position, nil): Position where the pattern was consumed (prefix match successful)
- (0, ErrBadPattern): Pattern is malformed (syntax error)
- (0, ErrNoMatch): Pattern does not match the input
This function performs prefix matching, meaning it succeeds if the beginning of the input matches the pattern, even if there are extra characters at the end.
The function uses the Unicode-aware matching algorithm with case folding. Character classes remain case-sensitive to maintain standard glob behavior.
Examples:
MatchFold("HELLO", "hello world") // Returns (5, nil) - case-insensitive prefix match
MatchFold("CAFÉ", "café au lait") // Returns (4, nil) - Unicode case-insensitive prefix
MatchFold("FILE?.TXT", "file.txt") // Returns (8, nil) - ? matches zero characters
MatchFold("FILE?.TXT", "fileX.txt") // Returns (9, nil) - ? matches one character
Types ¶
type MatchResult ¶
MatchResult holds the result of a pattern match operation.
func MatchFoldMultiple ¶
func MatchFoldMultiple[S ~string | ~[]byte](patterns []S, s S) []MatchResult
MatchFoldMultiple concurrently matches a single input against multiple patterns(case-insensitive). It returns a slice of MatchResult structs where each element corresponds to the pattern at the same index.
The order of results corresponds to the order of input patterns.
Example:
patterns := []string{"Foo*", "foo*", "baz[0-9]"}
matches := MatchFoldMultiple(patterns, "foobar")
// matches[0] = {Position: 6, Err: nil} (first pattern matches fully)
// matches[1] = {Position: 6, Err: nil} (second pattern matches fully)
// matches[2] = {Position: 0, Err: ErrNoMatch} (third pattern doesn't match)
func MatchMultiple ¶
func MatchMultiple[S ~string | ~[]byte](patterns []S, s S) []MatchResult