@@ -655,6 +655,27 @@ int PyUnicode_GetSize(PyObject *unicode)
655655 return -1 ;
656656}
657657
658+ int PyUnicode_GetWidth (PyObject * unicode )
659+ {
660+ const Py_UNICODE * p , * e ;
661+ int width ;
662+
663+ if (!PyUnicode_Check (unicode )) {
664+ PyErr_BadArgument ();
665+ return -1 ;
666+ }
667+
668+ p = PyUnicode_AS_UNICODE (unicode );
669+ e = p + PyUnicode_GET_SIZE (unicode );
670+ for (width = 0 ; p < e ; p ++ )
671+ if (Py_UNICODE_ISWIDE (* p ))
672+ width += 2 ;
673+ else
674+ width ++ ;
675+
676+ return width ;
677+ }
678+
658679const char * PyUnicode_GetDefaultEncoding (void )
659680{
660681 return unicode_default_encoding ;
@@ -5316,6 +5337,35 @@ unicode_isnumeric(PyUnicodeObject *self)
53165337 return PyBool_FromLong (1 );
53175338}
53185339
5340+ PyDoc_STRVAR (iswide__doc__ ,
5341+ "S.iswide() -> bool\n\
5342+ \n\
5343+ Return True if all characters in S are wide width\n\
5344+ and there is at least one character in S, False otherwise." );
5345+
5346+ static PyObject *
5347+ unicode_iswide (PyUnicodeObject * self )
5348+ {
5349+ register const Py_UNICODE * p = PyUnicode_AS_UNICODE (self );
5350+ register const Py_UNICODE * e ;
5351+
5352+ /* Shortcut for single character strings */
5353+ if (PyUnicode_GET_SIZE (self ) == 1 &&
5354+ Py_UNICODE_ISWIDE (* p ))
5355+ Py_RETURN_TRUE ;
5356+
5357+ /* Special case for empty strings */
5358+ if (PyString_GET_SIZE (self ) == 0 )
5359+ Py_RETURN_FALSE ;
5360+
5361+ e = p + PyUnicode_GET_SIZE (self );
5362+ for (; p < e ; p ++ ) {
5363+ if (!Py_UNICODE_ISWIDE (* p ))
5364+ Py_RETURN_FALSE ;
5365+ }
5366+ Py_RETURN_TRUE ;
5367+ }
5368+
53195369PyDoc_STRVAR (join__doc__ ,
53205370"S.join(sequence) -> unicode\n\
53215371\n\
@@ -5335,7 +5385,7 @@ unicode_length(PyUnicodeObject *self)
53355385}
53365386
53375387PyDoc_STRVAR (ljust__doc__ ,
5338- "S.ljust(width[, fillchar]) -> unicode \n\
5388+ "S.ljust(width[, fillchar]) -> int \n\
53395389\n\
53405390Return S left justified in a Unicode string of length width. Padding is\n\
53415391done using the specified fill character (default is a space)." );
@@ -5927,6 +5977,21 @@ unicode_upper(PyUnicodeObject *self)
59275977 return fixup (self , fixupper );
59285978}
59295979
5980+ PyDoc_STRVAR (width__doc__ ,
5981+ "S.width() -> unicode\n\
5982+ \n\
5983+ Return a fixed-width representation length of S." );
5984+
5985+ static PyObject *
5986+ unicode_width (PyObject * self )
5987+ {
5988+ int width = PyUnicode_GetWidth (self );
5989+ if (width == -1 )
5990+ return NULL ;
5991+ else
5992+ return PyInt_FromLong ((long )width );
5993+ }
5994+
59305995PyDoc_STRVAR (zfill__doc__ ,
59315996"S.zfill(width) -> unicode\n\
59325997\n\
@@ -6090,6 +6155,8 @@ static PyMethodDef unicode_methods[] = {
60906155 {"isnumeric" , (PyCFunction ) unicode_isnumeric , METH_NOARGS , isnumeric__doc__ },
60916156 {"isalpha" , (PyCFunction ) unicode_isalpha , METH_NOARGS , isalpha__doc__ },
60926157 {"isalnum" , (PyCFunction ) unicode_isalnum , METH_NOARGS , isalnum__doc__ },
6158+ {"iswide" , (PyCFunction ) unicode_iswide , METH_NOARGS , iswide__doc__ },
6159+ {"width" , (PyCFunction ) unicode_width , METH_NOARGS , width__doc__ },
60936160 {"zfill" , (PyCFunction ) unicode_zfill , METH_VARARGS , zfill__doc__ },
60946161#if 0
60956162 {"capwords" , (PyCFunction ) unicode_capwords , METH_NOARGS , capwords__doc__ },
0 commit comments