@@ -4404,19 +4404,47 @@ unicode_capwords(PyUnicodeObject *self)
44044404}
44054405#endif
44064406
4407+ /* Argument converter. Coerces to a single unicode character */
4408+
4409+ static int
4410+ convert_uc (PyObject * obj , void * addr )
4411+ {
4412+ Py_UNICODE * fillcharloc = (Py_UNICODE * )addr ;
4413+ PyObject * uniobj ;
4414+ Py_UNICODE * unistr ;
4415+
4416+ uniobj = PyUnicode_FromObject (obj );
4417+ if (uniobj == NULL ) {
4418+ PyErr_SetString (PyExc_TypeError ,
4419+ "The fill character cannot be converted to Unicode" );
4420+ return 0 ;
4421+ }
4422+ if (PyUnicode_GET_SIZE (uniobj ) != 1 ) {
4423+ PyErr_SetString (PyExc_TypeError ,
4424+ "The fill character must be exactly one character long" );
4425+ Py_DECREF (uniobj );
4426+ return 0 ;
4427+ }
4428+ unistr = PyUnicode_AS_UNICODE (uniobj );
4429+ * fillcharloc = unistr [0 ];
4430+ Py_DECREF (uniobj );
4431+ return 1 ;
4432+ }
4433+
44074434PyDoc_STRVAR (center__doc__ ,
4408- "S.center(width) -> unicode\n\
4435+ "S.center(width[, fillchar] ) -> unicode\n\
44094436\n\
4410- Return S centered in a Unicode string of length width. Padding is done \n\
4411- using spaces. " );
4437+ Return S centered in a Unicode string of length width. Padding is\n\
4438+ done using the specified fill character (default is a space) " );
44124439
44134440static PyObject *
44144441unicode_center (PyUnicodeObject * self , PyObject * args )
44154442{
44164443 int marg , left ;
44174444 int width ;
4445+ Py_UNICODE fillchar = ' ' ;
44184446
4419- if (!PyArg_ParseTuple (args , "i:center" , & width ))
4447+ if (!PyArg_ParseTuple (args , "i|O& :center" , & width , convert_uc , & fillchar ))
44204448 return NULL ;
44214449
44224450 if (self -> length >= width && PyUnicode_CheckExact (self )) {
@@ -4427,7 +4455,7 @@ unicode_center(PyUnicodeObject *self, PyObject *args)
44274455 marg = width - self -> length ;
44284456 left = marg / 2 + (marg & width & 1 );
44294457
4430- return (PyObject * ) pad (self , left , marg - left , ' ' );
4458+ return (PyObject * ) pad (self , left , marg - left , fillchar );
44314459}
44324460
44334461#if 0
@@ -5170,24 +5198,26 @@ unicode_length(PyUnicodeObject *self)
51705198}
51715199
51725200PyDoc_STRVAR (ljust__doc__ ,
5173- "S.ljust(width) -> unicode\n\
5201+ "S.ljust(width[, fillchar] ) -> unicode\n\
51745202\n\
51755203Return S left justified in a Unicode string of length width. Padding is\n\
5176- done using spaces ." );
5204+ done using the specified fill character (default is a space) ." );
51775205
51785206static PyObject *
51795207unicode_ljust (PyUnicodeObject * self , PyObject * args )
51805208{
51815209 int width ;
5182- if (!PyArg_ParseTuple (args , "i:ljust" , & width ))
5210+ Py_UNICODE fillchar = ' ' ;
5211+
5212+ if (!PyArg_ParseTuple (args , "i|O&:ljust" , & width , convert_uc , & fillchar ))
51835213 return NULL ;
51845214
51855215 if (self -> length >= width && PyUnicode_CheckExact (self )) {
51865216 Py_INCREF (self );
51875217 return (PyObject * ) self ;
51885218 }
51895219
5190- return (PyObject * ) pad (self , 0 , width - self -> length , ' ' );
5220+ return (PyObject * ) pad (self , 0 , width - self -> length , fillchar );
51915221}
51925222
51935223PyDoc_STRVAR (lower__doc__ ,
@@ -5552,24 +5582,26 @@ unicode_rindex(PyUnicodeObject *self, PyObject *args)
55525582}
55535583
55545584PyDoc_STRVAR (rjust__doc__ ,
5555- "S.rjust(width) -> unicode\n\
5585+ "S.rjust(width[, fillchar] ) -> unicode\n\
55565586\n\
55575587Return S right justified in a Unicode string of length width. Padding is\n\
5558- done using spaces ." );
5588+ done using the specified fill character (default is a space) ." );
55595589
55605590static PyObject *
55615591unicode_rjust (PyUnicodeObject * self , PyObject * args )
55625592{
55635593 int width ;
5564- if (!PyArg_ParseTuple (args , "i:rjust" , & width ))
5594+ Py_UNICODE fillchar = ' ' ;
5595+
5596+ if (!PyArg_ParseTuple (args , "i|O&:rjust" , & width , convert_uc , & fillchar ))
55655597 return NULL ;
55665598
55675599 if (self -> length >= width && PyUnicode_CheckExact (self )) {
55685600 Py_INCREF (self );
55695601 return (PyObject * ) self ;
55705602 }
55715603
5572- return (PyObject * ) pad (self , width - self -> length , 0 , ' ' );
5604+ return (PyObject * ) pad (self , width - self -> length , 0 , fillchar );
55735605}
55745606
55755607static PyObject *
0 commit comments