[Python-checkins] python/dist/src/Objects stringobject.c, 2.212, 2.213 unicodeobject.c, 2.201, 2.202

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed Nov 26 03:21:38 EST 2003


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv11053/Objects
Modified Files:
	stringobject.c unicodeobject.c 
Log Message:
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.212
retrieving revision 2.213
diff -C2 -d -r2.212 -r2.213
*** stringobject.c	22 Oct 2003 02:56:39 -0000	2.212
--- stringobject.c	26 Nov 2003 08:21:35 -0000	2.213
***************
*** 2618,2625 ****
 
 PyDoc_STRVAR(ljust__doc__,
! "S.ljust(width) -> string\n"
 "\n"
 "Return S left justified in a string of length width. Padding is\n"
! "done using spaces.");
 
 static PyObject *
--- 2618,2625 ----
 
 PyDoc_STRVAR(ljust__doc__,
! "S.ljust(width[, fillchar]) -> string\n"
 "\n"
 "Return S left justified in a string of length width. Padding is\n"
! "done using the specified fill character (default is a space).");
 
 static PyObject *
***************
*** 2627,2631 ****
 {
 int width;
! if (!PyArg_ParseTuple(args, "i:ljust", &width))
 return NULL;
 
--- 2627,2633 ----
 {
 int width;
! char fillchar = ' ';
! 
! if (!PyArg_ParseTuple(args, "i|c:ljust", &width, &fillchar))
 return NULL;
 
***************
*** 2635,2647 ****
 }
 
! return pad(self, 0, width - PyString_GET_SIZE(self), ' ');
 }
 
 
 PyDoc_STRVAR(rjust__doc__,
! "S.rjust(width) -> string\n"
 "\n"
 "Return S right justified in a string of length width. Padding is\n"
! "done using spaces.");
 
 static PyObject *
--- 2637,2649 ----
 }
 
! return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);
 }
 
 
 PyDoc_STRVAR(rjust__doc__,
! "S.rjust(width[, fillchar]) -> string\n"
 "\n"
 "Return S right justified in a string of length width. Padding is\n"
! "done using the specified fill character (default is a space)");
 
 static PyObject *
***************
*** 2649,2653 ****
 {
 int width;
! if (!PyArg_ParseTuple(args, "i:rjust", &width))
 return NULL;
 
--- 2651,2657 ----
 {
 int width;
! char fillchar = ' ';
! 
! if (!PyArg_ParseTuple(args, "i|c:rjust", &width, &fillchar))
 return NULL;
 
***************
*** 2657,2669 ****
 }
 
! return pad(self, width - PyString_GET_SIZE(self), 0, ' ');
 }
 
 
 PyDoc_STRVAR(center__doc__,
! "S.center(width) -> string\n"
 "\n"
! "Return S centered in a string of length width. Padding is done\n"
! "using spaces.");
 
 static PyObject *
--- 2661,2673 ----
 }
 
! return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);
 }
 
 
 PyDoc_STRVAR(center__doc__,
! "S.center(width[, fillchar]) -> string\n"
 "\n"
! "Return S centered in a string of length width. Padding is\n"
! "done using the specified fill character (default is a space)");
 
 static PyObject *
***************
*** 2672,2677 ****
 int marg, left;
 int width;
 
! if (!PyArg_ParseTuple(args, "i:center", &width))
 return NULL;
 
--- 2676,2682 ----
 int marg, left;
 int width;
+ char fillchar = ' ';
 
! if (!PyArg_ParseTuple(args, "i|c:center", &width, &fillchar))
 return NULL;
 
***************
*** 2684,2688 ****
 left = marg / 2 + (marg & width & 1);
 
! return pad(self, left, marg - left, ' ');
 }
 
--- 2689,2693 ----
 left = marg / 2 + (marg & width & 1);
 
! return pad(self, left, marg - left, fillchar);
 }
 
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.201
retrieving revision 2.202
diff -C2 -d -r2.201 -r2.202
*** unicodeobject.c	24 Oct 2003 14:25:28 -0000	2.201
--- unicodeobject.c	26 Nov 2003 08:21:35 -0000	2.202
***************
*** 4405,4413 ****
 #endif
 
 PyDoc_STRVAR(center__doc__,
! "S.center(width) -> unicode\n\
 \n\
! Return S centered in a Unicode string of length width. Padding is done\n\
! using spaces.");
 
 static PyObject *
