helix-mods/helix-core/src/search.rs
ahkrr e2d780f993 fix: 2 panics while setting style + off by 1
The panics would occur because set_style 
would draw outside of the the surface. 
Both occured using `find_prev` or `till_prev`
In my case the first panic! would appear
in a terminal with around 80 columns 
in helix/README.md going to the end of the file
with `geglf(`
the second with `geglfX`
The off by one fix ensures that `find_nth_prev` 
starts at the first character to the left
2021-06-07 09:15:08 +09:00

63 lines
1,003 B
Rust

use crate::RopeSlice;
pub fn find_nth_next(
text: RopeSlice,
ch: char,
mut pos: usize,
n: usize,
inclusive: bool,
) -> Option<usize> {
if pos >= text.len_chars() {
return None;
}
// start searching right after pos
let mut chars = text.chars_at(pos + 1);
for _ in 0..n {
loop {
let c = chars.next()?;
pos += 1;
if c == ch {
break;
}
}
}
if !inclusive {
pos -= 1;
}
Some(pos)
}
pub fn find_nth_prev(
text: RopeSlice,
ch: char,
mut pos: usize,
n: usize,
inclusive: bool,
) -> Option<usize> {
// start searching right before pos
let mut chars = text.chars_at(pos);
for _ in 0..n {
loop {
let c = chars.prev()?;
pos = pos.saturating_sub(1);
if c == ch {
break;
}
}
}
if !inclusive {
pos += 1;
}
Some(pos)
}