1
Fork 0
mirror of https://github.com/RGBCube/cstree synced 2025-08-01 11:37:46 +00:00

text: add SyntaxText::rfind_char

This commit is contained in:
RGBCube 2025-07-29 18:59:54 +03:00
parent a8ebf29808
commit c35e88c3da
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M

View file

@ -91,6 +91,18 @@ impl<'n, 'i, I: Resolver<TokenKey> + ?Sized, S: Syntax, D> SyntaxText<'n, 'i, I,
found(res)
}
/// If `self.contains_char(c)`, returns `Some(pos)`, where `pos` is the byte position of the
/// last appearance of `c`. Otherwise, returns `None`.
pub fn rfind_char(&self, c: char) -> Option<TextSize> {
let mut acc: TextSize = 0.into();
let mut res = None;
self.for_each_chunk(|chunk| {
res = chunk.rfind(c).map(|pos| acc + TextSize::from(pos as u32)).or(res);
acc += TextSize::of(chunk);
});
res
}
/// If `offset < self.len()`, returns `Some(c)`, where `c` is the first `char` at or after
/// `offset` (in bytes). Otherwise, returns `None`.
pub fn char_at(&self, offset: TextSize) -> Option<char> {