2021-03-10 12:50:46 +04:00
|
|
|
use crate::RopeSlice;
|
|
|
|
|
2021-07-24 18:44:11 +04:00
|
|
|
pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
|
2021-07-09 03:45:19 +04:00
|
|
|
if pos >= text.len_chars() || n == 0 {
|
2021-06-02 08:14:55 +04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-07-09 03:45:19 +04:00
|
|
|
let mut chars = text.chars_at(pos);
|
2021-03-10 12:50:46 +04:00
|
|
|
|
|
|
|
for _ in 0..n {
|
|
|
|
loop {
|
2021-03-11 05:44:38 +04:00
|
|
|
let c = chars.next()?;
|
|
|
|
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
if c == ch {
|
|
|
|
break;
|
2021-03-10 12:50:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 05:44:38 +04:00
|
|
|
|
2021-07-24 18:44:11 +04:00
|
|
|
Some(pos - 1)
|
2021-03-10 12:50:46 +04:00
|
|
|
}
|
|
|
|
|
2021-07-24 18:44:11 +04:00
|
|
|
pub fn find_nth_prev(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
|
2021-07-09 03:45:19 +04:00
|
|
|
if pos == 0 || n == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-06-05 18:25:45 +04:00
|
|
|
let mut chars = text.chars_at(pos);
|
2021-03-10 12:50:46 +04:00
|
|
|
|
|
|
|
for _ in 0..n {
|
|
|
|
loop {
|
2021-03-11 05:44:38 +04:00
|
|
|
let c = chars.prev()?;
|
|
|
|
|
2021-07-09 03:45:19 +04:00
|
|
|
pos -= 1;
|
2021-03-11 05:44:38 +04:00
|
|
|
|
|
|
|
if c == ch {
|
|
|
|
break;
|
2021-03-10 12:50:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 05:44:38 +04:00
|
|
|
|
|
|
|
Some(pos)
|
2021-03-10 12:50:46 +04:00
|
|
|
}
|