| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#define PY_SSIZE_T_CLEAN |
| 17 |
|
| 18 |
#include "Python.h" |
| 19 |
#ifndef PYPY_VERSION |
| 20 |
#if PY_MINOR_VERSION < 10 |
| 21 |
#include "ucnhash.h" |
| 22 |
#else |
| 23 |
#define Py_BUILD_CORE |
| 24 |
#include "internal/pycore_ucnhash.h" |
| 25 |
#endif |
| 26 |
#endif |
| 27 |
#include "structmember.h" |
| 28 |
#include "unicodectype.h" |
| 29 |
|
| 30 |
#include "_unicodedata2_compat.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef struct { |
| 41 |
const unsigned char category; |
| 42 |
|
| 43 |
const unsigned char combining; |
| 44 |
const unsigned char bidirectional; |
| 45 |
|
| 46 |
const unsigned char mirrored; |
| 47 |
const unsigned char east_asian_width; |
| 48 |
|
| 49 |
const unsigned char normalization_quick_check; |
| 50 |
} _PyUnicode_DatabaseRecord; |
| 51 |
|
| 52 |
typedef struct change_record { |
| 53 |
|
| 54 |
const unsigned char bidir_changed; |
| 55 |
const unsigned char category_changed; |
| 56 |
const unsigned char decimal_changed; |
| 57 |
const unsigned char mirrored_changed; |
| 58 |
const unsigned char east_asian_width_changed; |
| 59 |
const double numeric_changed; |
| 60 |
} change_record; |
| 61 |
|
| 62 |
|
| 63 |
#include "unicodedata_db.h" |
| 64 |
|
| 65 |
static const _PyUnicode_DatabaseRecord* |
| 66 |
_getrecord_ex(Py_UCS4 code) |
| 67 |
{ |
| 68 |
int index; |
| 69 |
if (code >= 0x110000) |
| 70 |
index = 0; |
| 71 |
else { |
| 72 |
index = index1[(code>>SHIFT)]; |
| 73 |
index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))]; |
| 74 |
} |
| 75 |
|
| 76 |
return &_PyUnicode_Database_Records[index]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
typedef struct previous_version { |
| 81 |
PyObject_HEAD |
| 82 |
const char *name; |
| 83 |
const change_record* (*getrecord)(Py_UCS4); |
| 84 |
Py_UCS4 (*normalization)(Py_UCS4); |
| 85 |
} PreviousDBVersion; |
| 86 |
|
| 87 |
#include "unicodedata.c.h" |
| 88 |
|
| 89 |
#define get_old_record(self, v) ((((PreviousDBVersion*)self)->getrecord)(v)) |
| 90 |
|
| 91 |
static PyMemberDef DB_members[] = { |
| 92 |
{"unidata_version", T_STRING, offsetof(PreviousDBVersion, name), READONLY}, |
| 93 |
{NULL} |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
static PyTypeObject UCD_Type; |
| 98 |
#define UCD_Check(o) (Py_TYPE(o)==&UCD_Type) |
| 99 |
|
| 100 |
#if 0 |
| 101 |
static PyObject* |
| 102 |
new_previous_version(const char*name, const change_record* (*getrecord)(Py_UCS4), |
| 103 |
Py_UCS4 (*normalization)(Py_UCS4)) |
| 104 |
{ |
| 105 |
PreviousDBVersion *self; |
| 106 |
self = PyObject_New(PreviousDBVersion, &UCD_Type); |
| 107 |
if (self == NULL) |
| •108 |
return NULL; |
| 109 |
self->name = name; |
| 110 |
self->getrecord = getrecord; |
| 111 |
self->normalization = normalization; |
| 112 |
return (PyObject*)self; |
| 113 |
} |
| 114 |
#endif |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| •122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static PyObject * |
| 135 |
unicodedata_UCD_decimal_impl(PyObject *self, int chr, |
| 136 |
PyObject *default_value) |
| 137 |
|
| 138 |
{ |
| 139 |
int have_old = 0; |
| 140 |
long rc; |
| 141 |
Py_UCS4 c = (Py_UCS4)chr; |
| 142 |
|
| 143 |
if (self && UCD_Check(self)) { |
| 144 |
const change_record *old = get_old_record(self, c); |
| 145 |
if (old->category_changed == 0) { |
| 146 |
|
| 147 |
have_old = 1; |
| 148 |
rc = -1; |
| 149 |
} |
| 150 |
else if (old->decimal_changed != 0xFF) { |
| 151 |
have_old = 1; |
| 152 |
rc = old->decimal_changed; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if (!have_old) |
| 157 |
rc = _PyUnicode2_ToDecimalDigit(c); |
| 158 |
if (rc < 0) { |
| 159 |
if (default_value == NULL) { |
| 160 |
PyErr_SetString(PyExc_ValueError, |
| 161 |
"not a decimal"); |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
else { |
| 165 |
Py_INCREF(default_value); |
| 166 |
return default_value; |
| 167 |
} |
| 168 |
} |
| 169 |
return PyLong_FromLong(rc); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static PyObject * |
| 188 |
unicodedata_UCD_digit_impl(PyObject *self, int chr, PyObject *default_value) |
| 189 |
|
| 190 |
{ |
| 191 |
long rc; |
| 192 |
Py_UCS4 c = (Py_UCS4)chr; |
| 193 |
rc = _PyUnicode2_ToDigit(c); |
| 194 |
if (rc < 0) { |
| 195 |
if (default_value == NULL) { |
| 196 |
PyErr_SetString(PyExc_ValueError, "not a digit"); |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
else { |
| 200 |
Py_INCREF(default_value); |
| 201 |
return default_value; |
| 202 |
} |
| 203 |
} |
| 204 |
return PyLong_FromLong(rc); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
static PyObject * |
| 223 |
unicodedata_UCD_numeric_impl(PyObject *self, int chr, |
| 224 |
PyObject *default_value) |
| 225 |
|
| 226 |
{ |
| 227 |
int have_old = 0; |
| 228 |
double rc; |
| 229 |
Py_UCS4 c = (Py_UCS4)chr; |
| 230 |
|
| 231 |
if (self && UCD_Check(self)) { |
| 232 |
const change_record *old = get_old_record(self, c); |
| 233 |
if (old->category_changed == 0) { |
| 234 |
|
| 235 |
have_old = 1; |
| 236 |
rc = -1.0; |
| 237 |
} |
| 238 |
else if (old->decimal_changed != 0xFF) { |
| 239 |
have_old = 1; |
| 240 |
rc = old->decimal_changed; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if (!have_old) |
| 245 |
rc = _PyUnicode2_ToNumeric(c); |
| 246 |
if (rc == -1.0) { |
| 247 |
if (default_value == NULL) { |
| 248 |
PyErr_SetString(PyExc_ValueError, "not a numeric character"); |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
else { |
| 252 |
Py_INCREF(default_value); |
| 253 |
return default_value; |
| 254 |
} |
| 255 |
} |
| 256 |
return PyFloat_FromDouble(rc); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
static PyObject * |
| 270 |
unicodedata_UCD_category_impl(PyObject *self, int chr) |
| 271 |
|
| 272 |
{ |
| 273 |
int index; |
| 274 |
Py_UCS4 c = (Py_UCS4)chr; |
| 275 |
index = (int) _getrecord_ex(c)->category; |
| 276 |
if (self && UCD_Check(self)) { |
| 277 |
const change_record *old = get_old_record(self, c); |
| 278 |
if (old->category_changed != 0xFF) |
| 279 |
index = old->category_changed; |
| 280 |
} |
| 281 |
return PyUnicode_FromString(_PyUnicode_CategoryNames[index]); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static PyObject * |
| 297 |
unicodedata_UCD_bidirectional_impl(PyObject *self, int chr) |
| 298 |
|
| 299 |
{ |
| 300 |
int index; |
| 301 |
Py_UCS4 c = (Py_UCS4)chr; |
| 302 |
index = (int) _getrecord_ex(c)->bidirectional; |
| 303 |
if (self && UCD_Check(self)) { |
| 304 |
const change_record *old = get_old_record(self, c); |
| 305 |
if (old->category_changed == 0) |
| 306 |
index = 0; |
| 307 |
else if (old->bidir_changed != 0xFF) |
| 308 |
index = old->bidir_changed; |
| 309 |
} |
| 310 |
return PyUnicode_FromString(_PyUnicode_BidirectionalNames[index]); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static int |
| 326 |
unicodedata_UCD_combining_impl(PyObject *self, int chr) |
| 327 |
|
| 328 |
{ |
| 329 |
int index; |
| 330 |
Py_UCS4 c = (Py_UCS4)chr; |
| 331 |
index = (int) _getrecord_ex(c)->combining; |
| 332 |
if (self && UCD_Check(self)) { |
| 333 |
const change_record *old = get_old_record(self, c); |
| 334 |
if (old->category_changed == 0) |
| 335 |
index = 0; |
| 336 |
} |
| 337 |
return index; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
static int |
| 354 |
unicodedata_UCD_mirrored_impl(PyObject *self, int chr) |
| 355 |
|
| 356 |
{ |
| 357 |
int index; |
| 358 |
Py_UCS4 c = (Py_UCS4)chr; |
| 359 |
index = (int) _getrecord_ex(c)->mirrored; |
| 360 |
if (self && UCD_Check(self)) { |
| 361 |
const change_record *old = get_old_record(self, c); |
| 362 |
if (old->category_changed == 0) |
| 363 |
index = 0; |
| 364 |
else if (old->mirrored_changed != 0xFF) |
| 365 |
index = old->mirrored_changed; |
| 366 |
} |
| 367 |
return index; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static PyObject * |
| 381 |
unicodedata_UCD_east_asian_width_impl(PyObject *self, int chr) |
| 382 |
|
| 383 |
{ |
| 384 |
int index; |
| 385 |
Py_UCS4 c = (Py_UCS4)chr; |
| 386 |
index = (int) _getrecord_ex(c)->east_asian_width; |
| 387 |
if (self && UCD_Check(self)) { |
| 388 |
const change_record *old = get_old_record(self, c); |
| 389 |
if (old->category_changed == 0) |
| 390 |
index = 0; |
| 391 |
else if (old->east_asian_width_changed != 0xFF) |
| 392 |
index = old->east_asian_width_changed; |
| 393 |
} |
| 394 |
return PyUnicode_FromString(_PyUnicode_EastAsianWidthNames[index]); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static PyObject * |
| 410 |
unicodedata_UCD_decomposition_impl(PyObject *self, int chr) |
| 411 |
|
| 412 |
{ |
| 413 |
char decomp[256]; |
| 414 |
int code, index, count; |
| 415 |
size_t i; |
| 416 |
unsigned int prefix_index; |
| 417 |
Py_UCS4 c = (Py_UCS4)chr; |
| 418 |
|
| 419 |
code = (int)c; |
| 420 |
|
| 421 |
if (self && UCD_Check(self)) { |
| 422 |
const change_record *old = get_old_record(self, c); |
| 423 |
if (old->category_changed == 0) |
| 424 |
return PyUnicode_FromString(""); |
| 425 |
} |
| 426 |
|
| 427 |
if (code < 0 || code >= 0x110000) |
| 428 |
index = 0; |
| 429 |
else { |
| 430 |
index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 431 |
index = decomp_index2[(index<<DECOMP_SHIFT)+ |
| 432 |
(code&((1<<DECOMP_SHIFT)-1))]; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
count = decomp_data[index] >> 8; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
prefix_index = decomp_data[index] & 255; |
| 446 |
assert(prefix_index < Py_ARRAY_LENGTH(decomp_prefix)); |
| 447 |
|
| 448 |
|
| 449 |
i = strlen(decomp_prefix[prefix_index]); |
| 450 |
memcpy(decomp, decomp_prefix[prefix_index], i); |
| 451 |
|
| 452 |
while (count-- > 0) { |
| 453 |
if (i) |
| 454 |
decomp[i++] = ' '; |
| 455 |
assert(i < sizeof(decomp)); |
| 456 |
PyOS_snprintf(decomp + i, sizeof(decomp) - i, "%04X", |
| 457 |
decomp_data[++index]); |
| 458 |
i += strlen(decomp + i); |
| 459 |
} |
| 460 |
return PyUnicode_FromStringAndSize(decomp, i); |
| 461 |
} |
| 462 |
|
| 463 |
static void |
| 464 |
get_decomp_record(PyObject *self, Py_UCS4 code, int *index, int *prefix, int *count) |
| 465 |
{ |
| 466 |
if (code >= 0x110000) { |
| 467 |
*index = 0; |
| 468 |
} else if (self && UCD_Check(self) && |
| 469 |
get_old_record(self, code)->category_changed==0) { |
| 470 |
|
| 471 |
*index = 0; |
| 472 |
} |
| 473 |
else { |
| 474 |
*index = decomp_index1[(code>>DECOMP_SHIFT)]; |
| 475 |
*index = decomp_index2[(*index<<DECOMP_SHIFT)+ |
| 476 |
(code&((1<<DECOMP_SHIFT)-1))]; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
*count = decomp_data[*index] >> 8; |
| 482 |
*prefix = decomp_data[*index] & 255; |
| 483 |
|
| 484 |
(*index)++; |
| 485 |
} |
| 486 |
|
| 487 |
#define SBase 0xAC00 |
| 488 |
#define LBase 0x1100 |
| 489 |
#define VBase 0x1161 |
| 490 |
#define TBase 0x11A7 |
| 491 |
#define LCount 19 |
| 492 |
#define VCount 21 |
| 493 |
#define TCount 28 |
| 494 |
#define NCount (VCount*TCount) |
| 495 |
#define SCount (LCount*NCount) |
| 496 |
|
| 497 |
static PyObject* |
| 498 |
nfd_nfkd(PyObject *self, PyObject *input, int k) |
| 499 |
{ |
| 500 |
PyObject *result; |
| 501 |
Py_UCS4 *output; |
| 502 |
Py_ssize_t i, o, osize; |
| 503 |
int kind; |
| 504 |
void *data; |
| 505 |
|
| 506 |
Py_UCS4 stack[20]; |
| 507 |
Py_ssize_t space, isize; |
| 508 |
int index, prefix, count, stackptr; |
| 509 |
unsigned char prev, cur; |
| 510 |
|
| 511 |
stackptr = 0; |
| 512 |
isize = PyUnicode_GET_LENGTH(input); |
| 513 |
space = isize; |
| 514 |
|
| 515 |
if (space > 10) { |
| 516 |
if (space <= PY_SSIZE_T_MAX - 10) |
| 517 |
space += 10; |
| 518 |
} |
| 519 |
else { |
| 520 |
space *= 2; |
| 521 |
} |
| 522 |
osize = space; |
| 523 |
output = PyMem_NEW(Py_UCS4, space); |
| 524 |
if (!output) { |
| 525 |
PyErr_NoMemory(); |
| 526 |
return NULL; |
| 527 |
} |
| 528 |
i = o = 0; |
| 529 |
kind = PyUnicode_KIND(input); |
| 530 |
data = PyUnicode_DATA(input); |
| 531 |
|
| 532 |
while (i < isize) { |
| 533 |
stack[stackptr++] = PyUnicode_READ(kind, data, i++); |
| 534 |
while(stackptr) { |
| 535 |
Py_UCS4 code = stack[--stackptr]; |
| 536 |
|
| 537 |
|
| 538 |
if (space < 3) { |
| 539 |
Py_UCS4 *new_output; |
| 540 |
osize += 10; |
| 541 |
space += 10; |
| 542 |
new_output = PyMem_Realloc(output, osize*sizeof(Py_UCS4)); |
| 543 |
if (new_output == NULL) { |
| 544 |
PyMem_Free(output); |
| 545 |
PyErr_NoMemory(); |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
output = new_output; |
| 549 |
} |
| 550 |
|
| 551 |
if (SBase <= code && code < (SBase+SCount)) { |
| 552 |
int SIndex = code - SBase; |
| 553 |
int L = LBase + SIndex / NCount; |
| 554 |
int V = VBase + (SIndex % NCount) / TCount; |
| 555 |
int T = TBase + SIndex % TCount; |
| 556 |
output[o++] = L; |
| 557 |
output[o++] = V; |
| 558 |
space -= 2; |
| 559 |
if (T != TBase) { |
| 560 |
output[o++] = T; |
| 561 |
space --; |
| 562 |
} |
| 563 |
continue; |
| 564 |
} |
| 565 |
|
| 566 |
if (self && UCD_Check(self)) { |
| 567 |
Py_UCS4 value = ((PreviousDBVersion*)self)->normalization(code); |
| 568 |
if (value != 0) { |
| 569 |
stack[stackptr++] = value; |
| 570 |
continue; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
get_decomp_record(self, code, &index, &prefix, &count); |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
if (!count || (prefix && !k)) { |
| 580 |
output[o++] = code; |
| 581 |
space--; |
| 582 |
continue; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
while(count) { |
| 587 |
code = decomp_data[index + (--count)]; |
| 588 |
stack[stackptr++] = code; |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
| 594 |
output, o); |
| 595 |
PyMem_Free(output); |
| 596 |
if (!result) |
| 597 |
return NULL; |
| 598 |
|
| 599 |
kind = PyUnicode_KIND(result); |
| 600 |
data = PyUnicode_DATA(result); |
| 601 |
|
| 602 |
|
| 603 |
i = 0; |
| 604 |
prev = _getrecord_ex(PyUnicode_READ(kind, data, i))->combining; |
| 605 |
for (i++; i < PyUnicode_GET_LENGTH(result); i++) { |
| 606 |
cur = _getrecord_ex(PyUnicode_READ(kind, data, i))->combining; |
| 607 |
if (prev == 0 || cur == 0 || prev <= cur) { |
| 608 |
prev = cur; |
| 609 |
continue; |
| 610 |
} |
| 611 |
|
| 612 |
o = i - 1; |
| 613 |
while (1) { |
| 614 |
Py_UCS4 tmp = PyUnicode_READ(kind, data, o+1); |
| 615 |
PyUnicode_WRITE(kind, data, o+1, |
| 616 |
PyUnicode_READ(kind, data, o)); |
| 617 |
PyUnicode_WRITE(kind, data, o, tmp); |
| 618 |
o--; |
| 619 |
if (o < 0) |
| 620 |
break; |
| 621 |
prev = _getrecord_ex(PyUnicode_READ(kind, data, o))->combining; |
| 622 |
if (prev == 0 || prev <= cur) |
| 623 |
break; |
| 624 |
} |
| 625 |
prev = _getrecord_ex(PyUnicode_READ(kind, data, i))->combining; |
| 626 |
} |
| 627 |
return result; |
| 628 |
} |
| 629 |
|
| 630 |
static int |
| 631 |
find_nfc_index(PyObject *self, struct reindex* nfc, Py_UCS4 code) |
| 632 |
{ |
| 633 |
unsigned int index; |
| 634 |
for (index = 0; nfc[index].start; index++) { |
| 635 |
unsigned int start = nfc[index].start; |
| 636 |
if (code < start) |
| 637 |
return -1; |
| 638 |
if (code <= start + nfc[index].count) { |
| 639 |
unsigned int delta = code - start; |
| 640 |
return nfc[index].index + delta; |
| 641 |
} |
| 642 |
} |
| 643 |
return -1; |
| 644 |
} |
| 645 |
|
| 646 |
static PyObject* |
| 647 |
nfc_nfkc(PyObject *self, PyObject *input, int k) |
| 648 |
{ |
| 649 |
PyObject *result; |
| 650 |
int kind; |
| 651 |
void *data; |
| 652 |
Py_UCS4 *output; |
| 653 |
Py_ssize_t i, i1, o, len; |
| 654 |
int f,l,index,index1,comb; |
| 655 |
Py_UCS4 code; |
| 656 |
Py_ssize_t skipped[20]; |
| 657 |
int cskipped = 0; |
| 658 |
|
| 659 |
result = nfd_nfkd(self, input, k); |
| 660 |
if (!result) |
| 661 |
return NULL; |
| 662 |
|
| 663 |
kind = PyUnicode_KIND(result); |
| 664 |
data = PyUnicode_DATA(result); |
| 665 |
len = PyUnicode_GET_LENGTH(result); |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
output = PyMem_NEW(Py_UCS4, len); |
| 671 |
if (!output) { |
| 672 |
PyErr_NoMemory(); |
| 673 |
Py_DECREF(result); |
| 674 |
return 0; |
| 675 |
} |
| 676 |
i = o = 0; |
| 677 |
|
| 678 |
again: |
| 679 |
while (i < len) { |
| 680 |
for (index = 0; index < cskipped; index++) { |
| 681 |
if (skipped[index] == i) { |
| 682 |
|
| 683 |
|
| 684 |
skipped[index] = skipped[cskipped-1]; |
| 685 |
cskipped--; |
| 686 |
i++; |
| 687 |
goto again; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
code = PyUnicode_READ(kind, data, i); |
| 693 |
if (LBase <= code && code < (LBase+LCount) && |
| 694 |
i + 1 < len && |
| 695 |
VBase <= PyUnicode_READ(kind, data, i+1) && |
| 696 |
PyUnicode_READ(kind, data, i+1) < (VBase+VCount)) { |
| 697 |
|
| 698 |
|
| 699 |
int LIndex, VIndex; |
| 700 |
LIndex = code - LBase; |
| 701 |
VIndex = PyUnicode_READ(kind, data, i+1) - VBase; |
| 702 |
code = SBase + (LIndex*VCount+VIndex)*TCount; |
| 703 |
i+=2; |
| 704 |
if (i < len && |
| 705 |
TBase < PyUnicode_READ(kind, data, i) && |
| 706 |
PyUnicode_READ(kind, data, i) < (TBase+TCount)) { |
| 707 |
|
| 708 |
|
| 709 |
code += PyUnicode_READ(kind, data, i)-TBase; |
| 710 |
i++; |
| 711 |
} |
| 712 |
output[o++] = code; |
| 713 |
continue; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
f = find_nfc_index(self, nfc_first, code); |
| 718 |
if (f == -1) { |
| 719 |
output[o++] = code; |
| 720 |
i++; |
| 721 |
continue; |
| 722 |
} |
| 723 |
|
| 724 |
i1 = i+1; |
| 725 |
comb = 0; |
| 726 |
|
| 727 |
output[o] = PyUnicode_READ(kind, data, i); |
| 728 |
while (i1 < len) { |
| 729 |
Py_UCS4 code1 = PyUnicode_READ(kind, data, i1); |
| 730 |
int comb1 = _getrecord_ex(code1)->combining; |
| 731 |
if (comb) { |
| 732 |
if (comb1 == 0) |
| 733 |
break; |
| 734 |
if (comb >= comb1) { |
| 735 |
|
| 736 |
i1++; |
| 737 |
continue; |
| 738 |
} |
| 739 |
} |
| 740 |
l = find_nfc_index(self, nfc_last, code1); |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
if (l == -1) { |
| 745 |
not_combinable: |
| 746 |
if (comb1 == 0) |
| 747 |
break; |
| 748 |
comb = comb1; |
| 749 |
i1++; |
| 750 |
continue; |
| 751 |
} |
| 752 |
index = f*TOTAL_LAST + l; |
| 753 |
index1 = comp_index[index >> COMP_SHIFT]; |
| 754 |
code = comp_data[(index1<<COMP_SHIFT)+ |
| 755 |
(index&((1<<COMP_SHIFT)-1))]; |
| 756 |
if (code == 0) |
| 757 |
goto not_combinable; |
| 758 |
|
| 759 |
|
| 760 |
output[o] = code; |
| 761 |
|
| 762 |
assert(cskipped < 20); |
| 763 |
skipped[cskipped++] = i1; |
| 764 |
i1++; |
| 765 |
f = find_nfc_index(self, nfc_first, output[o]); |
| 766 |
if (f == -1) |
| 767 |
break; |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
o++; i++; |
| 772 |
} |
| 773 |
if (o == len) { |
| 774 |
|
| 775 |
PyMem_Free(output); |
| 776 |
return result; |
| 777 |
} |
| 778 |
Py_DECREF(result); |
| 779 |
result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, |
| 780 |
output, o); |
| 781 |
PyMem_Free(output); |
| 782 |
return result; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
static int |
| 787 |
is_normalized(PyObject *self, PyObject *input, int nfc, int k) |
| 788 |
{ |
| 789 |
Py_ssize_t i, len; |
| 790 |
int kind; |
| 791 |
void *data; |
| 792 |
unsigned char prev_combining = 0, quickcheck_mask; |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
if (self && UCD_Check(self)) |
| 797 |
return 0; |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
quickcheck_mask = 3 << ((nfc ? 4 : 0) + (k ? 2 : 0)); |
| 802 |
|
| 803 |
i = 0; |
| 804 |
kind = PyUnicode_KIND(input); |
| 805 |
data = PyUnicode_DATA(input); |
| 806 |
len = PyUnicode_GET_LENGTH(input); |
| 807 |
while (i < len) { |
| 808 |
Py_UCS4 ch = PyUnicode_READ(kind, data, i++); |
| 809 |
const _PyUnicode_DatabaseRecord *record = _getrecord_ex(ch); |
| 810 |
unsigned char combining = record->combining; |
| 811 |
unsigned char quickcheck = record->normalization_quick_check; |
| 812 |
|
| 813 |
if (quickcheck & quickcheck_mask) |
| 814 |
return 0; |
| 815 |
if (combining && prev_combining > combining) |
| 816 |
return 0; |
| 817 |
prev_combining = combining; |
| 818 |
} |
| 819 |
return 1; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
static PyObject * |
| 836 |
unicodedata_UCD_normalize_impl(PyObject *self, const char *form, |
| 837 |
PyObject *input) |
| 838 |
|
| 839 |
{ |
| 840 |
if (PyUnicode_GET_LENGTH(input) == 0) { |
| 841 |
|
| 842 |
|
| 843 |
Py_INCREF(input); |
| 844 |
return input; |
| 845 |
} |
| 846 |
|
| 847 |
if (strcmp(form, "NFC") == 0) { |
| 848 |
if (is_normalized(self, input, 1, 0)) { |
| 849 |
Py_INCREF(input); |
| 850 |
return input; |
| 851 |
} |
| 852 |
return nfc_nfkc(self, input, 0); |
| 853 |
} |
| 854 |
if (strcmp(form, "NFKC") == 0) { |
| 855 |
if (is_normalized(self, input, 1, 1)) { |
| 856 |
Py_INCREF(input); |
| 857 |
return input; |
| 858 |
} |
| 859 |
return nfc_nfkc(self, input, 1); |
| 860 |
} |
| 861 |
if (strcmp(form, "NFD") == 0) { |
| 862 |
if (is_normalized(self, input, 0, 0)) { |
| 863 |
Py_INCREF(input); |
| 864 |
return input; |
| 865 |
} |
| 866 |
return nfd_nfkd(self, input, 0); |
| 867 |
} |
| 868 |
if (strcmp(form, "NFKD") == 0) { |
| 869 |
if (is_normalized(self, input, 0, 1)) { |
| 870 |
Py_INCREF(input); |
| 871 |
return input; |
| 872 |
} |
| 873 |
return nfd_nfkd(self, input, 1); |
| 874 |
} |
| 875 |
PyErr_SetString(PyExc_ValueError, "invalid normalization form"); |
| 876 |
return NULL; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
#include "unicodename_db.h" |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
static unsigned long |
| 889 |
_gethash(const char *s, int len, int scale) |
| 890 |
{ |
| 891 |
int i; |
| 892 |
unsigned long h = 0; |
| 893 |
unsigned long ix; |
| 894 |
for (i = 0; i < len; i++) { |
| 895 |
h = (h * scale) + (unsigned char) Py_TOUPPER(Py_CHARMASK(s[i])); |
| 896 |
ix = h & 0xff000000; |
| 897 |
if (ix) |
| 898 |
h = (h ^ ((ix>>24) & 0xff)) & 0x00ffffff; |
| 899 |
} |
| 900 |
return h; |
| 901 |
} |
| 902 |
|
| 903 |
static const char * const hangul_syllables[][3] = { |
| 904 |
{ "G", "A", "" }, |
| 905 |
{ "GG", "AE", "G" }, |
| 906 |
{ "N", "YA", "GG" }, |
| 907 |
{ "D", "YAE", "GS" }, |
| 908 |
{ "DD", "EO", "N", }, |
| 909 |
{ "R", "E", "NJ" }, |
| 910 |
{ "M", "YEO", "NH" }, |
| 911 |
{ "B", "YE", "D" }, |
| 912 |
{ "BB", "O", "L" }, |
| 913 |
{ "S", "WA", "LG" }, |
| 914 |
{ "SS", "WAE", "LM" }, |
| 915 |
{ "", "OE", "LB" }, |
| 916 |
{ "J", "YO", "LS" }, |
| 917 |
{ "JJ", "U", "LT" }, |
| 918 |
{ "C", "WEO", "LP" }, |
| 919 |
{ "K", "WE", "LH" }, |
| 920 |
{ "T", "WI", "M" }, |
| 921 |
{ "P", "YU", "B" }, |
| 922 |
{ "H", "EU", "BS" }, |
| 923 |
{ 0, "YI", "S" }, |
| 924 |
{ 0, "I", "SS" }, |
| 925 |
{ 0, 0, "NG" }, |
| 926 |
{ 0, 0, "J" }, |
| 927 |
{ 0, 0, "C" }, |
| 928 |
{ 0, 0, "K" }, |
| 929 |
{ 0, 0, "T" }, |
| 930 |
{ 0, 0, "P" }, |
| 931 |
{ 0, 0, "H" } |
| 932 |
}; |
| 933 |
|
| 934 |
|
| 935 |
#include "unicodedata_cjk.h" |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
#define IS_ALIAS(cp) ((cp >= aliases_start) && (cp < aliases_end)) |
| 940 |
#define IS_NAMED_SEQ(cp) ((cp >= named_sequences_start) && \ |
| 941 |
(cp < named_sequences_end)) |
| 942 |
|
| 943 |
static int |
| 944 |
_getucname(PyObject *self, Py_UCS4 code, char* buffer, int buflen, |
| 945 |
int with_alias_and_seq) |
| 946 |
{ |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
int offset; |
| 951 |
int i; |
| 952 |
int word; |
| 953 |
const unsigned char* w; |
| 954 |
|
| 955 |
if (code >= 0x110000) |
| 956 |
return 0; |
| 957 |
|
| 958 |
|
| 959 |
if (!with_alias_and_seq && (IS_ALIAS(code) || IS_NAMED_SEQ(code))) |
| 960 |
return 0; |
| 961 |
|
| 962 |
if (self && UCD_Check(self)) { |
| 963 |
|
| 964 |
const change_record *old; |
| 965 |
if (IS_ALIAS(code) || IS_NAMED_SEQ(code)) |
| 966 |
return 0; |
| 967 |
old = get_old_record(self, code); |
| 968 |
if (old->category_changed == 0) { |
| 969 |
|
| 970 |
return 0; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
if (SBase <= code && code < SBase+SCount) { |
| 975 |
|
| 976 |
int SIndex = code - SBase; |
| 977 |
int L = SIndex / NCount; |
| 978 |
int V = (SIndex % NCount) / TCount; |
| 979 |
int T = SIndex % TCount; |
| 980 |
|
| 981 |
if (buflen < 27) |
| 982 |
|
| 983 |
return 0; |
| 984 |
strcpy(buffer, "HANGUL SYLLABLE "); |
| 985 |
buffer += 16; |
| 986 |
strcpy(buffer, hangul_syllables[L][0]); |
| 987 |
buffer += strlen(hangul_syllables[L][0]); |
| 988 |
strcpy(buffer, hangul_syllables[V][1]); |
| 989 |
buffer += strlen(hangul_syllables[V][1]); |
| 990 |
strcpy(buffer, hangul_syllables[T][2]); |
| 991 |
buffer += strlen(hangul_syllables[T][2]); |
| 992 |
*buffer = '\0'; |
| 993 |
return 1; |
| 994 |
} |
| 995 |
|
| 996 |
if (is_unified_ideograph(code)) { |
| 997 |
if (buflen < 28) |
| 998 |
|
| 999 |
return 0; |
| 1000 |
sprintf(buffer, "CJK UNIFIED IDEOGRAPH-%X", code); |
| 1001 |
return 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
offset = phrasebook_offset1[(code>>phrasebook_shift)]; |
| 1006 |
offset = phrasebook_offset2[(offset<<phrasebook_shift) + |
| 1007 |
(code&((1<<phrasebook_shift)-1))]; |
| 1008 |
if (!offset) |
| 1009 |
return 0; |
| 1010 |
|
| 1011 |
i = 0; |
| 1012 |
|
| 1013 |
for (;;) { |
| 1014 |
|
| 1015 |
word = phrasebook[offset] - phrasebook_short; |
| 1016 |
if (word >= 0) { |
| 1017 |
word = (word << 8) + phrasebook[offset+1]; |
| 1018 |
offset += 2; |
| 1019 |
} else |
| 1020 |
word = phrasebook[offset++]; |
| 1021 |
if (i) { |
| 1022 |
if (i > buflen) |
| 1023 |
return 0; |
| 1024 |
buffer[i++] = ' '; |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
w = lexicon + lexicon_offset[word]; |
| 1030 |
while (*w < 128) { |
| 1031 |
if (i >= buflen) |
| 1032 |
return 0; |
| 1033 |
buffer[i++] = *w++; |
| 1034 |
} |
| 1035 |
if (i >= buflen) |
| 1036 |
return 0; |
| 1037 |
buffer[i++] = *w & 127; |
| 1038 |
if (*w == 128) |
| 1039 |
break; |
| 1040 |
} |
| 1041 |
|
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int |
| 1046 |
_cmpname(PyObject *self, int code, const char* name, int namelen) |
| 1047 |
{ |
| 1048 |
|
| 1049 |
int i; |
| 1050 |
char buffer[NAME_MAXLEN+1]; |
| 1051 |
if (!_getucname(self, code, buffer, NAME_MAXLEN, 1)) |
| 1052 |
return 0; |
| 1053 |
for (i = 0; i < namelen; i++) { |
| 1054 |
if (Py_TOUPPER(Py_CHARMASK(name[i])) != buffer[i]) |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
return buffer[namelen] == '\0'; |
| 1058 |
} |
| 1059 |
|
| 1060 |
static void |
| 1061 |
find_syllable(const char *str, int *len, int *pos, int count, int column) |
| 1062 |
{ |
| 1063 |
int i, len1; |
| 1064 |
*len = -1; |
| 1065 |
for (i = 0; i < count; i++) { |
| 1066 |
const char *s = hangul_syllables[i][column]; |
| 1067 |
len1 = Py_SAFE_DOWNCAST(strlen(s), size_t, int); |
| 1068 |
if (len1 <= *len) |
| 1069 |
continue; |
| 1070 |
if (strncmp(str, s, len1) == 0) { |
| 1071 |
*len = len1; |
| 1072 |
*pos = i; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
if (*len == -1) { |
| 1076 |
*len = 0; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
static int |
| 1081 |
_check_alias_and_seq(unsigned int cp, Py_UCS4* code, int with_named_seq) |
| 1082 |
{ |
| 1083 |
|
| 1084 |
if (!with_named_seq && IS_NAMED_SEQ(cp)) |
| 1085 |
return 0; |
| 1086 |
|
| 1087 |
|
| 1088 |
if (IS_ALIAS(cp)) |
| 1089 |
*code = name_aliases[cp-aliases_start]; |
| 1090 |
else |
| 1091 |
*code = cp; |
| 1092 |
return 1; |
| 1093 |
} |
| 1094 |
|
| 1095 |
static int |
| 1096 |
_getcode(PyObject* self, const char* name, int namelen, Py_UCS4* code, |
| 1097 |
int with_named_seq) |
| 1098 |
{ |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
unsigned int h, v; |
| 1104 |
unsigned int mask = code_size-1; |
| 1105 |
unsigned int i, incr; |
| 1106 |
|
| 1107 |
|
| 1108 |
if (strncmp(name, "HANGUL SYLLABLE ", 16) == 0) { |
| 1109 |
int len, L = -1, V = -1, T = -1; |
| 1110 |
const char *pos = name + 16; |
| 1111 |
find_syllable(pos, &len, &L, LCount, 0); |
| 1112 |
pos += len; |
| 1113 |
find_syllable(pos, &len, &V, VCount, 1); |
| 1114 |
pos += len; |
| 1115 |
find_syllable(pos, &len, &T, TCount, 2); |
| 1116 |
pos += len; |
| 1117 |
if (L != -1 && V != -1 && T != -1 && pos-name == namelen) { |
| 1118 |
*code = SBase + (L*VCount+V)*TCount + T; |
| 1119 |
return 1; |
| 1120 |
} |
| 1121 |
|
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) { |
| 1127 |
|
| 1128 |
v = 0; |
| 1129 |
name += 22; |
| 1130 |
namelen -= 22; |
| 1131 |
if (namelen != 4 && namelen != 5) |
| 1132 |
return 0; |
| 1133 |
while (namelen--) { |
| 1134 |
v *= 16; |
| 1135 |
if (*name >= '0' && *name <= '9') |
| 1136 |
v += *name - '0'; |
| 1137 |
else if (*name >= 'A' && *name <= 'F') |
| 1138 |
v += *name - 'A' + 10; |
| 1139 |
else |
| 1140 |
return 0; |
| 1141 |
name++; |
| 1142 |
} |
| 1143 |
if (!is_unified_ideograph(v)) |
| 1144 |
return 0; |
| 1145 |
*code = v; |
| 1146 |
return 1; |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
h = (unsigned int) _gethash(name, namelen, code_magic); |
| 1154 |
i = (~h) & mask; |
| 1155 |
v = code_hash[i]; |
| 1156 |
if (!v) |
| 1157 |
return 0; |
| 1158 |
if (_cmpname(self, v, name, namelen)) |
| 1159 |
return _check_alias_and_seq(v, code, with_named_seq); |
| 1160 |
incr = (h ^ (h >> 3)) & mask; |
| 1161 |
if (!incr) |
| 1162 |
incr = mask; |
| 1163 |
for (;;) { |
| 1164 |
i = (i + incr) & mask; |
| 1165 |
v = code_hash[i]; |
| 1166 |
if (!v) |
| 1167 |
return 0; |
| 1168 |
if (_cmpname(self, v, name, namelen)) |
| 1169 |
return _check_alias_and_seq(v, code, with_named_seq); |
| 1170 |
incr = incr << 1; |
| 1171 |
if (incr > mask) |
| 1172 |
incr = incr ^ code_poly; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
static PyObject * |
| 1201 |
unicodedata_UCD_name_impl(PyObject *self, int chr, PyObject *default_value) |
| 1202 |
|
| 1203 |
{ |
| 1204 |
char name[NAME_MAXLEN+1]; |
| 1205 |
Py_UCS4 c = (Py_UCS4)chr; |
| 1206 |
|
| 1207 |
if (!_getucname(self, c, name, NAME_MAXLEN, 0)) { |
| 1208 |
if (default_value == NULL) { |
| 1209 |
PyErr_SetString(PyExc_ValueError, "no such name"); |
| 1210 |
return NULL; |
| 1211 |
} |
| 1212 |
else { |
| 1213 |
Py_INCREF(default_value); |
| 1214 |
return default_value; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
return PyUnicode_FromString(name); |
| 1219 |
} |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
static PyObject * |
| 1235 |
unicodedata_UCD_lookup_impl(PyObject *self, const char *name, |
| 1236 |
Py_ssize_clean_t name_length) |
| 1237 |
|
| 1238 |
{ |
| 1239 |
Py_UCS4 code; |
| 1240 |
unsigned int index; |
| 1241 |
if (name_length > NAME_MAXLEN) { |
| 1242 |
PyErr_SetString(PyExc_KeyError, "name too long"); |
| 1243 |
return NULL; |
| 1244 |
} |
| 1245 |
|
| 1246 |
if (!_getcode(self, name, (int)name_length, &code, 1)) { |
| 1247 |
PyErr_Format(PyExc_KeyError, "undefined character name '%s'", name); |
| 1248 |
return NULL; |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
if (IS_NAMED_SEQ(code)) { |
| 1253 |
index = code-named_sequences_start; |
| 1254 |
return PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, |
| 1255 |
named_sequences[index].seq, |
| 1256 |
named_sequences[index].seqlen); |
| 1257 |
} |
| 1258 |
return PyUnicode_FromOrdinal(code); |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
static PyMethodDef unicodedata_functions[] = { |
| 1264 |
UNICODEDATA_UCD_DECIMAL_METHODDEF |
| 1265 |
UNICODEDATA_UCD_DIGIT_METHODDEF |
| 1266 |
UNICODEDATA_UCD_NUMERIC_METHODDEF |
| 1267 |
UNICODEDATA_UCD_CATEGORY_METHODDEF |
| 1268 |
UNICODEDATA_UCD_BIDIRECTIONAL_METHODDEF |
| 1269 |
UNICODEDATA_UCD_COMBINING_METHODDEF |
| 1270 |
UNICODEDATA_UCD_MIRRORED_METHODDEF |
| 1271 |
UNICODEDATA_UCD_EAST_ASIAN_WIDTH_METHODDEF |
| 1272 |
UNICODEDATA_UCD_DECOMPOSITION_METHODDEF |
| 1273 |
UNICODEDATA_UCD_NAME_METHODDEF |
| 1274 |
UNICODEDATA_UCD_LOOKUP_METHODDEF |
| 1275 |
UNICODEDATA_UCD_NORMALIZE_METHODDEF |
| 1276 |
{NULL, NULL} |
| 1277 |
}; |
| 1278 |
|
| 1279 |
static PyTypeObject UCD_Type = { |
| 1280 |
|
| 1281 |
|
| 1282 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1283 |
"unicodedata2.UCD", |
| 1284 |
sizeof(PreviousDBVersion), |
| 1285 |
0, |
| 1286 |
|
| 1287 |
(destructor)PyObject_Del, |
| 1288 |
0, |
| 1289 |
0, |
| 1290 |
0, |
| 1291 |
0, |
| 1292 |
0, |
| 1293 |
0, |
| 1294 |
0, |
| 1295 |
0, |
| 1296 |
0, |
| 1297 |
0, |
| 1298 |
0, |
| 1299 |
PyObject_GenericGetAttr, |
| 1300 |
0, |
| 1301 |
0, |
| 1302 |
Py_TPFLAGS_DEFAULT, |
| 1303 |
0, |
| 1304 |
0, |
| 1305 |
0, |
| 1306 |
0, |
| 1307 |
0, |
| 1308 |
0, |
| 1309 |
0, |
| 1310 |
unicodedata_functions, |
| 1311 |
DB_members, |
| 1312 |
0, |
| 1313 |
0, |
| 1314 |
0, |
| 1315 |
0, |
| 1316 |
0, |
| 1317 |
0, |
| 1318 |
0, |
| 1319 |
0, |
| 1320 |
0, |
| 1321 |
0, |
| 1322 |
0, |
| 1323 |
}; |
| 1324 |
|
| 1325 |
PyDoc_STRVAR(unicodedata_docstring, |
| 1326 |
"This module provides access to the Unicode Character Database which\n\ |
| 1327 |
defines character properties for all Unicode characters. The data in\n\ |
| 1328 |
this database is based on the UnicodeData.txt file version\n\ |
| 1329 |
" UNIDATA_VERSION " which is publicly available from ftp://ftp.unicode.org/.\n\ |
| 1330 |
\n\ |
| 1331 |
The module uses the same names and symbols as defined by the\n\ |
| 1332 |
UnicodeData File Format " UNIDATA_VERSION "."); |
| 1333 |
|
| 1334 |
static struct PyModuleDef unicodedatamodule = { |
| 1335 |
PyModuleDef_HEAD_INIT, |
| 1336 |
"unicodedata2", |
| 1337 |
unicodedata_docstring, |
| 1338 |
-1, |
| 1339 |
unicodedata_functions, |
| 1340 |
NULL, |
| 1341 |
NULL, |
| 1342 |
NULL, |
| 1343 |
NULL |
| 1344 |
}; |
| 1345 |
|
| 1346 |
PyMODINIT_FUNC |
| 1347 |
PyInit_unicodedata2(void) |
| 1348 |
{ |
| 1349 |
PyObject *m, *u, *v; |
| 1350 |
|
| 1351 |
Py_SET_TYPE(&UCD_Type, &PyType_Type); |
| 1352 |
|
| 1353 |
m = PyModule_Create(&unicodedatamodule); |
| 1354 |
if (!m) |
| 1355 |
return NULL; |
| 1356 |
|
| 1357 |
PyModule_AddStringConstant(m, "unidata_version", UNIDATA_VERSION); |
| 1358 |
Py_INCREF(&UCD_Type); |
| 1359 |
PyModule_AddObject(m, "UCD", (PyObject*)&UCD_Type); |
| 1360 |
|
| 1361 |
|
| 1362 |
u = PyImport_ImportModule("unicodedata"); |
| 1363 |
if (u != NULL) { |
| 1364 |
v = PyObject_GetAttrString(u, "ucd_3_2_0"); |
| 1365 |
if (v != NULL) |
| 1366 |
PyModule_AddObject(m, "ucd_3_2_0", v); |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| •1373 |
return m; |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|