ripgrep icon indicating copy to clipboard operation
ripgrep copied to clipboard

grep-searcher: Advance LineStep without iterating

Open XAMPPRocky opened this issue 5 years ago • 1 comments

I've been using grep_searcher::LineStep for a new feature in tokei that requires the level of control it provides and it has been great to use so far. The one minor nit I've come across is that is no way to advance LineStep without next, when there are times where I want to be able to skip to a certain part of the file. This is currently possible by creating a new LineStep that starts at that position, but it would be nice if I didn't need to re-assign just to update LineStep's position.

Example

let mut stepper = LineStep::new(b'\n', 0, lines.len());

while let Some((start, end)) = stepper.next(lines) {
    if let Some(position) = /* some parse function */ {
        // Current
        stepper = LineStep::new(b'\n', position, lines.len());
        // Proposed
        stepper.advance_to(position);
    }
}

XAMPPRocky avatar Jun 13 '20 18:06 XAMPPRocky

This seems fine to me. A PR is welcome. The docs for advance_to probably need a bit of detail. e.g., If the position you advance to is in the middle of a line, then the subsequent line reported may be incomplete (missing the part of the line before the position it was advanced to).

BurntSushi avatar Jun 15 '20 11:06 BurntSushi