forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicodedata.rs
More file actions
157 lines (137 loc) · 4.66 KB
/
unicodedata.rs
File metadata and controls
157 lines (137 loc) · 4.66 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Access to the unicode database.
See also: https://docs.python.org/3/library/unicodedata.html
*/
use crate::vm::{PyObjectRef, PyPayload, VirtualMachine};
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let module = unicodedata::make_module(vm);
let ucd: PyObjectRef = unicodedata::Ucd::new(unic_ucd_age::UNICODE_VERSION)
.into_ref(vm)
.into();
for attr in ["category", "lookup", "name", "bidirectional", "normalize"]
.iter()
.copied()
{
crate::vm::extend_module!(vm, &module, {
attr => ucd.get_attr(attr, vm).unwrap(),
});
}
module
}
#[pymodule]
mod unicodedata {
use crate::vm::{
builtins::PyStrRef, function::OptionalArg, PyObjectRef, PyPayload, PyRef, PyResult,
VirtualMachine,
};
use itertools::Itertools;
use unic_char_property::EnumeratedCharProperty;
use unic_normal::StrNormalForm;
use unic_ucd_age::{Age, UnicodeVersion, UNICODE_VERSION};
use unic_ucd_bidi::BidiClass;
use unic_ucd_category::GeneralCategory;
#[pyattr]
#[pyclass(name = "UCD")]
#[derive(Debug, PyPayload)]
pub(super) struct Ucd {
unic_version: UnicodeVersion,
}
impl Ucd {
pub fn new(unic_version: UnicodeVersion) -> Self {
Self { unic_version }
}
fn check_age(&self, c: char) -> bool {
Age::of(c).map_or(false, |age| age.actual() <= self.unic_version)
}
fn extract_char(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<Option<char>> {
let c = character.as_str().chars().exactly_one().map_err(|_| {
vm.new_type_error("argument must be an unicode character, not str".to_owned())
})?;
if self.check_age(c) {
Ok(Some(c))
} else {
Ok(None)
}
}
}
#[pyclass]
impl Ucd {
#[pymethod]
fn category(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
Ok(self
.extract_char(character, vm)?
.map_or(GeneralCategory::Unassigned, GeneralCategory::of)
.abbr_name()
.to_owned())
}
#[pymethod]
fn lookup(&self, name: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
if let Some(character) = unicode_names2::character(name.as_str()) {
if self.check_age(character) {
return Ok(character.to_string());
}
}
Err(vm.new_lookup_error(format!("undefined character name '{name}'")))
}
#[pymethod]
fn name(
&self,
character: PyStrRef,
default: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let c = self.extract_char(character, vm)?;
if let Some(c) = c {
if self.check_age(c) {
if let Some(name) = unicode_names2::name(c) {
return Ok(vm.ctx.new_str(name.to_string()).into());
}
}
}
default.ok_or_else(|| vm.new_value_error("character name not found!".to_owned()))
}
#[pymethod]
fn bidirectional(&self, character: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
let bidi = match self.extract_char(character, vm)? {
Some(c) => BidiClass::of(c).abbr_name(),
None => "",
};
Ok(bidi.to_owned())
}
#[pymethod]
fn normalize(
&self,
form: PyStrRef,
unistr: PyStrRef,
vm: &VirtualMachine,
) -> PyResult<String> {
let text = unistr.as_str();
let normalized_text = match form.as_str() {
"NFC" => text.nfc().collect::<String>(),
"NFKC" => text.nfkc().collect::<String>(),
"NFD" => text.nfd().collect::<String>(),
"NFKD" => text.nfkd().collect::<String>(),
_ => return Err(vm.new_value_error("invalid normalization form".to_owned())),
};
Ok(normalized_text)
}
#[pygetset]
fn unidata_version(&self) -> String {
self.unic_version.to_string()
}
}
#[pyattr]
fn ucd_3_2_0(vm: &VirtualMachine) -> PyRef<Ucd> {
Ucd {
unic_version: UnicodeVersion {
major: 3,
minor: 2,
micro: 0,
},
}
.into_ref(vm)
}
#[pyattr]
fn unidata_version(_vm: &VirtualMachine) -> String {
UNICODE_VERSION.to_string()
}
}