-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from tigregalis/iter-bidi-para
Replace use of `str::Lines` with `unicode-bidi`-based iterator
- Loading branch information
Showing
4 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: alloc::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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters