Mercurial > cpython-lru_cache-weakref
diff Objects/stringobject.c @ 23219:b08b51dce264 2.2
Backport the following changes:
Misc/NEWS 1.387->1.388
Lib/test/string_tests.py 1.10->1.11, 1.12->1.14,
Lib/test/test_unicode.py 1.50->1.51, 1.53->1.54, 1.55->1.56
Lib/test/test_string.py 1.15->1.16
Lib/string.py 1.61->1.63
Lib/test/test_userstring.py 1.5->1.6, 1.11, 1.12
Objects/stringobject.c 2.156->2.159
Objects/unicodeobject.c 2.137->2.139
Doc/lib/libstdtypes.tec 1.87->1.88
Add a method zfill to str, unicode and UserString
and change Lib/string.py accordingly
(see SF patch http://www.python.org/sf/536241)
This also adds Guido's fix to test_userstring.py
and the subinstance checks in test_string.py
and test_unicode.py.
| author | Walter Dörwald <walter@livinglogic.de> |
|---|---|
| date | Mon, 22 Apr 2002 11:57:06 +0000 |
| parents | a575e6ee85d9 |
| children | dd350b639309 |
line wrap: on
line diff
--- a/Objects/stringobject.c Mon Apr 22 11:21:59 2002 +0000 +++ b/Objects/stringobject.c Mon Apr 22 11:57:06 2002 +0000 @@ -1,4 +1,3 @@ - /* String object implementation */ #include "Python.h" @@ -605,7 +604,8 @@ /* figure out which quote to use; single is preferred */ quote = '\''; - if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) + if (strchr(op->ob_sval, '\'') && + !strchr(op->ob_sval, '"')) quote = '"'; fputc(quote, fp); @@ -649,7 +649,8 @@ /* figure out which quote to use; single is preferred */ quote = '\''; - if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) + if (strchr(op->ob_sval, '\'') && + !strchr(op->ob_sval, '"')) quote = '"'; p = PyString_AS_STRING(v); @@ -2414,6 +2415,52 @@ 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; + char *p; + + int width; + if (!PyArg_ParseTuple(args, "i:zfill", &width)) + return NULL; + + if (PyString_GET_SIZE(self) >= width) { + if (PyString_CheckExact(self)) { + Py_INCREF(self); + return (PyObject*) self; + } + else + return PyString_FromStringAndSize( + PyString_AS_STRING(self), + PyString_GET_SIZE(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() -> int\n" "\n" @@ -2743,9 +2790,11 @@ {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, - {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, capitalize__doc__}, + {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, + capitalize__doc__}, {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, - {"endswith", (PyCFunction)string_endswith, METH_VARARGS, endswith__doc__}, + {"endswith", (PyCFunction)string_endswith, METH_VARARGS, + endswith__doc__}, {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, @@ -2753,21 +2802,24 @@ {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, - {"startswith", (PyCFunction)string_startswith, METH_VARARGS, startswith__doc__}, + {"startswith", (PyCFunction)string_startswith, METH_VARARGS, + startswith__doc__}, {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, - {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, swapcase__doc__}, - {"translate", (PyCFunction)string_translate, METH_VARARGS, translate__doc__}, + {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, + swapcase__doc__}, + {"translate", (PyCFunction)string_translate, METH_VARARGS, + translate__doc__}, {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, {"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__}, - {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, splitlines__doc__}, -#if 0 - {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, -#endif + {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, + expandtabs__doc__}, + {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, + splitlines__doc__}, {NULL, NULL} /* sentinel */ }; @@ -3262,7 +3314,8 @@ char *pbuf; int sign; int len; - char formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ + char formatbuf[FORMATBUFLEN]; + /* For format{float,int,char}() */ #ifdef Py_USING_UNICODE char *fmt_start = fmt; int argidx_start = argidx; @@ -3460,7 +3513,8 @@ } else { pbuf = formatbuf; - len = formatint(pbuf, sizeof(formatbuf), + len = formatint(pbuf, + sizeof(formatbuf), flags, prec, c, v); if (len < 0) goto error; @@ -3476,7 +3530,8 @@ case 'g': case 'G': pbuf = formatbuf; - len = formatfloat(pbuf, sizeof(formatbuf), flags, prec, c, v); + len = formatfloat(pbuf, sizeof(formatbuf), + flags, prec, c, v); if (len < 0) goto error; sign = 1;
