]> granicus.if.org Git - python/commitdiff
Apply the second version of SF patch http://www.python.org/sf/536241
authorWalter Dörwald <walter@livinglogic.de>
Mon, 15 Apr 2002 13:36:47 +0000 (13:36 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Mon, 15 Apr 2002 13:36:47 +0000 (13:36 +0000)
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)

Doc/lib/libstdtypes.tex
Lib/UserString.py
Lib/string.py
Lib/test/string_tests.py
Lib/test/test_unicode.py
Misc/NEWS
Objects/stringobject.c
Objects/unicodeobject.c

index 6f819e959e4cfdb7a3a591105eda024399efc2bf..e7275cec14e800e1ad5e8e731fec78fe32ec243b 100644 (file)
@@ -694,6 +694,12 @@ must be a string of length 256.
 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 f4f5cab96ced91508bd9c77e6a9de604ba47215b..292e85242d8a8424fa276ffa79bf5e14542dc2ee 100755 (executable)
@@ -128,6 +128,7 @@ class UserString:
     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 d68b0bf3491ad59d569e7a2df01c6b5af975376c..cd9909e2661721c1a53277f1e82968a98901e4fc 100644 (file)
@@ -190,7 +190,10 @@ def rfind(s, *args):
 _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 @@ def zfill(x, width):
 
     """
     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 5c8dd934c6b74b7553d03d17272de5b77d16757f..ea25983e76a1f6428bcd56d38f12dd5f0971919c 100644 (file)
@@ -227,6 +227,16 @@ def run_method_tests(test):
     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 4b77e7531e33368646b98647d1a2a54db4acc073..29dd819bfacaadc0d1d52d82401a0d17b7fcc025 100644 (file)
@@ -206,8 +206,18 @@ if 0:
     test('capwords', u'abc\tdef\nghi', u'Abc Def Ghi')
     test('capwords', u'abc\t   def  \nghi', u'Abc Def Ghi')
 
-verify(string.zfill(u'34', 1) == u'34')
-verify(string.zfill(u'34', 5) == u'00034')
+test('zfill', u'123', u'123', 2)
+test('zfill', u'123', u'123', 3)
+test('zfill', u'123', u'0123', 4)
+test('zfill', u'+123', u'+123', 3)
+test('zfill', u'+123', u'+123', 4)
+test('zfill', u'+123', u'+0123', 5)
+test('zfill', u'-123', u'-123', 3)
+test('zfill', u'-123', u'-123', 4)
+test('zfill', u'-123', u'-0123', 5)
+test('zfill', u'', u'000', 3)
+test('zfill', u'34', u'34', 1)
+test('zfill', u'34', u'00034', 5)
 
 # Comparisons:
 print 'Testing Unicode comparisons...',
index 400b63161e3290bfbe1c99226a30b82531cd1c0b..a1f5f54155ce1e1fe176e252f644128c922713d9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -6,6 +6,10 @@ Type/class unification and new-style classes
 
 Core and builtins
 
+- A method zfill() was added to str and unicode, that fills a numeric
+  string to the left with zeros.  For example,
+  "+123".zfill(6) -> "+00123".
+
 - Complex numbers supported divmod() and the // and % operators, but
   these make no sense.  Since this was documented, they're being
   deprecated now.
index 709c5f79864d3767305e46c38a634a7386b13938..54ccb0d6f26df13dcd7239164c5fdc275683fa72 100644 (file)
@@ -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__},
index 7d917bd4538b6d0d97446eeb02f8d2fa7ee7409f..361612b16f0ceeafd6f800230434253e1b73932c 100644 (file)
@@ -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