Skip to content

Commit 4cdb8d1

Browse files
committed
unicodedata.is_mirrored
1 parent c824016 commit 4cdb8d1

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ unic-ucd-bidi = "0.9.0"
208208
unic-ucd-category = "0.9.0"
209209
unic-ucd-ident = "0.9.0"
210210
unicode_names2 = "1.3.0"
211+
unicode-bidi-mirroring = "0.2"
211212
widestring = "1.1.0"
212213
windows-sys = "0.59.0"
213214
wasm-bindgen = "0.2.100"

Lib/test/test_unicodedata.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ def test_decomposition(self):
179179
self.assertRaises(TypeError, self.db.decomposition)
180180
self.assertRaises(TypeError, self.db.decomposition, 'xx')
181181

182-
# TODO: RUSTPYTHON
183-
@unittest.expectedFailure
184182
def test_mirrored(self):
185183
self.assertEqual(self.db.mirrored('\uFFFE'), 0)
186184
self.assertEqual(self.db.mirrored('a'), 0)

stdlib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ unic-ucd-category = { workspace = true }
7373
unic-ucd-age = { workspace = true }
7474
unic-ucd-ident = { workspace = true }
7575
ucd = "0.1.1"
76+
unicode-bidi-mirroring = { workspace = true }
7677

7778
# compression
7879
adler32 = "1.2.0"

stdlib/src/unicodedata.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
2323
"bidirectional",
2424
"east_asian_width",
2525
"normalize",
26+
"mirrored",
2627
]
2728
.into_iter()
2829
{
@@ -72,6 +73,7 @@ mod unicodedata {
7273
use unic_ucd_age::{Age, UNICODE_VERSION, UnicodeVersion};
7374
use unic_ucd_bidi::BidiClass;
7475
use unic_ucd_category::GeneralCategory;
76+
use unicode_bidi_mirroring::is_mirroring;
7577

7678
#[pyattr]
7779
#[pyclass(name = "UCD")]
@@ -193,6 +195,21 @@ mod unicodedata {
193195
Ok(normalized_text)
194196
}
195197

198+
#[pymethod]
199+
fn mirrored(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<i32> {
200+
match self.extract_char(character, vm)? {
201+
Some(c) => {
202+
if let Some(ch) = c.to_char() {
203+
// Check if the character is mirrored in bidirectional text using Unicode standard
204+
Ok(if is_mirroring(ch) { 1 } else { 0 })
205+
} else {
206+
Ok(0)
207+
}
208+
}
209+
None => Ok(0),
210+
}
211+
}
212+
196213
#[pygetset]
197214
fn unidata_version(&self) -> String {
198215
self.unic_version.to_string()

0 commit comments

Comments
 (0)