BurntSushi / ripgrep

ripgrep recursively searches directories for a regex pattern while respecting your gitignore
The Unlicense
47.43k stars 1.96k forks source link

grep-searcher: Advance LineStep without iterating #1618

Open XAMPPRocky opened 4 years ago

XAMPPRocky commented 4 years ago

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);
    }
}
BurntSushi commented 4 years ago

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).