helix-mirror/helix-core/src/search.rs

40 lines
790 B
Rust
Raw Normal View History

2021-03-10 12:50:46 +04:00
use crate::RopeSlice;
pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
2021-03-10 12:50:46 +04:00
// start searching right after pos
let mut chars = text.chars_at(pos + 1);
2021-03-10 12:50:46 +04:00
for _ in 0..n {
loop {
let c = chars.next()?;
pos += 1;
if c == ch {
break;
2021-03-10 12:50:46 +04:00
}
}
}
Some(pos)
2021-03-10 12:50:46 +04:00
}
pub fn find_nth_prev(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
2021-03-10 12:50:46 +04:00
// start searching right before pos
let mut chars = text.chars_at(pos.saturating_sub(1));
2021-03-10 12:50:46 +04:00
for _ in 0..n {
loop {
let c = chars.prev()?;
pos = pos.saturating_sub(1);
if c == ch {
break;
2021-03-10 12:50:46 +04:00
}
}
}
Some(pos)
2021-03-10 12:50:46 +04:00
}