--- 4405,4440 ----
 #endif
 
+ /* Argument converter. Coerces to a single unicode character */
+ 
+ static int
+ convert_uc(PyObject *obj, void *addr)
+ {
+ 	Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;
+ 	PyObject *uniobj;
+ 	Py_UNICODE *unistr;
+ 
+ 	uniobj = PyUnicode_FromObject(obj);
+ 	if (uniobj == NULL) {
+ 		PyErr_SetString(PyExc_TypeError,
+ 			"The fill character cannot be converted to Unicode");
+ 		return 0;
+ 	}
+ 	if (PyUnicode_GET_SIZE(uniobj) != 1) {
+ 		PyErr_SetString(PyExc_TypeError,
+ 			"The fill character must be exactly one character long");
+ 		Py_DECREF(uniobj);
+ 		return 0;
+ 	}
+ 	unistr = PyUnicode_AS_UNICODE(uniobj);
+ 	*fillcharloc = unistr[0];
+ 	Py_DECREF(uniobj);
+ 	return 1;
+ }
+ 
 PyDoc_STRVAR(center__doc__,
! "S.center(width[, fillchar]) -> unicode\n\
 \n\
! Return S centered in a Unicode string of length width. Padding is\n\
! done using the specified fill character (default is a space)");
 
 static PyObject *
***************
*** 4416,4421 ****
 int marg, left;
 int width;
 
! if (!PyArg_ParseTuple(args, "i:center", &width))
 return NULL;
 
--- 4443,4449 ----
 int marg, left;
 int width;
+ Py_UNICODE fillchar = ' ';
 
! if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar))
 return NULL;
 
***************
*** 4428,4432 ****
 left = marg / 2 + (marg & width & 1);
 
! return (PyObject*) pad(self, left, marg - left, ' ');
 }
 
--- 4456,4460 ----
 left = marg / 2 + (marg & width & 1);
 
! return (PyObject*) pad(self, left, marg - left, fillchar);
 }
 
***************
*** 5171,5178 ****
 
 PyDoc_STRVAR(ljust__doc__,
! "S.ljust(width) -> unicode\n\
 \n\
 Return S left justified in a Unicode string of length width. Padding is\n\
! done using spaces.");
 
 static PyObject *
--- 5199,5206 ----
 
 PyDoc_STRVAR(ljust__doc__,
! "S.ljust(width[, fillchar]) -> unicode\n\
 \n\
 Return S left justified in a Unicode string of length width. Padding is\n\
! done using the specified fill character (default is a space).");
 
 static PyObject *
***************
*** 5180,5184 ****
 {
 int width;
! if (!PyArg_ParseTuple(args, "i:ljust", &width))
 return NULL;
 
--- 5208,5214 ----
 {
 int width;
! Py_UNICODE fillchar = ' ';
! 
! if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar))
 return NULL;
 
***************
*** 5188,5192 ****
 }
 
! return (PyObject*) pad(self, 0, width - self->length, ' ');
 }
 
--- 5218,5222 ----
 }
 
! return (PyObject*) pad(self, 0, width - self->length, fillchar);
 }
 
***************
*** 5553,5560 ****
 
 PyDoc_STRVAR(rjust__doc__,
! "S.rjust(width) -> unicode\n\
 \n\
 Return S right justified in a Unicode string of length width. Padding is\n\
! done using spaces.");
 
 static PyObject *
--- 5583,5590 ----
 
 PyDoc_STRVAR(rjust__doc__,
! "S.rjust(width[, fillchar]) -> unicode\n\
 \n\
 Return S right justified in a Unicode string of length width. Padding is\n\
! done using the specified fill character (default is a space).");
 
 static PyObject *
***************
*** 5562,5566 ****
 {
 int width;
! if (!PyArg_ParseTuple(args, "i:rjust", &width))
 return NULL;
 
--- 5592,5598 ----
 {
 int width;
! Py_UNICODE fillchar = ' ';
! 
! if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar))
 return NULL;
 
***************
*** 5570,5574 ****
 }
 
! return (PyObject*) pad(self, width - self->length, 0, ' ');
 }
 
--- 5602,5606 ----
 }
 
! return (PyObject*) pad(self, width - self->length, 0, fillchar);
 }
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /