summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2002-04-15 13:36:47 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2002-04-15 13:36:47 (GMT)
commit068325ef926538a30d7feb13f9b14a6163e24b6e (patch)
treec2eef78d030be1ce7a56b56420ba13a853aab57d /Objects
parentb384c7263928cc2d8dd16cac8537839673cd4982 (diff)
downloadcpython-068325ef926538a30d7feb13f9b14a6163e24b6e.zip
cpython-068325ef926538a30d7feb13f9b14a6163e24b6e.tar.gz
cpython-068325ef926538a30d7feb13f9b14a6163e24b6e.tar.bz2
Apply the second version of SF patch http://www.python.org/sf/536241
Add a method zfill to str, unicode and UserString and change Lib/string.py accordingly. This activates the zfill version in unicodeobject.c that was commented out and implements the same in stringobject.c. It also adds the test for unicode support in Lib/string.py back in and uses repr() instead() of str() (as it was before Lib/string.py 1.62)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/stringobject.c40
-rw-r--r--Objects/unicodeobject.c7
2 files changed, 44 insertions, 3 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 709c5f7..54ccb0d 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -2381,6 +2381,45 @@ string_center(PyStringObject *self, PyObject *args)
return pad(self, left, marg - left, ' ');
}
+static char zfill__doc__[] =
+"S.zfill(width) -> string\n"
+"\n"
+"Pad a numeric string S with zeros on the left, to fill a field\n"
+"of the specified width. The string S is never truncated.";
+
+static PyObject *
+string_zfill(PyStringObject *self, PyObject *args)
+{
+ int fill;
+ PyObject *s;
+ const char *p;
+
+ int width;
+ if (!PyArg_ParseTuple(args, "i:zfill", &width))
+ return NULL;
+
+ if (PyString_GET_SIZE(self) >= width) {
+ Py_INCREF(self);
+ return (PyObject*) self;
+ }
+
+ fill = width - PyString_GET_SIZE(self);
+
+ s = pad(self, fill, 0, '0');
+
+ if (s == NULL)
+ return NULL;
+
+ p = PyString_AS_STRING(s);
+ if (p[fill] == '+' || p[fill] == '-') {
+ /* move sign to beginning of string */
+ p[0] = p[fill];
+ p[fill] = '0';
+ }
+
+ return (PyObject*) s;
+}
+
static char isspace__doc__[] =
"S.isspace() -> bool\n"
"\n"
@@ -2728,6 +2767,7 @@ string_methods[] = {
{"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__},
{"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__},
{"center", (PyCFunction)string_center, METH_VARARGS, center__doc__},
+ {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__},
{"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__},
{"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__},
{"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, expandtabs__doc__},
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 7d917bd..361612b 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -4824,7 +4824,6 @@ unicode_upper(PyUnicodeObject *self)
return fixup(self, fixupper);
}
-#if 0
static char zfill__doc__[] =
"S.zfill(width) -> unicode\n\
\n\
@@ -4850,6 +4849,9 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
u = pad(self, fill, 0, '0');
+ if (u == NULL)
+ return NULL;
+
if (u->str[fill] == '+' || u->str[fill] == '-') {
/* move sign to beginning of string */
u->str[0] = u->str[fill];
@@ -4858,7 +4860,6 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
return (PyObject*) u;
}
-#endif
#if 0
static PyObject*
@@ -4970,8 +4971,8 @@ static PyMethodDef unicode_methods[] = {
{"isnumeric", (PyCFunction) unicode_isnumeric, METH_NOARGS, isnumeric__doc__},
{"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
{"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
-#if 0
{"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
+#if 0
{"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
#endif