Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

---

## [0.11.8] - 2026-02-01

### Fixed
- **Critical regression in UseAnchoredLiteral strategy** (Issue #107)
- `FindIndices*` and `findIndicesAtWithState` were missing `UseAnchoredLiteral` case
- Pattern `^/.*[\w-]+\.php$` fell through to slow NFA path: 0.01ms → 408ms (40,000x slower)
- Added `findIndicesAnchoredLiteral` and `findIndicesAnchoredLiteralAt` methods
- Now correctly uses O(1) anchored literal matching: **408ms → 0.5ms**

---

## [0.11.7] - 2026-02-01

### Fixed
Expand Down
24 changes: 24 additions & 0 deletions meta/find_indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (e *Engine) FindIndices(haystack []byte) (start, end int, found bool) {
return e.findIndicesAhoCorasick(haystack)
case UseMultilineReverseSuffix:
return e.findIndicesMultilineReverseSuffix(haystack)
case UseAnchoredLiteral:
return e.findIndicesAnchoredLiteral(haystack)
default:
return e.findIndicesNFA(haystack)
}
Expand Down Expand Up @@ -91,6 +93,8 @@ func (e *Engine) FindIndicesAt(haystack []byte, at int) (start, end int, found b
return e.findIndicesAhoCorasickAt(haystack, at)
case UseMultilineReverseSuffix:
return e.findIndicesMultilineReverseSuffixAt(haystack, at)
case UseAnchoredLiteral:
return e.findIndicesAnchoredLiteralAt(haystack, at)
default:
return e.findIndicesNFAAt(haystack, at)
}
Expand Down Expand Up @@ -452,6 +456,24 @@ func (e *Engine) findIndicesMultilineReverseSuffix(haystack []byte) (int, int, b
return e.multilineReverseSuffixSearcher.FindIndicesAt(haystack, 0)
}

// findIndicesAnchoredLiteral uses O(1) specialized matching for ^prefix.*suffix$ patterns.
// For anchored patterns, match always spans [0, len(haystack)].
func (e *Engine) findIndicesAnchoredLiteral(haystack []byte) (int, int, bool) {
if MatchAnchoredLiteral(haystack, e.anchoredLiteralInfo) {
return 0, len(haystack), true
}
return -1, -1, false
}

// findIndicesAnchoredLiteralAt searches using anchored literal at position - zero alloc.
// Anchored patterns can only match from position 0.
func (e *Engine) findIndicesAnchoredLiteralAt(haystack []byte, at int) (int, int, bool) {
if at > 0 {
return -1, -1, false
}
return e.findIndicesAnchoredLiteral(haystack)
}

// findIndicesMultilineReverseSuffixAt searches using multiline suffix optimization from position - zero alloc.
func (e *Engine) findIndicesMultilineReverseSuffixAt(haystack []byte, at int) (int, int, bool) {
if e.multilineReverseSuffixSearcher == nil {
Expand Down Expand Up @@ -824,6 +846,8 @@ func (e *Engine) findIndicesAtWithState(haystack []byte, at int, state *SearchSt
return e.findIndicesAhoCorasickAt(haystack, at)
case UseMultilineReverseSuffix:
return e.findIndicesMultilineReverseSuffixAt(haystack, at)
case UseAnchoredLiteral:
return e.findIndicesAnchoredLiteralAt(haystack, at)
default:
return e.findIndicesNFAAtWithState(haystack, at, state)
}
Expand Down