Skip to main content

find

Function find 

Source
pub fn find(
    buffer: &Buffer,
    regex: &Regex,
    from: Position,
    direction: Direction,
    cancel: &CancellationToken,
) -> CoreResult<Option<SearchHit>>
Expand description

Find the next match of regex from from in direction, wrapping around the buffer once. Re-exported at the crate root as search_find.

Inclusive of from in both directions (see the module docs): a match starting exactly at from is returned, so a caller implementing vim’s n advances from by one UTF-8 scalar first. None when the buffer is empty or has no match at all. SearchHit::wrapped reports whether the hit was found only after wrapping.

§Errors

CoreError::Cancelled if cancel flips mid-scan; CoreError::Protocol if from is out of bounds (see Buffer::position_to_byte).

§Examples

use fancy_regex::Regex;
use lattice_core::protocol::CancellationToken;
use lattice_core::protocol::position::Position;
use lattice_core::search::{Direction, find};
use lattice_core::Buffer;

let buf = Buffer::from_text("one two one");
let re = Regex::new("one")?;
let never = CancellationToken::never();

// From byte 1, the next "one" is at byte 8 — no wrap needed.
let hit = find(&buf, &re, Position::new(0, 1), Direction::Forward, &never)?;
assert_eq!(hit.map(|h| (h.range.start, h.wrapped)), Some((Position::new(0, 8), false)));

// From byte 9 there is nothing ahead, so the search wraps to byte 0.
let hit = find(&buf, &re, Position::new(0, 9), Direction::Forward, &never)?;
assert_eq!(hit.map(|h| (h.range.start, h.wrapped)), Some((Position::new(0, 0), true)));