| 1 |
/* ------------------------------------------------------------------------ |
| 2 |
|
| 3 |
unicodedata -- Provides access to the Unicode database. |
| 4 |
|
| 5 |
Data was extracted from the UnicodeData.txt file. |
| 6 |
The current version number is reported in the unidata_version constant. |
| 7 |
|
| 8 |
Written by Marc-Andre Lemburg ([email protected]). |
| 9 |
Modified for Python 2.0 by Fredrik Lundh ([email protected]) |
| 10 |
Modified by Martin v. Löwis ([email protected]) |
| 11 |
|
| 12 |
Copyright (c) Corporation for National Research Initiatives. |
| 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 |
/*[clinic input] |
| 33 |
module unicodedata |
| 34 |
class unicodedata.UCD 'PreviousDBVersion *' '&UCD_Type' |
| 35 |
[clinic start generated code]*/ |
| 36 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6dac153082d150bc]*/ |
| 37 |
|
| 38 |
/* character properties */ |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
const unsigned char category; /* index into |
| 42 |
_PyUnicode_CategoryNames */ |
| 43 |
const unsigned char combining; /* combining class value 0 - 255 */ |
| 44 |
const unsigned char bidirectional; /* index into |
| 45 |
_PyUnicode_BidirectionalNames */ |
| 46 |
const unsigned char mirrored; /* true if mirrored in bidir mode */ |
| 47 |
const unsigned char east_asian_width; /* index into |
| 48 |
_PyUnicode_EastAsianWidth */ |
| 49 |
const unsigned char normalization_quick_check; /* see is_normalized() */ |
| 50 |
} _PyUnicode_DatabaseRecord; |
| 51 |
|
| 52 |
typedef struct change_record { |
| 53 |
/* sequence of fields should be the same as in merge_old_version */ |
| 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 |
/* data file generated by Tools/unicode/makeunicodedata.py */ |
| 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 |
/* ------------- Previous-version API ------------------------------------- */ |
| 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 |
/* forward declaration */ |
| 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 |
/* --- Module API --------------------------------------------------------- */ |
| 118 |
|
| 119 |
/*[clinic input] |
| 120 |
unicodedata.UCD.decimal |
| 121 |
|
| 122 |
self: self |
| 123 |
chr: int(accept={str}) |
| 124 |
default: object=NULL |
| 125 |
/ |
| 126 |
|
| 127 |
Converts a Unicode character into its equivalent decimal value. |
| 128 |
|
| 129 |
Returns the decimal value assigned to the character chr as integer. |
| 130 |
If no such value is defined, default is returned, or, if not given, |
| 131 |
ValueError is raised. |
| 132 |
[clinic start generated code]*/ |
| 133 |
|
| 134 |
static PyObject * |
| 135 |
unicodedata_UCD_decimal_impl(PyObject *self, int chr, |
| 136 |
PyObject *default_value) |
| 137 |
/*[clinic end generated code: output=be23376e1a185231 input=933f8107993f23d0]*/ |
| 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 |
/* unassigned */ |
| 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 |
/*[clinic input] |
| 173 |
unicodedata.UCD.digit |
| 174 |
|
| 175 |
self: self |
| 176 |
chr: int(accept={str}) |
| 177 |
default: object=NULL |
| 178 |
/ |
| 179 |
|
| 180 |
Converts a Unicode character into its equivalent digit value. |
| 181 |
|
| 182 |
Returns the digit value assigned to the character chr as integer. |
| 183 |
If no such value is defined, default is returned, or, if not given, |
| 184 |
ValueError is raised. |
| 185 |
[clinic start generated code]*/ |
| 186 |
|
| 187 |
static PyObject * |
| 188 |
unicodedata_UCD_digit_impl(PyObject *self, int chr, PyObject *default_value) |
| 189 |
/*[clinic end generated code: output=96e18c950171fd2f input=e27d6e4565cd29f2]*/ |
| 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 |
/*[clinic input] |
| 208 |
unicodedata.UCD.numeric |
| 209 |
|
| 210 |
self: self |
| 211 |
chr: int(accept={str}) |
| 212 |
default: object=NULL |
| 213 |
/ |
| 214 |
|
| 215 |
Converts a Unicode character into its equivalent numeric value. |
| 216 |
|
| 217 |
Returns the numeric value assigned to the character chr as float. |
| 218 |
If no such value is defined, default is returned, or, if not given, |
| 219 |
ValueError is raised. |
| 220 |
[clinic start generated code]*/ |
| 221 |
|
| 222 |
static PyObject * |
| 223 |
unicodedata_UCD_numeric_impl(PyObject *self, int chr, |
| 224 |
PyObject *default_value) |
| 225 |
/*[clinic end generated code: output=53ce281fe85b10c4 input=fdf5871a5542893c]*/ |
| 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 |
/* unassigned */ |
| 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 |
/*[clinic input] |
| 260 |
unicodedata.UCD.category |
| 261 |
|
| 262 |
self: self |
| 263 |
chr: int(accept={str}) |
| 264 |
/ |
| 265 |
|
| 266 |
Returns the general category assigned to the character chr as string. |
| 267 |
[clinic start generated code]*/ |
| 268 |
|
| 269 |
static PyObject * |
| 270 |
unicodedata_UCD_category_impl(PyObject *self, int chr) |
| 271 |
/*[clinic end generated code: output=8571539ee2e6783a input=27d6f3d85050bc06]*/ |
| 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 |
/*[clinic input] |
| 285 |
unicodedata.UCD.bidirectional |
| 286 |
|
| 287 |
self: self |
| 288 |
chr: int(accept={str}) |
| 289 |
/ |
| 290 |
|
| 291 |
Returns the bidirectional class assigned to the character chr as string. |
| 292 |
|
| 293 |
If no such value is defined, an empty string is returned. |
| 294 |
[clinic start generated code]*/ |
| 295 |
|
| 296 |
static PyObject * |
| 297 |
unicodedata_UCD_bidirectional_impl(PyObject *self, int chr) |
| 298 |
/*[clinic end generated code: output=d36310ce2039bb92 input=b3d8f42cebfcf475]*/ |
| 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; /* unassigned */ |
| 307 |
else if (old->bidir_changed != 0xFF) |
| 308 |
index = old->bidir_changed; |
| 309 |
} |
| 310 |
return PyUnicode_FromString(_PyUnicode_BidirectionalNames[index]); |
| 311 |
} |
| 312 |
|
| 313 |
/*[clinic input] |
| 314 |
unicodedata.UCD.combining -> int |
| 315 |
|
| 316 |
self: self |
| 317 |
chr: int(accept={str}) |
| 318 |
/ |
| 319 |
|
| 320 |
Returns the canonical combining class assigned to the character chr as integer. |
| 321 |
|
| 322 |
Returns 0 if no combining class is defined. |
| 323 |
[clinic start generated code]*/ |
| 324 |
|
| 325 |
static int |
| 326 |
unicodedata_UCD_combining_impl(PyObject *self, int chr) |
| 327 |
/*[clinic end generated code: output=cad056d0cb6a5920 input=9f2d6b2a95d0a22a]*/ |
| 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; /* unassigned */ |
| 336 |
} |
| 337 |
return index; |
| 338 |
} |
| 339 |
|
| 340 |
/*[clinic input] |
| 341 |
unicodedata.UCD.mirrored -> int |
| 342 |
|
| 343 |
self: self |
| 344 |
chr: int(accept={str}) |
| 345 |
/ |
| 346 |
|
| 347 |
Returns the mirrored property assigned to the character chr as integer. |
| 348 |
|
| 349 |
Returns 1 if the character has been identified as a "mirrored" |
| 350 |
character in bidirectional text, 0 otherwise. |
| 351 |
[clinic start generated code]*/ |
| 352 |
|
| 353 |
static int |
| 354 |
unicodedata_UCD_mirrored_impl(PyObject *self, int chr) |
| 355 |
/*[clinic end generated code: output=2532dbf8121b50e6 input=5dd400d351ae6f3b]*/ |
| 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; /* unassigned */ |
| 364 |
else if (old->mirrored_changed != 0xFF) |
| 365 |
index = old->mirrored_changed; |
| 366 |
} |
| 367 |
return index; |
| 368 |
} |
| 369 |
|
| 370 |
/*[clinic input] |
| 371 |
unicodedata.UCD.east_asian_width |
| 372 |
|
| 373 |
self: self |
| 374 |
chr: int(accept={str}) |
| 375 |
/ |
| 376 |
|
| 377 |
Returns the east asian width assigned to the character chr as string. |
| 378 |
[clinic start generated code]*/ |
| 379 |
|
| 380 |
static PyObject * |
| 381 |
unicodedata_UCD_east_asian_width_impl(PyObject *self, int chr) |
| 382 |
/*[clinic end generated code: output=484e8537d9ee8197 input=c4854798aab026e0]*/ |
| 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; /* unassigned */ |
| 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 |
/*[clinic input] |
| 398 |
unicodedata.UCD.decomposition |
| 399 |
|
| 400 |
self: self |
| 401 |
chr: int(accept={str}) |
| 402 |
/ |
| 403 |
|
| 404 |
Returns the character decomposition mapping assigned to the character chr as string. |
| 405 |
|
| 406 |
An empty string is returned in case no such mapping is defined. |
| 407 |
[clinic start generated code]*/ |
| 408 |
|
| 409 |
static PyObject * |
| 410 |
unicodedata_UCD_decomposition_impl(PyObject *self, int chr) |
| 411 |
/*[clinic end generated code: output=7d699f3ec7565d27 input=e4c12459ad68507b]*/ |
| 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(""); /* unassigned */ |
| 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 |
/* high byte is number of hex bytes (usually one or two), low byte |
| 436 |
is prefix code (from*/ |
| 437 |
count = decomp_data[index] >> 8; |
| 438 |
|
| 439 |
/* XXX: could allocate the PyString up front instead |
| 440 |
(strlen(prefix) + 5 * count + 1 bytes) */ |
| 441 |
|
| 442 |
/* Based on how index is calculated above and decomp_data is generated |
| 443 |
from Tools/unicode/makeunicodedata.py, it should not be possible |
| 444 |
to overflow decomp_prefix. */ |
| 445 |
prefix_index = decomp_data[index] & 255; |
| 446 |
assert(prefix_index < Py_ARRAY_LENGTH(decomp_prefix)); |
| 447 |
|
| 448 |
/* copy prefix */ |
| 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 |
/* unassigned in old version */ |
| 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 |
/* high byte is number of hex bytes (usually one or two), low byte |
| 480 |
is prefix code (from*/ |
| 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 |
/* Longest decomposition in Unicode 3.2: U+FDFA */ |
| 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 |
/* Overallocate at most 10 characters. */ |
| 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 |
/* Hangul Decomposition adds three characters in |
| 537 |
a single step, so we need at least that much room. */ |
| 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 |
/* Hangul Decomposition. */ |
| 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 |
/* normalization changes */ |
| 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 |
/* Other decompositions. */ |
| 575 |
get_decomp_record(self, code, &index, &prefix, &count); |
| 576 |
|
| 577 |
/* Copy character if it is not decomposable, or has a |
| 578 |
compatibility decomposition, but we do NFD. */ |
| 579 |
if (!count || (prefix && !k)) { |
| 580 |
output[o++] = code; |
| 581 |
space--; |
| 582 |
continue; |
| 583 |
} |
| 584 |
/* Copy decomposition onto the stack, in reverse |
| 585 |
order. */ |
| 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 |
/* result is guaranteed to be ready, as it is compact. */ |
| 599 |
kind = PyUnicode_KIND(result); |
| 600 |
data = PyUnicode_DATA(result); |
| 601 |
|
| 602 |
/* Sort canonically. */ |
| 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 |
/* Non-canonical order. Need to switch *i with previous. */ |
| 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 |
/* result will be "ready". */ |
| 663 |
kind = PyUnicode_KIND(result); |
| 664 |
data = PyUnicode_DATA(result); |
| 665 |
len = PyUnicode_GET_LENGTH(result); |
| 666 |
|
| 667 |
/* We allocate a buffer for the output. |
| 668 |
If we find that we made no changes, we still return |
| 669 |
the NFD result. */ |
| 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 |
/* *i character is skipped. |
| 683 |
Remove from list. */ |
| 684 |
skipped[index] = skipped[cskipped-1]; |
| 685 |
cskipped--; |
| 686 |
i++; |
| 687 |
goto again; /* continue while */ |
| 688 |
} |
| 689 |
} |
| 690 |
/* Hangul Composition. We don't need to check for <LV,T> |
| 691 |
pairs, since we always have decomposed data. */ |
| 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 |
/* check L character is a modern leading consonant (0x1100 ~ 0x1112) |
| 698 |
and V character is a modern vowel (0x1161 ~ 0x1175). */ |
| 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 |
/* check T character is a modern trailing consonant |
| 708 |
(0x11A8 ~ 0x11C2). */ |
| 709 |
code += PyUnicode_READ(kind, data, i)-TBase; |
| 710 |
i++; |
| 711 |
} |
| 712 |
output[o++] = code; |
| 713 |
continue; |
| 714 |
} |
| 715 |
|
| 716 |
/* code is still input[i] here */ |
| 717 |
f = find_nfc_index(self, nfc_first, code); |
| 718 |
if (f == -1) { |
| 719 |
output[o++] = code; |
| 720 |
i++; |
| 721 |
continue; |
| 722 |
} |
| 723 |
/* Find next unblocked character. */ |
| 724 |
i1 = i+1; |
| 725 |
comb = 0; |
| 726 |
/* output base character for now; might be updated later. */ |
| 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 |
/* Character is blocked. */ |
| 736 |
i1++; |
| 737 |
continue; |
| 738 |
} |
| 739 |
} |
| 740 |
l = find_nfc_index(self, nfc_last, code1); |
| 741 |
/* i1 cannot be combined with i. If i1 |
| 742 |
is a starter, we don't need to look further. |
| 743 |
Otherwise, record the combining class. */ |
| 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 |
/* Replace the original character. */ |
| 760 |
output[o] = code; |
| 761 |
/* Mark the second character unused. */ |
| 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 |
/* Output character was already written. |
| 770 |
Just advance the indices. */ |
| 771 |
o++; i++; |
| 772 |
} |
| 773 |
if (o == len) { |
| 774 |
/* No changes. Return original string. */ |
| 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 |
/* Return 1 if the input is certainly normalized, 0 if it might not be. */ |
| 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 |
/* An older version of the database is requested, quickchecks must be |
| 795 |
disabled. */ |
| 796 |
if (self && UCD_Check(self)) |
| 797 |
return 0; |
| 798 |
|
| 799 |
/* The two quickcheck bits at this shift mean 0=Yes, 1=Maybe, 2=No, |
| 800 |
as described in http://unicode.org/reports/tr15/#Annex8. */ |
| 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; /* this string might need normalization */ |
| 815 |
if (combining && prev_combining > combining) |
| 816 |
return 0; /* non-canonical sort order, not normalized */ |
| 817 |
prev_combining = combining; |
| 818 |
} |
| 819 |
return 1; /* certainly normalized */ |
| 820 |
} |
| 821 |
|
| 822 |
/*[clinic input] |
| 823 |
unicodedata.UCD.normalize |
| 824 |
|
| 825 |
self: self |
| 826 |
form: str |
| 827 |
unistr as input: unicode |
| 828 |
/ |
| 829 |
|
| 830 |
Return the normal form 'form' for the Unicode string unistr. |
| 831 |
|
| 832 |
Valid values for form are 'NFC', 'NFKC', 'NFD', and 'NFKD'. |
| 833 |
[clinic start generated code]*/ |
| 834 |
|
| 835 |
static PyObject * |
| 836 |
unicodedata_UCD_normalize_impl(PyObject *self, const char *form, |
| 837 |
PyObject *input) |
| 838 |
/*[clinic end generated code: output=62d1f8870027efdc input=1744c55f4ab79bf0]*/ |
| 839 |
{ |
| 840 |
if (PyUnicode_GET_LENGTH(input) == 0) { |
| 841 |
/* Special case empty input strings, since resizing |
| 842 |
them later would cause internal errors. */ |
| 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 |
/* unicode character name tables */ |
| 881 |
|
| 882 |
/* data file generated by Tools/unicode/makeunicodedata.py */ |
| 883 |
#include "unicodename_db.h" |
| 884 |
|
| 885 |
/* -------------------------------------------------------------------- */ |
| 886 |
/* database code (cut and pasted from the unidb package) */ |
| 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 |
/* CJK Unified Ideograph ranges, generated by makeunicodedata.py */ |
| 935 |
#include "unicodedata_cjk.h" |
| 936 |
|
| 937 |
/* macros used to determine if the given code point is in the PUA range that |
| 938 |
* we are using to store aliases and named sequences */ |
| 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 |
/* Find the name associated with the given code point. |
| 948 |
* If with_alias_and_seq is 1, check for names in the Private Use Area 15 |
| 949 |
* that we are using for aliases and named sequences. */ |
| 950 |
int offset; |
| 951 |
int i; |
| 952 |
int word; |
| 953 |
const unsigned char* w; |
| 954 |
|
| 955 |
if (code >= 0x110000) |
| 956 |
return 0; |
| 957 |
|
| 958 |
/* XXX should we just skip all the code points in the PUAs here? */ |
| 959 |
if (!with_alias_and_seq && (IS_ALIAS(code) || IS_NAMED_SEQ(code))) |
| 960 |
return 0; |
| 961 |
|
| 962 |
if (self && UCD_Check(self)) { |
| 963 |
/* in 3.2.0 there are no aliases and named sequences */ |
| 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 |
/* unassigned */ |
| 970 |
return 0; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
if (SBase <= code && code < SBase+SCount) { |
| 975 |
/* Hangul syllable. */ |
| 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 |
/* Worst case: HANGUL SYLLABLE <10chars>. */ |
| 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 |
/* Worst case: CJK UNIFIED IDEOGRAPH-20000 */ |
| 999 |
return 0; |
| 1000 |
sprintf(buffer, "CJK UNIFIED IDEOGRAPH-%X", code); |
| 1001 |
return 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
/* get offset into phrasebook */ |
| 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 |
/* get word index */ |
| 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; /* buffer overflow */ |
| 1024 |
buffer[i++] = ' '; |
| 1025 |
} |
| 1026 |
/* copy word string from lexicon. the last character in the |
| 1027 |
word has bit 7 set. the last word in a string ends with |
| 1028 |
0x80 */ |
| 1029 |
w = lexicon + lexicon_offset[word]; |
| 1030 |
while (*w < 128) { |
| 1031 |
if (i >= buflen) |
| 1032 |
return 0; /* buffer overflow */ |
| 1033 |
buffer[i++] = *w++; |
| 1034 |
} |
| 1035 |
if (i >= buflen) |
| 1036 |
return 0; /* buffer overflow */ |
| 1037 |
buffer[i++] = *w & 127; |
| 1038 |
if (*w == 128) |
| 1039 |
break; /* end of word */ |
| 1040 |
} |
| 1041 |
|
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int |
| 1046 |
_cmpname(PyObject *self, int code, const char* name, int namelen) |
| 1047 |
{ |
| 1048 |
/* check if code corresponds to the given name */ |
| 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 |
/* check if named sequences are allowed */ |
| 1084 |
if (!with_named_seq && IS_NAMED_SEQ(cp)) |
| 1085 |
return 0; |
| 1086 |
/* if the code point is in the PUA range that we use for aliases, |
| 1087 |
* convert it to obtain the right code point */ |
| 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 |
/* Return the code point associated with the given name. |
| 1100 |
* Named aliases are resolved too (unless self != NULL (i.e. we are using |
| 1101 |
* 3.2.0)). If with_named_seq is 1, returns the PUA code point that we are |
| 1102 |
* using for the named sequence, and the caller must then convert it. */ |
| 1103 |
unsigned int h, v; |
| 1104 |
unsigned int mask = code_size-1; |
| 1105 |
unsigned int i, incr; |
| 1106 |
|
| 1107 |
/* Check for hangul syllables. */ |
| 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 |
/* Otherwise, it's an illegal syllable name. */ |
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
|
| 1125 |
/* Check for unified ideographs. */ |
| 1126 |
if (strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) == 0) { |
| 1127 |
/* Four or five hexdigits must follow. */ |
| 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 |
/* the following is the same as python's dictionary lookup, with |
| 1150 |
only minor changes. see the makeunicodedata script for more |
| 1151 |
details */ |
| 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 |
static const _PyUnicode_Name_CAPI hashAPI = |
| 1177 |
{ |
| 1178 |
sizeof(_PyUnicode_Name_CAPI), |
| 1179 |
_getucname, |
| 1180 |
_getcode |
| 1181 |
}; |
| 1182 |
*/ |
| 1183 |
/* -------------------------------------------------------------------- */ |
| 1184 |
/* Python bindings */ |
| 1185 |
|
| 1186 |
/*[clinic input] |
| 1187 |
unicodedata.UCD.name |
| 1188 |
|
| 1189 |
self: self |
| 1190 |
chr: int(accept={str}) |
| 1191 |
default: object=NULL |
| 1192 |
/ |
| 1193 |
|
| 1194 |
Returns the name assigned to the character chr as a string. |
| 1195 |
|
| 1196 |
If no name is defined, default is returned, or, if not given, |
| 1197 |
ValueError is raised. |
| 1198 |
[clinic start generated code]*/ |
| 1199 |
|
| 1200 |
static PyObject * |
| 1201 |
unicodedata_UCD_name_impl(PyObject *self, int chr, PyObject *default_value) |
| 1202 |
/*[clinic end generated code: output=6bbb37a326407707 input=3e0367f534de56d9]*/ |
| 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 |
/*[clinic input] |
| 1222 |
unicodedata.UCD.lookup |
| 1223 |
|
| 1224 |
self: self |
| 1225 |
name: str(accept={str, robuffer}, zeroes=True) |
| 1226 |
/ |
| 1227 |
|
| 1228 |
Look up character by name. |
| 1229 |
|
| 1230 |
If a character with the given name is found, return the |
| 1231 |
corresponding character. If not found, KeyError is raised. |
| 1232 |
[clinic start generated code]*/ |
| 1233 |
|
| 1234 |
static PyObject * |
| 1235 |
unicodedata_UCD_lookup_impl(PyObject *self, const char *name, |
| 1236 |
Py_ssize_clean_t name_length) |
| 1237 |
/*[clinic end generated code: output=765cb8186788e6be input=a557be0f8607a0d6]*/ |
| 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 |
/* check if code is in the PUA range that we use for named sequences |
| 1251 |
and convert it */ |
| 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 |
/* XXX Add doc strings. */ |
| 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} /* sentinel */ |
| 1277 |
}; |
| 1278 |
|
| 1279 |
static PyTypeObject UCD_Type = { |
| 1280 |
/* The ob_type field must be initialized in the module init function |
| 1281 |
* to be portable to Windows without using C++. */ |
| 1282 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1283 |
"unicodedata2.UCD", /*tp_name*/ |
| 1284 |
sizeof(PreviousDBVersion), /*tp_basicsize*/ |
| 1285 |
0, /*tp_itemsize*/ |
| 1286 |
/* methods */ |
| 1287 |
(destructor)PyObject_Del, /*tp_dealloc*/ |
| 1288 |
0, /*tp_print*/ |
| 1289 |
0, /*tp_getattr*/ |
| 1290 |
0, /*tp_setattr*/ |
| 1291 |
0, /*tp_reserved*/ |
| 1292 |
0, /*tp_repr*/ |
| 1293 |
0, /*tp_as_number*/ |
| 1294 |
0, /*tp_as_sequence*/ |
| 1295 |
0, /*tp_as_mapping*/ |
| 1296 |
0, /*tp_hash*/ |
| 1297 |
0, /*tp_call*/ |
| 1298 |
0, /*tp_str*/ |
| 1299 |
PyObject_GenericGetAttr,/*tp_getattro*/ |
| 1300 |
0, /*tp_setattro*/ |
| 1301 |
0, /*tp_as_buffer*/ |
| 1302 |
Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 1303 |
0, /*tp_doc*/ |
| 1304 |
0, /*tp_traverse*/ |
| 1305 |
0, /*tp_clear*/ |
| 1306 |
0, /*tp_richcompare*/ |
| 1307 |
0, /*tp_weaklistoffset*/ |
| 1308 |
0, /*tp_iter*/ |
| 1309 |
0, /*tp_iternext*/ |
| 1310 |
unicodedata_functions, /*tp_methods*/ |
| 1311 |
DB_members, /*tp_members*/ |
| 1312 |
0, /*tp_getset*/ |
| 1313 |
0, /*tp_base*/ |
| 1314 |
0, /*tp_dict*/ |
| 1315 |
0, /*tp_descr_get*/ |
| 1316 |
0, /*tp_descr_set*/ |
| 1317 |
0, /*tp_dictoffset*/ |
| 1318 |
0, /*tp_init*/ |
| 1319 |
0, /*tp_alloc*/ |
| 1320 |
0, /*tp_new*/ |
| 1321 |
0, /*tp_free*/ |
| 1322 |
0, /*tp_is_gc*/ |
| 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 |
/* Import ucd_3_2_0 from unicodedata */ |
| 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 |
/* Export C API */ |
| 1370 |
// v = PyCapsule_New((void *)&hashAPI, PyUnicodeData_CAPSULE_NAME, NULL); |
| 1371 |
// if (v != NULL) |
| 1372 |
// PyModule_AddObject(m, "ucnhash_CAPI", v); |
| 1373 |
return m; |
| 1374 |
} |
| 1375 |
|
| 1376 |
/* |
| 1377 |
Local variables: |
| 1378 |
c-basic-offset: 4 |
| 1379 |
indent-tabs-mode: nil |
| 1380 |
End: |
| 1381 |
*/ |