? diff.txt Index: Doc/lib/libstdtypes.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstdtypes.tex,v retrieving revision 1.86 diff -u -r1.86 libstdtypes.tex --- Doc/lib/libstdtypes.tex 12 Apr 2002 15:11:59 -0000 1.86 +++ Doc/lib/libstdtypes.tex 12 Apr 2002 18:35:46 -0000 @@ -682,6 +682,12 @@ Return a copy of the string converted to uppercase. \end{methoddesc} +\begin{methoddesc}[string]{zfill}{width} +Return the numeric string left filled with zeros in a string +of length \var{width}. The original string is returned if +\var{width} is less than \code{len(\var{s})}. +\end{methoddesc} + \subsubsection{String Formatting Operations \label{typesseq-strings}} Index: Lib/UserString.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/UserString.py,v retrieving revision 1.10 diff -u -r1.10 UserString.py --- Lib/UserString.py 15 May 2001 11:58:05 -0000 1.10 +++ Lib/UserString.py 12 Apr 2002 18:35:47 -0000 @@ -128,6 +128,7 @@ def translate(self, *args): return self.__class__(self.data.translate(*args)) def upper(self): return self.__class__(self.data.upper()) + def zfill(self, width): return self.__class__(self.data.zfill(width)) class MutableString(UserString): """mutable string objects Index: Lib/string.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/string.py,v retrieving revision 1.62 diff -u -r1.62 string.py --- Lib/string.py 29 Mar 2002 16:20:33 -0000 1.62 +++ Lib/string.py 12 Apr 2002 18:35:47 -0000 @@ -190,7 +190,10 @@ _float = float _int = int _long = long -_StringTypes = (str, unicode) +try: + _StringTypes = (str, unicode) +except NameError: + _StringTypes = (str,) # Convert string to float def atof(s): @@ -277,13 +280,8 @@ """ if not isinstance(x, _StringTypes): - x = str(x) - n = len(x) - if n >= width: return x - sign = '' - if x[0] in '-+': - sign, x = x[0], x[1:] - return sign + '0'*(width-n) + x + x = repr(x) + return x.zfill(width) # Expand tabs in a string. # Doesn't take non-printing chars into account, but does understand \n. Index: Lib/test/string_tests.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/string_tests.py,v retrieving revision 1.11 diff -u -r1.11 string_tests.py --- Lib/test/string_tests.py 29 Mar 2002 16:00:13 -0000 1.11 +++ Lib/test/string_tests.py 12 Apr 2002 18:35:48 -0000 @@ -215,6 +215,16 @@ test('endswith', 'ab', 0, 'ab', 0, 1) test('endswith', 'ab', 0, 'ab', 0, 0) + test('zfill', '123', '123', 2) + test('zfill', '123', '123', 3) + test('zfill', '123', '0123', 4) + test('zfill', '+123', '+123', 3) + test('zfill', '+123', '+123', 4) + test('zfill', '+123', '+0123', 5) + test('zfill', '-123', '-123', 3) + test('zfill', '-123', '-123', 4) + test('zfill', '-123', '-0123', 5) + test('zfill', '', '000', 3) test('zfill', '34', '34', 1) test('zfill', '34', '0034', 4) Index: Objects/stringobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v retrieving revision 2.155 diff -u -r2.155 stringobject.c --- Objects/stringobject.c 12 Apr 2002 03:05:19 -0000 2.155 +++ Objects/stringobject.c 12 Apr 2002 18:35:53 -0000 @@ -2310,6 +2310,45 @@ 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" @@ -2657,6 +2696,7 @@ {"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__}, Index: Objects/unicodeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v retrieving revision 2.137 diff -u -r2.137 unicodeobject.c --- Objects/unicodeobject.c 12 Apr 2002 03:07:20 -0000 2.137 +++ Objects/unicodeobject.c 12 Apr 2002 18:35:55 -0000 @@ -4824,7 +4824,6 @@ return fixup(self, fixupper); } -#if 0 static char zfill__doc__[] = "S.zfill(width) -> unicode\n\ \n\ @@ -4850,6 +4849,9 @@ 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 @@ return (PyObject*) u; } -#endif #if 0 static PyObject* @@ -4970,8 +4971,8 @@ {"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