1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use super::{bidi_class, BidiClass, is_explicit, is_rtl};
pub trait BidiChar {
fn bidi_class(self) -> BidiClass;
fn is_explicit(self) -> bool;
fn is_ltr(self) -> bool;
fn is_rtl(self) -> bool;
}
impl BidiChar for char {
#[inline]
fn bidi_class(self) -> BidiClass {
bidi_class(self)
}
#[inline]
fn is_explicit(self) -> bool {
is_explicit(bidi_class(self))
}
#[inline]
fn is_ltr(self) -> bool {
!is_rtl(bidi_class(self))
}
#[inline]
fn is_rtl(self) -> bool {
is_rtl(bidi_class(self))
}
}
pub trait BidiStr {
fn has_explicit(&self) -> bool;
fn has_ltr(&self) -> bool;
fn has_rtl(&self) -> bool;
}
impl BidiStr for str {
#[inline]
fn has_explicit(&self) -> bool {
self.chars().any(|ch| ch.is_explicit())
}
#[inline]
fn has_ltr(&self) -> bool {
self.chars().any(|ch| ch.is_rtl())
}
#[inline]
fn has_rtl(&self) -> bool {
self.chars().any(|ch| ch.is_rtl())
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_bidi_char() {
use super::{BidiChar, BidiClass};
let ch = '\u{0041}';
assert_eq!(ch.bidi_class(), BidiClass::L);
assert!(ch.is_ltr());
assert!(!ch.is_rtl());
let ch = '\u{05D0}';
assert_eq!(ch.bidi_class(), BidiClass::R);
assert!(!ch.is_ltr());
assert!(ch.is_rtl());
let ch = '\u{0627}';
assert_eq!(ch.bidi_class(), BidiClass::AL);
assert!(!ch.is_ltr());
assert!(ch.is_rtl());
}
#[test]
fn test_bidi_str() {
use super::BidiStr;
let text = "";
assert!(!text.has_explicit());
assert!(!text.has_ltr());
assert!(!text.has_rtl());
let text = "\u{0041}\u{05D0}\u{0627}";
assert!(!text.has_explicit());
assert!(text.has_ltr());
assert!(text.has_rtl());
}
}