[Python-checkins] python/dist/src/Objects stringobject.c,2.174,2.175 unicodeobject.c,2.157,2.158

bwarsaw@users.sourceforge.net bwarsaw@users.sourceforge.net
Tue, 06 Aug 2002 09:58:23 -0700


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

Modified Files:
	stringobject.c unicodeobject.c 
Log Message:
Committing patch #591250 which provides "str1 in str2" when str1 is a
string of longer than 1 character.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.174
retrieving revision 2.175
diff -C2 -d -r2.174 -r2.175
*** stringobject.c	5 Aug 2002 06:28:21 -0000	2.174
--- stringobject.c	6 Aug 2002 16:58:21 -0000	2.175
***************
*** 804,825 ****
  string_contains(PyObject *a, PyObject *el)
  {
! 	register char *s, *end;
! 	register char c;
  #ifdef Py_USING_UNICODE
  	if (PyUnicode_Check(el))
  		return PyUnicode_Contains(a, el);
  #endif
! 	if (!PyString_Check(el) || PyString_Size(el) != 1) {
  		PyErr_SetString(PyExc_TypeError,
! 		    "'in <string>' requires character as left operand");
  		return -1;
  	}
! 	c = PyString_AsString(el)[0];
! 	s = PyString_AsString(a);
! 	end = s + PyString_Size(a);
! 	while (s < end) {
! 		if (c == *s++)
  			return 1;
  	}
  	return 0;
  }
--- 804,832 ----
  string_contains(PyObject *a, PyObject *el)
  {
! 	const char *lhs, *rhs, *end;
! 	int size;
  #ifdef Py_USING_UNICODE
  	if (PyUnicode_Check(el))
  		return PyUnicode_Contains(a, el);
  #endif
! 	if (!PyString_Check(el)) {
  		PyErr_SetString(PyExc_TypeError,
! 			      "'in <string>' requires string as left operand");
  		return -1;
  	}
! 	size = PyString_Size(el);
! 	rhs = PyString_AS_STRING(el);
! 	lhs = PyString_AS_STRING(a);
! 
! 	/* optimize for a single character */
! 	if (size == 1)
! 		return memchr(lhs, *rhs, PyString_Size(a)) != NULL;
! 
! 	end = lhs + (PyString_Size(a) - size);
! 	while (lhs <= end) {
! 		if (memcmp(lhs++, rhs, size) == 0)
  			return 1;
  	}
+ 
  	return 0;
  }

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.157
retrieving revision 2.158
diff -C2 -d -r2.157 -r2.158
*** unicodeobject.c	26 Jul 2002 16:22:46 -0000	2.157
--- unicodeobject.c	6 Aug 2002 16:58:21 -0000	2.158
***************
*** 3733,3739 ****
  {
      PyUnicodeObject *u = NULL, *v = NULL;
!     int result;
!     register const Py_UNICODE *p, *e;
!     register Py_UNICODE ch;
  
      /* Coerce the two arguments */
--- 3733,3738 ----
  {
      PyUnicodeObject *u = NULL, *v = NULL;
!     int result, size;
!     register const Py_UNICODE *lhs, *end, *rhs;
  
      /* Coerce the two arguments */
***************
*** 3741,3745 ****
      if (v == NULL) {
  	PyErr_SetString(PyExc_TypeError,
! 	    "'in <string>' requires character as left operand");
  	goto onError;
      }
--- 3740,3744 ----
      if (v == NULL) {
  	PyErr_SetString(PyExc_TypeError,
! 	    "'in <string>' requires string as left operand");
  	goto onError;
      }
***************
*** 3750,3767 ****
      }
  
!     /* Check v in u */
!     if (PyUnicode_GET_SIZE(v) != 1) {
! 	PyErr_SetString(PyExc_TypeError,
! 	    "'in <string>' requires character as left operand");
! 	goto onError;
!     }
!     ch = *PyUnicode_AS_UNICODE(v);
!     p = PyUnicode_AS_UNICODE(u);
!     e = p + PyUnicode_GET_SIZE(u);
      result = 0;
!     while (p < e) {
! 	if (*p++ == ch) {
! 	    result = 1;
! 	    break;
  	}
      }
--- 3749,3773 ----
      }
  
!     size = PyUnicode_GET_SIZE(v);
!     rhs = PyUnicode_AS_UNICODE(v);
!     lhs = PyUnicode_AS_UNICODE(u);
! 
      result = 0;
!     if (size == 1) {
! 	end = lhs + PyUnicode_GET_SIZE(u);
! 	while (lhs < end) {
! 	    if (*lhs++ == *rhs) {
! 		result = 1;
! 		break;
! 	    }
! 	}
!     }
!     else {
! 	end = lhs + (PyUnicode_GET_SIZE(u) - size);
! 	while (lhs <= end) {
! 	    if (memcmp(lhs++, rhs, size) == 0) {
! 		result = 1;
! 		break;
! 	    }
  	}
      }