Skip to main content

ferroui_base/media/text_formatting/unicode/
codepoint_enumerator.rs

1use super::codepoint::Codepoint;
2
3/// Enumerates the codepoints of UTF-16 text.
4#[derive(Clone, Debug)]
5pub struct CodepointEnumerator<'a> {
6    text: &'a [u16],
7    offset: usize,
8}
9
10impl<'a> CodepointEnumerator<'a> {
11    #[inline]
12    pub const fn new(text: &'a [u16]) -> Self {
13        Self { text, offset: 0 }
14    }
15
16    /// Moves to the next [`Codepoint`].
17    ///
18    /// Returns `None` at the end of the text (where upstream returns `false`
19    /// and sets the out value to the replacement codepoint).
20    #[inline]
21    pub fn move_next(&mut self) -> Option<Codepoint> {
22        if self.offset >= self.text.len() {
23            return None;
24        }
25
26        let (codepoint, count) = Codepoint::read_at(self.text, self.offset);
27
28        self.offset += count;
29
30        Some(codepoint)
31    }
32}
33
34impl Iterator for CodepointEnumerator<'_> {
35    type Item = Codepoint;
36
37    #[inline]
38    fn next(&mut self) -> Option<Codepoint> {
39        self.move_next()
40    }
41}