[Python-checkins] CVS: python/dist/src/Objects stringobject.c,2.130,2.131

Tim Peters tim_one@users.sourceforge.net
Tue, 11 Sep 2001 19:18:32 -0700


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

Modified Files:
	stringobject.c 
Log Message:
More bug 460020:  lots of string optimizations inhibited for string
subclasses, all "the usual" ones (slicing etc), plus replace, translate,
ljust, rjust, center and strip.  I don't know how to be sure they've all
been caught.

Question:  Should we complain if someone tries to intern an instance of
a string subclass?  I hate to slow any code on those paths.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.130
retrieving revision 2.131
diff -C2 -d -r2.130 -r2.131
*** stringobject.c	2001/09/11 01:41:59	2.130
--- stringobject.c	2001/09/12 02:18:30	2.131
***************
*** 674,682 ****
  #define b ((PyStringObject *)bb)
  	/* Optimize cases with empty left or right operand */
! 	if (a->ob_size == 0) {
! 		Py_INCREF(bb);
! 		return bb;
! 	}
! 	if (b->ob_size == 0) {
  		Py_INCREF(a);
  		return (PyObject *)a;
--- 674,683 ----
  #define b ((PyStringObject *)bb)
  	/* Optimize cases with empty left or right operand */
! 	if ((a->ob_size == 0 || b->ob_size == 0) &&
! 	    PyString_CheckExact(a) && PyString_CheckExact(b)) {
! 		if (a->ob_size == 0) {
! 			Py_INCREF(bb);
! 			return bb;
! 		}
  		Py_INCREF(a);
  		return (PyObject *)a;
***************
*** 720,724 ****
  		return NULL;
  	}
! 	if (size == a->ob_size) {
  		Py_INCREF(a);
  		return (PyObject *)a;
--- 721,725 ----
  		return NULL;
  	}
! 	if (size == a->ob_size && PyString_CheckExact(a)) {
  		Py_INCREF(a);
  		return (PyObject *)a;
***************
*** 760,764 ****
  	if (j > a->ob_size)
  		j = a->ob_size;
! 	if (i == 0 && j == a->ob_size) { /* It's the same as a */
  		Py_INCREF(a);
  		return (PyObject *)a;
--- 761,766 ----
  	if (j > a->ob_size)
  		j = a->ob_size;
! 	if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) {
! 		/* It's the same as a */
  		Py_INCREF(a);
  		return (PyObject *)a;
***************
*** 1379,1383 ****
  	}
  
! 	if (i == 0 && j == len) {
  		Py_INCREF(self);
  		return (PyObject*)self;
--- 1381,1385 ----
  	}
  
! 	if (i == 0 && j == len && PyString_CheckExact(self)) {
  		Py_INCREF(self);
  		return (PyObject*)self;
***************
*** 1736,1740 ****
  				changed = 1;
  		}
! 		if (changed)
  			return result;
  		Py_DECREF(result);
--- 1738,1742 ----
  				changed = 1;
  		}
! 		if (changed || !PyString_CheckExact(input_obj))
  			return result;
  		Py_DECREF(result);
***************
*** 1756,1760 ****
  		changed = 1;
  	}
! 	if (!changed) {
  		Py_DECREF(result);
  		Py_INCREF(input_obj);
--- 1758,1762 ----
  		changed = 1;
  	}
! 	if (!changed && PyString_CheckExact(input_obj)) {
  		Py_DECREF(result);
  		Py_INCREF(input_obj);
***************
*** 1918,1922 ****
  	const char *str = PyString_AS_STRING(self), *sub, *repl;
  	char *new_s;
! 	int len = PyString_GET_SIZE(self), sub_len, repl_len, out_len;
  	int count = -1;
  	PyObject *new;
--- 1920,1925 ----
  	const char *str = PyString_AS_STRING(self), *sub, *repl;
  	char *new_s;
! 	const int len = PyString_GET_SIZE(self);
! 	int sub_len, repl_len, out_len;
  	int count = -1;
  	PyObject *new;
***************
*** 1961,1967 ****
  	}
  	if (out_len == -1) {
! 		/* we're returning another reference to self */
! 		new = (PyObject*)self;
! 		Py_INCREF(new);
  	}
  	else {
--- 1964,1977 ----
  	}
  	if (out_len == -1) {
! 		if (PyString_CheckExact(self)) {
! 			/* we're returning another reference to self */
! 			new = (PyObject*)self;
! 			Py_INCREF(new);
! 		}
! 		else {
! 			new = PyString_FromStringAndSize(str, len);
! 			if (new == NULL)
! 				return NULL;
! 		}
  	}
  	else {
***************
*** 2183,2191 ****
  }
  
