use std::borrow::Cow;
use unicode_bidi::{BidiClass, bidi_class};
pub(crate) const LEFT_TO_RIGHT_MARK: char = '\u{200E}';
pub(crate) const RIGHT_TO_LEFT_MARK: char = '\u{200F}';
pub(crate) const LEFT_TO_RIGHT_ISOLATE: char = '\u{2066}';
pub(crate) const RIGHT_TO_LEFT_ISOLATE: char = '\u{2067}';
pub(crate) const FIRST_STRONG_ISOLATE: char = '\u{2068}';
pub(crate) const POP_DIRECTIONAL_ISOLATE: char = '\u{2069}';
pub(crate) const WORD_JOINER: char = '\u{2060}';
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Direction {
LeftToRight,
RightToLeft,
}
impl Direction {
pub(crate) fn of_letter(ch: char) -> Option<Self> {
match bidi_class(ch) {
BidiClass::L => Some(Self::LeftToRight),
BidiClass::R | BidiClass::AL => Some(Self::RightToLeft),
_ => None,
}
}
}
pub(crate) fn has_right_to_left(text: &str) -> bool {
text
.chars()
.any(|ch| matches!(bidi_class(ch), BidiClass::R | BidiClass::AL | BidiClass::AN))
}
pub(crate) fn is_paragraph_break(ch: char) -> bool {
matches!(
ch,
'\n' | '\r' | '\u{1C}'..='\u{1E}' | '\u{85}' | '\u{2028}' | '\u{2029}'
)
}
pub(crate) fn paragraph_breaks_as_line_feeds(text: &str) -> Cow<'_, str> {
replace_chars(text, |ch| is_paragraph_break(ch) && !matches!(ch, '\n' | '\r'), '\n')
}
pub(crate) fn paragraph_breaks_as_spaces(text: &str) -> Cow<'_, str> {
replace_chars(text, is_paragraph_break, ' ')
}
fn replace_chars(text: &str, replaced: impl Fn(char) -> bool, with: char) -> Cow<'_, str> {
if text.chars().any(&replaced) {
Cow::Owned(text.chars().map(|ch| if replaced(ch) { with } else { ch }).collect())
} else {
Cow::Borrowed(text)
}
}
pub(crate) fn is_invisible(text: &str) -> bool {
text.chars().all(|ch| {
ch.is_whitespace()
|| ch.is_control()
|| is_default_ignorable(ch)
|| ch == '\u{2800}'
|| bidi_class(ch) == BidiClass::NSM
})
}
fn is_default_ignorable(ch: char) -> bool {
matches!(
ch,
'\u{AD}'
| '\u{34F}'
| '\u{61C}'
| '\u{115F}'..='\u{1160}'
| '\u{17B4}'..='\u{17B5}'
| '\u{180B}'..='\u{180F}'
| '\u{200B}'..='\u{200F}'
| '\u{202A}'..='\u{202E}'
| '\u{2060}'..='\u{206F}'
| '\u{3164}'
| '\u{FE00}'..='\u{FE0F}'
| '\u{FEFF}'
| '\u{FFA0}'
| '\u{FFF0}'..='\u{FFF8}'
| '\u{1BCA0}'..='\u{1BCA3}'
| '\u{1D173}'..='\u{1D17A}'
| '\u{E0000}'..='\u{E0FFF}'
)
}
pub(crate) fn line_break_offsets(text: &str) -> impl Iterator<Item = usize> + '_ {
unicode_linebreak::linebreaks(text)
.map(|(offset, _)| offset)
.filter(move |offset| *offset > 0 && *offset < text.len())
}
pub fn is_bidi_control(ch: char) -> bool {
matches!(
ch,
'\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}' | '\u{200E}' | '\u{200F}' | '\u{061C}'
)
}
pub fn strip_bidi_controls(text: &str) -> Cow<'_, str> {
if text.chars().any(is_bidi_control) {
Cow::Owned(text.chars().filter(|ch| !is_bidi_control(*ch)).collect())
} else {
Cow::Borrowed(text)
}
}
pub fn isolate_bidi(text: &str) -> String {
let stripped = strip_bidi_controls(text);
let mut isolated = String::with_capacity(stripped.len() + 6);
isolated.push(FIRST_STRONG_ISOLATE);
isolated.push_str(&stripped);
isolated.push(POP_DIRECTIONAL_ISOLATE);
isolated
}