introduce BidiParagraphs iterator
This commit is contained in:
parent
a93ec8adf8
commit
e298259dd5
2 changed files with 42 additions and 0 deletions
39
src/bidi_para.rs
Normal file
39
src/bidi_para.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
|
use unicode_bidi::{bidi_class, BidiClass, BidiInfo, ParagraphInfo};
|
||||||
|
|
||||||
|
/// An iterator over the paragraphs in the input text.
|
||||||
|
/// It is equivalent to [`core::str::Lines`] but follows `unicode-bidi` behaviour.
|
||||||
|
pub struct BidiParagraphs<'text> {
|
||||||
|
text: &'text str,
|
||||||
|
info: std::vec::IntoIter<ParagraphInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'text> BidiParagraphs<'text> {
|
||||||
|
/// Create an iterator to split the input text into paragraphs
|
||||||
|
/// in accordance with `unicode-bidi` behaviour.
|
||||||
|
pub fn new(text: &'text str) -> Self {
|
||||||
|
let info = BidiInfo::new(text, None);
|
||||||
|
let info = info.paragraphs.into_iter();
|
||||||
|
Self { text, info }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'text> Iterator for BidiParagraphs<'text> {
|
||||||
|
type Item = &'text str;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let para = self.info.next()?;
|
||||||
|
let paragraph = &self.text[para.range];
|
||||||
|
// `para.range` includes the newline that splits the line, so remove it if present
|
||||||
|
let mut char_indices = paragraph.char_indices();
|
||||||
|
if let Some(i) = char_indices.next_back().and_then(|(i, c)| {
|
||||||
|
// `BidiClass::B` is a Paragraph_Separator (various newline characters)
|
||||||
|
(bidi_class(c) == BidiClass::B).then_some(i)
|
||||||
|
}) {
|
||||||
|
Some(¶graph[0..i])
|
||||||
|
} else {
|
||||||
|
Some(paragraph)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -95,6 +95,9 @@ extern crate alloc;
|
||||||
pub use self::attrs::*;
|
pub use self::attrs::*;
|
||||||
mod attrs;
|
mod attrs;
|
||||||
|
|
||||||
|
pub use self::bidi_para::*;
|
||||||
|
mod bidi_para;
|
||||||
|
|
||||||
pub use self::buffer::*;
|
pub use self::buffer::*;
|
||||||
mod buffer;
|
mod buffer;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue