@@ -774,6 +774,138 @@ PyObject *CPyStr_Upper(PyObject *self) {
774774#endif
775775}
776776
777+ // Convert the width argument of str.ljust() and friends.
778+ static inline Py_ssize_t CPyStr_PadWidth (CPyTagged width ) {
779+ Py_ssize_t result = CPyTagged_AsSsize_t (width );
780+ if (unlikely (result == -1 && PyErr_Occurred ())) {
781+ PyErr_SetString (PyExc_OverflowError , CPYTHON_LARGE_INT_ERRMSG );
782+ }
783+ return result ;
784+ }
785+
786+ // Get the fill character argument of str.ljust() and friends (NULL means a space).
787+ // Returns (Py_UCS4)-1 on error.
788+ static inline Py_UCS4 CPyStr_PadFillChar (PyObject * fillchar ) {
789+ if (fillchar == NULL ) {
790+ return ' ' ;
791+ }
792+ if (unlikely (PyUnicode_GET_LENGTH (fillchar ) != 1 )) {
793+ PyErr_SetString (PyExc_TypeError ,
794+ "The fill character must be exactly one character long" );
795+ return (Py_UCS4 )- 1 ;
796+ }
797+ return PyUnicode_READ_CHAR (fillchar , 0 );
798+ }
799+
800+ // Based on pad() in cpython.git:Objects/unicodeobject.c. The caller ensures that
801+ // left and right are non-negative and that left + len(self) + right fits in Py_ssize_t.
802+ static PyObject * CPyStr_Pad (PyObject * self , Py_ssize_t left , Py_ssize_t right , Py_UCS4 fill ) {
803+ Py_ssize_t len = PyUnicode_GET_LENGTH (self );
804+ if (left == 0 && right == 0 ) {
805+ // Like CPython, return the object itself if it's an exact str
806+ return PyUnicode_Substring (self , 0 , len );
807+ }
808+ Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE (self );
809+ if (fill > maxchar ) {
810+ maxchar = fill ;
811+ }
812+ PyObject * res = PyUnicode_New (left + len + right , maxchar );
813+ if (res == NULL ) {
814+ return NULL ;
815+ }
816+ int kind = PyUnicode_KIND (res );
817+ void * data = PyUnicode_DATA (res );
818+ if (kind == PyUnicode_1BYTE_KIND ) {
819+ Py_UCS1 * p = (Py_UCS1 * )data ;
820+ memset (p , (int )fill , left );
821+ memset (p + left + len , (int )fill , right );
822+ } else {
823+ for (Py_ssize_t i = 0 ; i < left ; i ++ ) {
824+ PyUnicode_WRITE (kind , data , i , fill );
825+ }
826+ for (Py_ssize_t i = left + len ; i < left + len + right ; i ++ ) {
827+ PyUnicode_WRITE (kind , data , i , fill );
828+ }
829+ }
830+ if (PyUnicode_KIND (self ) == kind ) {
831+ memcpy ((char * )data + kind * left , PyUnicode_DATA (self ), kind * len );
832+ } else if (PyUnicode_CopyCharacters (res , left , self , 0 , len ) < 0 ) {
833+ Py_DECREF (res );
834+ return NULL ;
835+ }
836+ return res ;
837+ }
838+
839+ PyObject * CPyStr_Ljust (PyObject * self , CPyTagged width , PyObject * fillchar ) {
840+ Py_ssize_t w = CPyStr_PadWidth (width );
841+ if (unlikely (w == -1 && PyErr_Occurred ())) {
842+ return NULL ;
843+ }
844+ Py_UCS4 fill = CPyStr_PadFillChar (fillchar );
845+ if (unlikely (fill == (Py_UCS4 )- 1 )) {
846+ return NULL ;
847+ }
848+ Py_ssize_t len = PyUnicode_GET_LENGTH (self );
849+ return CPyStr_Pad (self , 0 , w > len ? w - len : 0 , fill );
850+ }
851+
852+ PyObject * CPyStr_Rjust (PyObject * self , CPyTagged width , PyObject * fillchar ) {
853+ Py_ssize_t w = CPyStr_PadWidth (width );
854+ if (unlikely (w == -1 && PyErr_Occurred ())) {
855+ return NULL ;
856+ }
857+ Py_UCS4 fill = CPyStr_PadFillChar (fillchar );
858+ if (unlikely (fill == (Py_UCS4 )- 1 )) {
859+ return NULL ;
860+ }
861+ Py_ssize_t len = PyUnicode_GET_LENGTH (self );
862+ return CPyStr_Pad (self , w > len ? w - len : 0 , 0 , fill );
863+ }
864+
865+ PyObject * CPyStr_Center (PyObject * self , CPyTagged width , PyObject * fillchar ) {
866+ Py_ssize_t w = CPyStr_PadWidth (width );
867+ if (unlikely (w == -1 && PyErr_Occurred ())) {
868+ return NULL ;
869+ }
870+ Py_UCS4 fill = CPyStr_PadFillChar (fillchar );
871+ if (unlikely (fill == (Py_UCS4 )- 1 )) {
872+ return NULL ;
873+ }
874+ Py_ssize_t len = PyUnicode_GET_LENGTH (self );
875+ if (len >= w ) {
876+ return CPyStr_Pad (self , 0 , 0 , fill );
877+ }
878+ Py_ssize_t marg = w - len ;
879+ // This matches the rounding used by CPython
880+ Py_ssize_t left = marg / 2 + (marg & w & 1 );
881+ return CPyStr_Pad (self , left , marg - left , fill );
882+ }
883+
884+ PyObject * CPyStr_Zfill (PyObject * self , CPyTagged width ) {
885+ Py_ssize_t w = CPyStr_PadWidth (width );
886+ if (unlikely (w == -1 && PyErr_Occurred ())) {
887+ return NULL ;
888+ }
889+ Py_ssize_t len = PyUnicode_GET_LENGTH (self );
890+ if (len >= w ) {
891+ return CPyStr_Pad (self , 0 , 0 , '0' );
892+ }
893+ Py_ssize_t fill = w - len ;
894+ PyObject * res = CPyStr_Pad (self , fill , 0 , '0' );
895+ if (res == NULL ) {
896+ return NULL ;
897+ }
898+ int kind = PyUnicode_KIND (res );
899+ void * data = PyUnicode_DATA (res );
900+ Py_UCS4 ch = PyUnicode_READ (kind , data , fill );
901+ if (ch == '+' || ch == '-' ) {
902+ // Move the sign to the beginning
903+ PyUnicode_WRITE (kind , data , 0 , ch );
904+ PyUnicode_WRITE (kind , data , fill , '0' );
905+ }
906+ return res ;
907+ }
908+
777909bool CPyStr_IsDigit (PyObject * str ) {
778910 Py_ssize_t len = PyUnicode_GET_LENGTH (str );
779911 if (len == 0 ) return false;
0 commit comments