Replace unmaintained `unic` crates by ShaharNaveh · Pull Request #7555 · RustPython/RustPython
use icu_normalizer::{ComposingNormalizerBorrowed, DecomposingNormalizerBorrowed}; use icu_properties::{ CodePointSetData, props::{ BidiClass, BidiMirrored, CanonicalCombiningClass, EastAsianWidth, EnumeratedProperty, GeneralCategory, NamedEnumeratedProperty, }, }; use itertools::Itertools; use rustpython_common::wtf8::{CodePoint, Wtf8Buf}; use ucd::{Codepoint, DecompositionType, EastAsianWidth, Number, NumericType}; use unic_char_property::EnumeratedCharProperty; use unic_normal::StrNormalForm; use ucd::{Codepoint, DecompositionType, Number, NumericType}; use unic_ucd_age::{Age, UNICODE_VERSION, UnicodeVersion}; use unic_ucd_bidi::BidiClass; use unic_ucd_category::GeneralCategory; use unicode_bidi_mirroring::is_mirroring;
pub(crate) fn module_exec(vm: &VirtualMachine, module: &Py<PyModule>) -> PyResult<()> { __module_exec(vm, module);
#[pymethod] fn normalize(&self, form: super::NormalizeForm, unistr: PyStrRef) -> PyResult<Wtf8Buf> { let text = unistr.as_wtf8(); let normalized_text = match form { Nfc => text.map_utf8(|s| s.nfc()).collect(), Nfkc => text.map_utf8(|s| s.nfkc()).collect(), Nfd => text.map_utf8(|s| s.nfd()).collect(), Nfkd => text.map_utf8(|s| s.nfkd()).collect(), Nfc => { let normalizer = ComposingNormalizerBorrowed::new_nfc(); text.map_utf8(|s| normalizer.normalize_iter(s.chars())) .collect() } Nfkc => { let normalizer = ComposingNormalizerBorrowed::new_nfkc(); text.map_utf8(|s| normalizer.normalize_iter(s.chars())) .collect() } Nfd => { let normalizer = DecomposingNormalizerBorrowed::new_nfd(); text.map_utf8(|s| normalizer.normalize_iter(s.chars())) .collect() } Nfkd => { let normalizer = DecomposingNormalizerBorrowed::new_nfkd(); text.map_utf8(|s| normalizer.normalize_iter(s.chars())) .collect() } }; Ok(normalized_text) }
#[pymethod] fn combining(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<i32> { fn combining(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<u8> { Ok(self .extract_char(character, vm)? .and_then(|c| c.to_char()) .map_or(0, |ch| ch.canonical_combining_class() as i32)) .map_or(0, |ch| { CanonicalCombiningClass::for_char(ch).to_icu4c_value() })) }
#[pymethod]
trait EastAsianWidthAbbrName { fn abbr_name(&self) -> &'static str; }
impl EastAsianWidthAbbrName for EastAsianWidth { fn abbr_name(&self) -> &'static str { match self { Self::Narrow => "Na", Self::Wide => "W", Self::Neutral => "N", Self::Ambiguous => "A", Self::FullWidth => "F", Self::HalfWidth => "H", } } }
#[pyattr] fn ucd_3_2_0(vm: &VirtualMachine) -> PyRef<Ucd> { Ucd {