2021-03-10 09:50:46 +01:00
|
|
|
use crate::RopeSlice;
|
|
|
|
|
2021-03-11 08:14:52 +01:00
|
|
|
pub fn find_nth_next(
|
|
|
|
text: RopeSlice,
|
|
|
|
ch: char,
|
|
|
|
mut pos: usize,
|
|
|
|
n: usize,
|
|
|
|
inclusive: bool,
|
|
|
|
) -> Option<usize> {
|
2021-06-02 06:14:55 +02:00
|
|
|
if pos >= text.len_chars() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-10 09:50:46 +01:00
|
|
|
// start searching right after pos
|
2021-03-11 02:44:38 +01:00
|
|
|
let mut chars = text.chars_at(pos + 1);
|
2021-03-10 09:50:46 +01:00
|
|
|
|
|
|
|
for _ in 0..n {
|
|
|
|
loop {
|
2021-03-11 02:44:38 +01:00
|
|
|
let c = chars.next()?;
|
|
|
|
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
if c == ch {
|
|
|
|
break;
|
2021-03-10 09:50:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 02:44:38 +01:00
|
|
|
|
2021-03-11 08:14:52 +01:00
|
|
|
if !inclusive {
|
|
|
|
pos -= 1;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:44:38 +01:00
|
|
|
Some(pos)
|
2021-03-10 09:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 08:14:52 +01:00
|
|
|
pub fn find_nth_prev(
|
|
|
|
text: RopeSlice,
|
|
|
|
ch: char,
|
|
|
|
mut pos: usize,
|
|
|
|
n: usize,
|
|
|
|
inclusive: bool,
|
|
|
|
) -> Option<usize> {
|
2021-03-10 09:50:46 +01:00
|
|
|
// start searching right before pos
|
2021-03-11 02:44:38 +01:00
|
|
|
let mut chars = text.chars_at(pos.saturating_sub(1));
|
2021-03-10 09:50:46 +01:00
|
|
|
|
|
|
|
for _ in 0..n {
|
|
|
|
loop {
|
2021-03-11 02:44:38 +01:00
|
|
|
let c = chars.prev()?;
|
|
|
|
|
|
|
|
pos = pos.saturating_sub(1);
|
|
|
|
|
|
|
|
if c == ch {
|
|
|
|
break;
|
2021-03-10 09:50:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 02:44:38 +01:00
|
|
|
|
2021-03-11 08:14:52 +01:00
|
|
|
if !inclusive {
|
|
|
|
pos -= 1;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:44:38 +01:00
|
|
|
Some(pos)
|
2021-03-10 09:50:46 +01:00
|
|
|
}
|