helix/helix-core/src/search.rs

66 lines
967 B
Rust
Raw Normal View History

2021-03-10 17:50:46 +09:00
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() || n == 0 {
return None;
}
let mut chars = text.chars_at(pos);
2021-03-10 17:50:46 +09:00
for _ in 0..n {
loop {
let c = chars.next()?;
pos += 1;
if c == ch {
break;
2021-03-10 17:50:46 +09:00
}
}
}
if !inclusive {
pos -= 1;
}
Some(pos)
2021-03-10 17:50:46 +09:00
}
pub fn find_nth_prev(
text: RopeSlice,
ch: char,
mut pos: usize,
n: usize,
inclusive: bool,
) -> Option<usize> {
if pos == 0 || n == 0 {
return None;
}
let mut chars = text.chars_at(pos);
2021-03-10 17:50:46 +09:00
for _ in 0..n {
loop {
let c = chars.prev()?;
pos -= 1;
if c == ch {
break;
2021-03-10 17:50:46 +09:00
}
}
}
if !inclusive {
pos += 1;
}
Some(pos)
2021-03-10 17:50:46 +09:00
}