! static
! PyObject *pad(PyStringObject *self,
! 	      int left,
! 	      int right,
! 	      char fill)
  {
      PyObject *u;
--- 2193,2198 ----
  }
  
! static PyObject *
! pad(PyStringObject *self, int left, int right, char fill)
  {
      PyObject *u;
***************
*** 2196,2200 ****
          right = 0;
  
!     if (left == 0 && right == 0) {
          Py_INCREF(self);
          return (PyObject *)self;
--- 2203,2207 ----
          right = 0;
  
!     if (left == 0 && right == 0 && PyString_CheckExact(self)) {
          Py_INCREF(self);
          return (PyObject *)self;
***************
*** 2218,2225 ****
  
  static char ljust__doc__[] =
! "S.ljust(width) -> string\n\
! \n\
! Return S left justified in a string of length width. Padding is\n\
! done using spaces.";
  
  static PyObject *
--- 2225,2232 ----
  
  static char ljust__doc__[] =
! "S.ljust(width) -> string\n"
! "\n"
! "Return S left justified in a string of length width. Padding is\n"
! "done using spaces.";
  
  static PyObject *
***************
*** 2230,2234 ****
          return NULL;
  
!     if (PyString_GET_SIZE(self) >= width) {
          Py_INCREF(self);
          return (PyObject*) self;
--- 2237,2241 ----
          return NULL;
  
!     if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
          Py_INCREF(self);
          return (PyObject*) self;
***************
*** 2240,2247 ****
  
  static char rjust__doc__[] =
! "S.rjust(width) -> string\n\
! \n\
! Return S right justified in a string of length width. Padding is\n\
! done using spaces.";
  
  static PyObject *
--- 2247,2254 ----
  
  static char rjust__doc__[] =
! "S.rjust(width) -> string\n"
! "\n"
! "Return S right justified in a string of length width. Padding is\n"
! "done using spaces.";
  
  static PyObject *
***************
*** 2252,2256 ****
          return NULL;
  
!     if (PyString_GET_SIZE(self) >= width) {
          Py_INCREF(self);
          return (PyObject*) self;
--- 2259,2263 ----
          return NULL;
  
!     if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
          Py_INCREF(self);
          return (PyObject*) self;
***************
*** 2262,2269 ****
  
  static char center__doc__[] =
! "S.center(width) -> string\n\
! \n\
! Return S centered in a string of length width. Padding is done\n\
! using spaces.";
  
  static PyObject *
--- 2269,2276 ----
  
  static char center__doc__[] =
! "S.center(width) -> string\n"
! "\n"
! "Return S centered in a string of length width. Padding is done\n"
! "using spaces.";
  
  static PyObject *
***************
*** 2276,2280 ****
          return NULL;
  
!     if (PyString_GET_SIZE(self) >= width) {
          Py_INCREF(self);
          return (PyObject*) self;
--- 2283,2287 ----
          return NULL;
  
!     if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
          Py_INCREF(self);
          return (PyObject*) self;
***************
*** 2287,2335 ****
  }
  
- #if 0
- static char zfill__doc__[] =
- "S.zfill(width) -> string\n\
- \n\
- Pad a numeric string x with zeros on the left, to fill a field\n\
- of the specified width. The string x is never truncated.";
- 
- static PyObject *
- string_zfill(PyStringObject *self, PyObject *args)
- {
-     int fill;
-     PyObject *u;
-     char *str;
- 
-     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);
- 
-     u = pad(self, fill, 0, '0');
-     if (u == NULL)
- 	return NULL;
- 
-     str = PyString_AS_STRING(u);
-     if (str[fill] == '+' || str[fill] == '-') {
-         /* move sign to beginning of string */
-         str[0] = str[fill];
-         str[fill] = '0';
-     }
- 
-     return u;
- }
- #endif
- 
  static char isspace__doc__[] =
! "S.isspace() -> int\n\
! \n\
! Return 1 if there are only whitespace characters in S,\n\
! 0 otherwise.";
  
  static PyObject*
--- 2294,2302 ----
  }
  
  static char isspace__doc__[] =
! "S.isspace() -> int\n"
! "\n"
! "Return 1 if there are only whitespace characters in S,\n"
! "0 otherwise.";
  
  static PyObject*