[Python-checkins] python/dist/src/Objects stringobject.c,2.156,2.157 unicodeobject.c,2.137,2.138

doerwalter@sourceforge.net doerwalter@sourceforge.net
Mon, 15 Apr 2002 06:36:49 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv32693/Objects

Modified Files:
	stringobject.c unicodeobject.c 
Log Message:
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)


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.156
retrieving revision 2.157
diff -C2 -d -r2.156 -r2.157
*** stringobject.c	13 Apr 2002 00:56:08 -0000	2.156
--- stringobject.c	15 Apr 2002 13:36:47 -0000	2.157
***************
*** 2382,2385 ****
--- 2382,2424 ----
  }
  
+ 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"
***************
*** 2729,2732 ****
--- 2768,2772 ----
  	{"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__},

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.137
retrieving revision 2.138
diff -C2 -d -r2.137 -r2.138
*** unicodeobject.c	12 Apr 2002 03:07:20 -0000	2.137
--- unicodeobject.c	15 Apr 2002 13:36:47 -0000	2.138
***************
*** 4825,4829 ****
  }
  
- #if 0
  static char zfill__doc__[] =
  "S.zfill(width) -> unicode\n\
--- 4825,4828 ----
***************
*** 4851,4854 ****
--- 4850,4856 ----
      u = pad(self, fill, 0, '0');
  
+     if (u == NULL)
+         return NULL;
+ 
      if (u->str[fill] == '+' || u->str[fill] == '-') {
          /* move sign to beginning of string */
***************
*** 4859,4863 ****
      return (PyObject*) u;
  }
- #endif
  
  #if 0
--- 4861,4864 ----
***************
*** 4971,4976 ****
      {"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__},
      {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
  #endif
--- 4972,4977 ----
      {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
      {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
      {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
+ #if 0
      {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
  #endif