diff options
author | Raymond Hettinger <python@rcn.com> | 2003-11-26 08:21:35 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-11-26 08:21:35 (GMT) |
commit | 4f8f9765766a126ebfff3c81655454821f1ad532 (patch) | |
tree | 90f6c08e42a551b8bb461d191afddaef5e4f0cf4 /Objects | |
parent | bd93b3ea8fd87742dd56c9c1ff514223892e0d4a (diff) | |
download | cpython-4f8f9765766a126ebfff3c81655454821f1ad532.zip cpython-4f8f9765766a126ebfff3c81655454821f1ad532.tar.gz cpython-4f8f9765766a126ebfff3c81655454821f1ad532.tar.bz2 |
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringobject.c | 31 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 58 |
2 files changed, 63 insertions, 26 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 0a22440..739cc3e 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -2617,16 +2617,18 @@ pad(PyStringObject *self, int left, int right, char fill) } PyDoc_STRVAR(ljust__doc__, -"S.ljust(width) -> string\n" +"S.ljust(width[, fillchar]) -> string\n" "\n" "Return S left justified in a string of length width. Padding is\n" -"done using spaces."); +"done using the specified fill character (default is a space)."); static PyObject * string_ljust(PyStringObject *self, PyObject *args) { int width; - if (!PyArg_ParseTuple(args, "i:ljust", &width)) + char fillchar = ' '; + + if (!PyArg_ParseTuple(args, "i|c:ljust", &width, &fillchar)) return NULL; if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { @@ -2634,21 +2636,23 @@ string_ljust(PyStringObject *self, PyObject *args) return (PyObject*) self; } - return pad(self, 0, width - PyString_GET_SIZE(self), ' '); + return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); } PyDoc_STRVAR(rjust__doc__, -"S.rjust(width) -> string\n" +"S.rjust(width[, fillchar]) -> string\n" "\n" "Return S right justified in a string of length width. Padding is\n" -"done using spaces."); +"done using the specified fill character (default is a space)"); static PyObject * string_rjust(PyStringObject *self, PyObject *args) { int width; - if (!PyArg_ParseTuple(args, "i:rjust", &width)) + char fillchar = ' '; + + if (!PyArg_ParseTuple(args, "i|c:rjust", &width, &fillchar)) return NULL; if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { @@ -2656,23 +2660,24 @@ string_rjust(PyStringObject *self, PyObject *args) return (PyObject*) self; } - return pad(self, width - PyString_GET_SIZE(self), 0, ' '); + return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); } PyDoc_STRVAR(center__doc__, -"S.center(width) -> string\n" +"S.center(width[, fillchar]) -> string\n" "\n" -"Return S centered in a string of length width. Padding is done\n" -"using spaces."); +"Return S centered in a string of length width. Padding is\n" +"done using the specified fill character (default is a space)"); static PyObject * string_center(PyStringObject *self, PyObject *args) { int marg, left; int width; + char fillchar = ' '; - if (!PyArg_ParseTuple(args, "i:center", &width)) + if (!PyArg_ParseTuple(args, "i|c:center", &width, &fillchar)) return NULL; if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { @@ -2683,7 +2688,7 @@ string_center(PyStringObject *self, PyObject *args) marg = width - PyString_GET_SIZE(self); left = marg / 2 + (marg & width & 1); - return pad(self, left, marg - left, ' '); + return pad(self, left, marg - left, fillchar); } PyDoc_STRVAR(zfill__doc__, diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index e4fe531..c950f8b 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -4404,19 +4404,47 @@ onError: } #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) -> unicode\n\ +"S.center(width[, fillchar]) -> unicode\n\ \n\ -Return S centered in a Unicode string of length width. Padding is done\n\ -using spaces."); +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 * unicode_center(PyUnicodeObject *self, PyObject *args) { int marg, left; int width; + Py_UNICODE fillchar = ' '; - if (!PyArg_ParseTuple(args, "i:center", &width)) + if (!PyArg_ParseTuple(args, "i|O&:center", &width, convert_uc, &fillchar)) return NULL; if (self->length >= width && PyUnicode_CheckExact(self)) { @@ -4427,7 +4455,7 @@ unicode_center(PyUnicodeObject *self, PyObject *args) marg = width - self->length; left = marg / 2 + (marg & width & 1); - return (PyObject*) pad(self, left, marg - left, ' '); + return (PyObject*) pad(self, left, marg - left, fillchar); } #if 0 @@ -5170,16 +5198,18 @@ unicode_length(PyUnicodeObject *self) } PyDoc_STRVAR(ljust__doc__, -"S.ljust(width) -> unicode\n\ +"S.ljust(width[, fillchar]) -> unicode\n\ \n\ Return S left justified in a Unicode string of length width. Padding is\n\ -done using spaces."); +done using the specified fill character (default is a space)."); static PyObject * unicode_ljust(PyUnicodeObject *self, PyObject *args) { int width; - if (!PyArg_ParseTuple(args, "i:ljust", &width)) + Py_UNICODE fillchar = ' '; + + if (!PyArg_ParseTuple(args, "i|O&:ljust", &width, convert_uc, &fillchar)) return NULL; if (self->length >= width && PyUnicode_CheckExact(self)) { @@ -5187,7 +5217,7 @@ unicode_ljust(PyUnicodeObject *self, PyObject *args) return (PyObject*) self; } - return (PyObject*) pad(self, 0, width - self->length, ' '); + return (PyObject*) pad(self, 0, width - self->length, fillchar); } PyDoc_STRVAR(lower__doc__, @@ -5552,16 +5582,18 @@ unicode_rindex(PyUnicodeObject *self, PyObject *args) } PyDoc_STRVAR(rjust__doc__, -"S.rjust(width) -> unicode\n\ +"S.rjust(width[, fillchar]) -> unicode\n\ \n\ Return S right justified in a Unicode string of length width. Padding is\n\ -done using spaces."); +done using the specified fill character (default is a space)."); static PyObject * unicode_rjust(PyUnicodeObject *self, PyObject *args) { int width; - if (!PyArg_ParseTuple(args, "i:rjust", &width)) + Py_UNICODE fillchar = ' '; + + if (!PyArg_ParseTuple(args, "i|O&:rjust", &width, convert_uc, &fillchar)) return NULL; if (self->length >= width && PyUnicode_CheckExact(self)) { @@ -5569,7 +5601,7 @@ unicode_rjust(PyUnicodeObject *self, PyObject *args) return (PyObject*) self; } - return (PyObject*) pad(self, width - self->length, 0, ' '); + return (PyObject*) pad(self, width - self->length, 0, fillchar); } static PyObject* |