cosmic_text/
bidi_para.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use unicode_bidi::{bidi_class, BidiClass, BidiInfo, ParagraphInfo};
4
5/// An iterator over the paragraphs in the input text.
6/// It is equivalent to [`core::str::Lines`] but follows `unicode-bidi` behaviour.
7#[derive(Debug)]
8pub struct BidiParagraphs<'text> {
9    text: &'text str,
10    info: alloc::vec::IntoIter<ParagraphInfo>,
11}
12
13impl<'text> BidiParagraphs<'text> {
14    /// Create an iterator to split the input text into paragraphs
15    /// in accordance with `unicode-bidi` behaviour.
16    pub fn new(text: &'text str) -> Self {
17        let info = BidiInfo::new(text, None);
18        let info = info.paragraphs.into_iter();
19        Self { text, info }
20    }
21}
22
23impl<'text> Iterator for BidiParagraphs<'text> {
24    type Item = &'text str;
25
26    fn next(&mut self) -> Option<Self::Item> {
27        let para = self.info.next()?;
28        let paragraph = &self.text[para.range];
29        // `para.range` includes the newline that splits the line, so remove it if present
30        let mut char_indices = paragraph.char_indices();
31        if let Some(i) = char_indices.next_back().and_then(|(i, c)| {
32            // `BidiClass::B` is a Paragraph_Separator (various newline characters)
33            (bidi_class(c) == BidiClass::B).then_some(i)
34        }) {
35            Some(&paragraph[0..i])
36        } else {
37            Some(paragraph)
38        }
39    }
40}