mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
SF #989185: Drop unicode.iswide() and unicode.width() and add
unicodedata.east_asian_width(). You can still implement your own
simple width() function using it like this:
def width(u):
w = 0
for c in unicodedata.normalize('NFC', u):
cwidth = unicodedata.east_asian_width(c)
if cwidth in ('W', 'F'): w += 2
else: w += 1
return w
This commit is contained in:
parent
b5047fd019
commit
e9ddfbb412
15 changed files with 1641 additions and 1617 deletions
|
|
@ -24,6 +24,8 @@ typedef struct {
|
|||
const unsigned char bidirectional; /* index into
|
||||
_PyUnicode_BidirectionalNames */
|
||||
const unsigned char mirrored; /* true if mirrored in bidir mode */
|
||||
const unsigned char east_asian_width; /* index into
|
||||
_PyUnicode_EastAsianWidth */
|
||||
} _PyUnicode_DatabaseRecord;
|
||||
|
||||
/* data file generated by Tools/unicode/makeunicodedata.py */
|
||||
|
|
@ -204,6 +206,24 @@ unicodedata_mirrored(PyObject *self, PyObject *args)
|
|||
return PyInt_FromLong((int) _getrecord(v)->mirrored);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
unicodedata_east_asian_width(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyUnicodeObject *v;
|
||||
int index;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O!:east_asian_width",
|
||||
&PyUnicode_Type, &v))
|
||||
return NULL;
|
||||
if (PyUnicode_GET_SIZE(v) != 1) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"need a single Unicode character as parameter");
|
||||
return NULL;
|
||||
}
|
||||
index = (int) _getrecord(v)->east_asian_width;
|
||||
return PyString_FromString(_PyUnicode_EastAsianWidthNames[index]);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
unicodedata_decomposition(PyObject *self, PyObject *args)
|
||||
{
|
||||
|
|
@ -871,6 +891,7 @@ static PyMethodDef unicodedata_functions[] = {
|
|||
{"bidirectional", unicodedata_bidirectional, METH_VARARGS},
|
||||
{"combining", unicodedata_combining, METH_VARARGS},
|
||||
{"mirrored", unicodedata_mirrored, METH_VARARGS},
|
||||
{"east_asian_width", unicodedata_east_asian_width, METH_VARARGS},
|
||||
{"decomposition",unicodedata_decomposition, METH_VARARGS},
|
||||
{"name", unicodedata_name, METH_VARARGS},
|
||||
{"lookup", unicodedata_lookup, METH_VARARGS},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue