Skip to main content

Module search

Module search 

Source
Expand description

Buffer-level regex search.

Uses fancy_regex::Regex – a hybrid engine that delegates to the regex crate’s RE2-style DFA for patterns without backreferences/lookarounds and falls back to a bounded NFA only for the patterns that need it. So:

  • Plain literals (/foo) hit memmem’s SIMD prefilter transparently. Same speed as the prior B-β literal-only path.
  • Standard regex (/(foo|bar)+) compiles through the lazy DFA. Linear-time guarantee.
  • Backref patterns (/(\w+)\s+\1/) use the NFA with a configurable recursion limit – catastrophic-backtracking patterns abort cleanly instead of locking the editor.

Replacement-side backrefs (the more common request) are handled by the fancy_regex::Regex::replace_all template syntax ($1, ${name}); see the substitute path in lattice-ui-tui::app.

§Streaming model

The search walks the rope’s chunk iterator. A sliding window: Vec<u8> holds the current chunk plus a MAX_MATCH_LEN-byte tail from the previous chunk so cross-chunk matches are caught in exactly one iteration. Match lengths longer than MAX_MATCH_LEN that span a chunk boundary will be missed – acceptable for v1 (no editor-search workflow needs >8KB matches).

§API shape

Callers compile the fancy_regex::Regex once and pass it by reference. Hlsearch / live-preview consumers call find_all each keystroke; the regex is compiled when the pattern changes, not per call.

Semantics (unchanged from the prior literal engine):

  • Forward: smallest match-start byte >= from. If none, wrap and return the smallest match-start byte in [0, from).
  • Backward: largest match-start byte <= from. If none, wrap and return the largest match-start byte > from.

Both directions are inclusive of from. To skip the match at the cursor (vim’s n after /), the caller advances from by one UTF-8 scalar before calling – not one byte. A byte step lands mid-scalar on non-ASCII text; this module snaps such an offset back to the containing scalar (see floor_char_boundary) rather than panicking, which would leave n re-finding the very match the caller meant to skip. dispatch::step_byte is the in-tree caller that honours this.

Structs§

SearchHit
One match returned by find.

Enums§

Direction
Which way find scans from its from position. Re-exported at the crate root as SearchDir.

Functions§

find
Find the next match of regex from from in direction, wrapping around the buffer once. Re-exported at the crate root as search_find.
find_all
Find every occurrence of the regex in the buffer. Returns the positional ranges in left-to-right order. Empty for matchless patterns and patterns whose minimum length exceeds the buffer.