| 1 |
""" |
| 2 |
Implementation of the interpreter-level functions in the module unicodedata. |
| 3 |
""" |
| 4 |
|
| 5 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 6 |
from pypy.interpreter.baseobjspace import W_Root |
| 7 |
from pypy.interpreter.error import OperationError, oefmt |
| 8 |
from pypy.interpreter.typedef import TypeDef, interp_attrproperty |
| 9 |
from rpython.rlib.rarithmetic import r_longlong, r_uint |
| 10 |
from rpython.rlib.unicodedata import unicodedb_14_0_0, unicodedb_3_2_0 |
| 11 |
from rpython.rlib.rutf8 import Utf8StringBuilder, unichr_as_utf8, Utf8StringPosIterator |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
SBase = 0xAC00 |
| 16 |
LBase = 0x1100 |
| 17 |
VBase = 0x1161 |
| 18 |
TBase = 0x11A7 |
| 19 |
LCount = 19 |
| 20 |
VCount = 21 |
| 21 |
TCount = 28 |
| 22 |
NCount = (VCount*TCount) |
| 23 |
SCount = (LCount*NCount) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def unichr_to_code_w(space, w_unichr): |
| 32 |
if not space.isinstance_w(w_unichr, space.w_unicode): |
| 33 |
raise oefmt( |
| 34 |
space.w_TypeError, 'argument 1 must be unicode, not %T', |
| 35 |
w_unichr) |
| 36 |
|
| 37 |
if not space.len_w(w_unichr) == 1: |
| 38 |
raise oefmt(space.w_TypeError, |
| 39 |
"need a single Unicode character as parameter") |
| 40 |
return space.int_w(space.ord(w_unichr)) |
| 41 |
|
| 42 |
|
| 43 |
class UCD(W_Root): |
| 44 |
def __init__(self, unicodedb): |
| 45 |
self._unicodedb = unicodedb |
| 46 |
self._lookup = unicodedb.lookup_with_alias |
| 47 |
self._lookup_named_sequence = unicodedb.lookup_named_sequence |
| 48 |
self._lookup_named_sequence_length = unicodedb.lookup_named_sequence_length |
| 49 |
self._name = unicodedb.name |
| 50 |
self._decimal = unicodedb.decimal |
| 51 |
self._digit = unicodedb.digit |
| 52 |
self._numeric = unicodedb.numeric |
| 53 |
self._category = unicodedb.category |
| 54 |
self._east_asian_width = unicodedb.east_asian_width |
| 55 |
self._bidirectional = unicodedb.bidirectional |
| 56 |
self._combining = unicodedb.combining |
| 57 |
self._mirrored = unicodedb.mirrored |
| 58 |
self._decomposition = unicodedb.decomposition |
| 59 |
self._canon_decomposition = unicodedb.canon_decomposition |
| 60 |
self._compat_decomposition = unicodedb.compat_decomposition |
| 61 |
self._composition = unicodedb.composition |
| 62 |
|
| 63 |
self.version = unicodedb.version |
| 64 |
|
| 65 |
@unwrap_spec(name='text') |
| 66 |
def _get_code(self, space, name): |
| 67 |
try: |
| 68 |
code = self._lookup(name.upper()) |
| 69 |
except KeyError: |
| 70 |
msg = space.mod(space.newtext("undefined character name '%s'"), space.newtext(name)) |
| 71 |
raise OperationError(space.w_KeyError, msg) |
| 72 |
return space.newint(code) |
| 73 |
|
| 74 |
@unwrap_spec(name='text') |
| 75 |
def lookup(self, space, name): |
| 76 |
try: |
| 77 |
code = self._lookup(name.upper(), with_named_sequence=True) |
| 78 |
except KeyError: |
| 79 |
msg = space.mod(space.newtext("undefined character name '%s'"), space.newtext(name)) |
| 80 |
raise OperationError(space.w_KeyError, msg) |
| 81 |
|
| 82 |
|
| 83 |
sequence = self._lookup_named_sequence(code) |
| 84 |
if sequence is not None: |
| 85 |
return space.newutf8(sequence, self._lookup_named_sequence_length(code)) |
| 86 |
|
| 87 |
return space.newutf8(unichr_as_utf8(r_uint(code)), 1) |
| 88 |
|
| 89 |
def name(self, space, w_unichr, w_default=None): |
| 90 |
code = unichr_to_code_w(space, w_unichr) |
| 91 |
try: |
| 92 |
name = self._name(code) |
| 93 |
except KeyError: |
| 94 |
if w_default is not None: |
| 95 |
return w_default |
| 96 |
raise oefmt(space.w_ValueError, "no such name") |
| 97 |
return space.newtext(name) |
| 98 |
|
| 99 |
def decimal(self, space, w_unichr, w_default=None): |
| 100 |
code = unichr_to_code_w(space, w_unichr) |
| 101 |
try: |
| 102 |
return space.newint(self._decimal(code)) |
| 103 |
except KeyError: |
| 104 |
pass |
| 105 |
if w_default is not None: |
| 106 |
return w_default |
| 107 |
raise oefmt(space.w_ValueError, "not a decimal") |
| 108 |
|
| 109 |
def digit(self, space, w_unichr, w_default=None): |
| 110 |
code = unichr_to_code_w(space, w_unichr) |
| 111 |
try: |
| 112 |
return space.newint(self._digit(code)) |
| 113 |
except KeyError: |
| 114 |
pass |
| 115 |
if w_default is not None: |
| 116 |
return w_default |
| 117 |
raise oefmt(space.w_ValueError, "not a digit") |
| 118 |
|
| 119 |
def numeric(self, space, w_unichr, w_default=None): |
| 120 |
code = unichr_to_code_w(space, w_unichr) |
| 121 |
try: |
| 122 |
return space.newfloat(self._numeric(code)) |
| 123 |
except KeyError: |
| 124 |
pass |
| 125 |
if w_default is not None: |
| 126 |
return w_default |
| 127 |
raise oefmt(space.w_ValueError, "not a numeric character") |
| 128 |
|
| 129 |
def category(self, space, w_unichr): |
| 130 |
code = unichr_to_code_w(space, w_unichr) |
| 131 |
return space.newtext(self._category(code)) |
| 132 |
|
| 133 |
def east_asian_width(self, space, w_unichr): |
| 134 |
code = unichr_to_code_w(space, w_unichr) |
| 135 |
return space.newtext(self._east_asian_width(code)) |
| 136 |
|
| 137 |
def bidirectional(self, space, w_unichr): |
| 138 |
code = unichr_to_code_w(space, w_unichr) |
| 139 |
return space.newtext(self._bidirectional(code)) |
| 140 |
|
| 141 |
def combining(self, space, w_unichr): |
| 142 |
code = unichr_to_code_w(space, w_unichr) |
| 143 |
return space.newint(self._combining(code)) |
| 144 |
|
| 145 |
def mirrored(self, space, w_unichr): |
| 146 |
code = unichr_to_code_w(space, w_unichr) |
| 147 |
|
| 148 |
return space.newint(int(self._mirrored(code))) |
| 149 |
|
| 150 |
def decomposition(self, space, w_unichr): |
| 151 |
code = unichr_to_code_w(space, w_unichr) |
| 152 |
return space.newtext(self._decomposition(code)) |
| 153 |
|
| 154 |
@unwrap_spec(form='text') |
| 155 |
def normalize(self, space, form, w_unistr): |
| 156 |
if not space.isinstance_w(w_unistr, space.w_unicode): |
| 157 |
raise oefmt( |
| 158 |
space.w_TypeError, 'argument 2 must be unicode, not %T', |
| 159 |
w_unistr) |
| 160 |
if form == 'NFC': |
| 161 |
composed = True |
| 162 |
decomposition = self._canon_decomposition |
| 163 |
elif form == 'NFD': |
| 164 |
composed = False |
| 165 |
decomposition = self._canon_decomposition |
| 166 |
elif form == 'NFKC': |
| 167 |
composed = True |
| 168 |
decomposition = self._compat_decomposition |
| 169 |
elif form == 'NFKD': |
| 170 |
composed = False |
| 171 |
decomposition = self._compat_decomposition |
| 172 |
else: |
| 173 |
raise oefmt(space.w_ValueError, "invalid normalization form") |
| 174 |
|
| 175 |
strlen = space.len_w(w_unistr) |
| 176 |
content = space.utf8_w(w_unistr) |
| 177 |
if len(content) == strlen: |
| 178 |
return space.newutf8(content, strlen) |
| 179 |
result = [0] * (strlen + strlen / 10 + 10) |
| 180 |
j = 0 |
| 181 |
resultlen = len(result) |
| 182 |
|
| 183 |
for ch, i in Utf8StringPosIterator(content): |
| 184 |
|
| 185 |
if SBase <= ch < SBase + SCount: |
| 186 |
SIndex = ch - SBase |
| 187 |
L = LBase + SIndex / NCount |
| 188 |
V = VBase + (SIndex % NCount) / TCount |
| 189 |
T = TBase + SIndex % TCount |
| 190 |
if T == TBase: |
| 191 |
if j + 2 > resultlen: |
| 192 |
result.extend([0] * (j + 2 - resultlen + 10)) |
| 193 |
resultlen = len(result) |
| 194 |
result[j] = L |
| 195 |
result[j + 1] = V |
| 196 |
j += 2 |
| 197 |
else: |
| 198 |
if j + 3 > resultlen: |
| 199 |
result.extend([0] * (j + 3 - resultlen + 10)) |
| 200 |
resultlen = len(result) |
| 201 |
result[j] = L |
| 202 |
result[j + 1] = V |
| 203 |
result[j + 2] = T |
| 204 |
j += 3 |
| 205 |
continue |
| 206 |
decomp = decomposition(ch) |
| 207 |
if decomp: |
| 208 |
decomplen = len(decomp) |
| 209 |
if j + decomplen > resultlen: |
| 210 |
result.extend([0] * (j + decomplen - resultlen + 10)) |
| 211 |
resultlen = len(result) |
| 212 |
for ch in decomp: |
| 213 |
result[j] = ch |
| 214 |
j += 1 |
| 215 |
else: |
| 216 |
if j + 1 > resultlen: |
| 217 |
result.extend([0] * (j + 1 - resultlen + 10)) |
| 218 |
resultlen = len(result) |
| 219 |
result[j] = ch |
| 220 |
j += 1 |
| 221 |
|
| 222 |
|
| 223 |
for i in range(j): |
| 224 |
ch = result[i] |
| 225 |
comb = self._combining(ch) |
| 226 |
if comb == 0: |
| 227 |
continue |
| 228 |
for k in range(i, 0, -1): |
| 229 |
if self._combining(result[k - 1]) <= comb: |
| 230 |
result[k] = ch |
| 231 |
break |
| 232 |
|
| 233 |
result[k] = result[k - 1] |
| 234 |
else: |
| 235 |
result[0] = ch |
| 236 |
|
| 237 |
if not composed: |
| 238 |
return self.build(space, result, stop=j) |
| 239 |
|
| 240 |
if j <= 1: |
| 241 |
return self.build(space, result, stop=j) |
| 242 |
|
| 243 |
current = result[0] |
| 244 |
starter_pos = 0 |
| 245 |
next_insert = 1 |
| 246 |
prev_combining = 0 |
| 247 |
if self._combining(current): |
| 248 |
prev_combining = 256 |
| 249 |
for k in range(1, j): |
| 250 |
next = result[k] |
| 251 |
next_combining = self._combining(next) |
| 252 |
if next_insert == starter_pos + 1 or prev_combining < next_combining: |
| 253 |
|
| 254 |
if (LBase <= current < LBase + LCount and |
| 255 |
VBase <= next < VBase + VCount): |
| 256 |
|
| 257 |
current = SBase + ((current - LBase)*VCount + (next - VBase)) * TCount |
| 258 |
continue |
| 259 |
|
| 260 |
if (SBase <= current < SBase + SCount and |
| 261 |
TBase < next < TBase + TCount and |
| 262 |
(current - SBase) % TCount == 0): |
| 263 |
|
| 264 |
current = current + (next - TBase) |
| 265 |
continue |
| 266 |
try: |
| 267 |
current = self._composition(current, next) |
| 268 |
continue |
| 269 |
except KeyError: |
| 270 |
pass |
| 271 |
|
| 272 |
if next_combining == 0: |
| 273 |
|
| 274 |
result[starter_pos] = current |
| 275 |
starter_pos = next_insert |
| 276 |
next_insert += 1 |
| 277 |
prev_combining = 0 |
| 278 |
current = next |
| 279 |
continue |
| 280 |
|
| 281 |
result[next_insert] = next |
| 282 |
next_insert += 1 |
| 283 |
if next_combining > prev_combining: |
| 284 |
prev_combining = next_combining |
| 285 |
|
| 286 |
result[starter_pos] = current |
| 287 |
|
| 288 |
return self.build(space, result, stop=next_insert) |
| 289 |
|
| 290 |
@unwrap_spec(form='text') |
| 291 |
def is_normalized(self, space, form, w_uni): |
| 292 |
|
| 293 |
return space.eq(self.normalize(space, form, w_uni), w_uni) |
| 294 |
|
| 295 |
def build(self, space, r, stop): |
| 296 |
builder = Utf8StringBuilder(stop * 3) |
| 297 |
for i in range(stop): |
| 298 |
builder.append_code(r[i]) |
| 299 |
return space.newutf8(builder.build(), stop) |
| 300 |
|
| 301 |
|
| 302 |
methods = {} |
| 303 |
for methodname in """ |
| 304 |
_get_code lookup name decimal digit numeric category east_asian_width |
| 305 |
bidirectional combining mirrored decomposition normalize |
| 306 |
is_normalized |
| 307 |
""".split(): |
| 308 |
methods[methodname] = interp2app(getattr(UCD, methodname)) |
| 309 |
|
| 310 |
|
| 311 |
UCD.typedef = TypeDef("unicodedata.UCD", |
| 312 |
__doc__ = "", |
| 313 |
unidata_version = interp_attrproperty('version', UCD, |
| 314 |
wrapfn="newtext"), |
| 315 |
**methods) |
| 316 |
|
| 317 |
ucd_3_2_0 = UCD(unicodedb_3_2_0) |
| 318 |
ucd_14_0_0 = UCD(unicodedb_14_0_0) |
| 319 |
ucd = ucd_14_0_0 |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
unicodedb = ucd._unicodedb |