[Python-checkins] python/dist/src/Objects stringobject.c, 2.216, 2.217

perky at users.sourceforge.net perky at users.sourceforge.net
Sun Jan 4 19:29:54 EST 2004


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

Modified Files:
	stringobject.c 
Log Message:
[SF #866875] Add a specialized routine for one character
separaters on str.split() and str.rsplit().


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.216
retrieving revision 2.217
diff -C2 -d -r2.216 -r2.217
*** stringobject.c	22 Dec 2003 16:31:41 -0000	2.216
--- stringobject.c	5 Jan 2004 00:29:51 -0000	2.217
***************
*** 1283,1292 ****
  #define STRIPNAME(i) (stripformat[i]+3)
  
  
  static PyObject *
  split_whitespace(const char *s, int len, int maxsplit)
  {
! 	int i, j, err;
! 	PyObject* item;
  	PyObject *list = PyList_New(0);
  
--- 1283,1315 ----
  #define STRIPNAME(i) (stripformat[i]+3)
  
+ #define SPLIT_APPEND(data, left, right)				\
+ 	str = PyString_FromStringAndSize((data) + (left),	\
+ 					 (right) - (left));	\
+ 	if (str == NULL)					\
+ 		goto onError;					\
+ 	if (PyList_Append(list, str)) {				\
+ 		Py_DECREF(str);					\
+ 		goto onError;					\
+ 	}							\
+ 	else							\
+ 		Py_DECREF(str);
+ 
+ #define SPLIT_INSERT(data, left, right)			 	\
+ 	str = PyString_FromStringAndSize((data) + (left),	\
+ 					 (right) - (left));	\
+ 	if (str == NULL)					\
+ 		goto onError;					\
+ 	if (PyList_Insert(list, 0, str)) {			\
+ 		Py_DECREF(str);					\
+ 		goto onError;					\
+ 	}							\
+ 	else							\
+ 		Py_DECREF(str);
  
  static PyObject *
  split_whitespace(const char *s, int len, int maxsplit)
  {
! 	int i, j;
! 	PyObject *str;
  	PyObject *list = PyList_New(0);
  
***************
*** 1303,1313 ****
  			if (maxsplit-- <= 0)
  				break;
! 			item = PyString_FromStringAndSize(s+j, (int)(i-j));
! 			if (item == NULL)
! 				goto finally;
! 			err = PyList_Append(list, item);
! 			Py_DECREF(item);
! 			if (err < 0)
! 				goto finally;
  			while (i < len && isspace(Py_CHARMASK(s[i])))
  				i++;
--- 1326,1330 ----
  			if (maxsplit-- <= 0)
  				break;
! 			SPLIT_APPEND(s, j, i);
  			while (i < len && isspace(Py_CHARMASK(s[i])))
  				i++;
***************
*** 1316,1333 ****
  	}
  	if (j < len) {
! 		item = PyString_FromStringAndSize(s+j, (int)(len - j));
! 		if (item == NULL)
! 			goto finally;
! 		err = PyList_Append(list, item);
! 		Py_DECREF(item);
! 		if (err < 0)
! 			goto finally;
  	}
  	return list;
!   finally:
  	Py_DECREF(list);
  	return NULL;
  }
  
  
  PyDoc_STRVAR(split__doc__,
--- 1333,1372 ----
  	}
  	if (j < len) {
! 		SPLIT_APPEND(s, j, len);
  	}
  	return list;
!   onError:
  	Py_DECREF(list);
  	return NULL;
  }
  
+ static PyObject *
+ split_char(const char *s, int len, char ch, int maxcount)
+ {
+ 	register int i, j;
+ 	PyObject *str;
+ 	PyObject *list = PyList_New(0);
+ 
+ 	if (list == NULL)
+ 		return NULL;
+ 
+ 	for (i = j = 0; i < len; ) {
+ 		if (s[i] == ch) {
+ 			if (maxcount-- <= 0)
+ 				break;
+ 			SPLIT_APPEND(s, j, i);
+ 			i = j = i + 1;
+ 		} else
+ 			i++;
+ 	}
+ 	if (j <= len) {
+ 		SPLIT_APPEND(s, j, len);
+ 	}
+ 	return list;
+ 
+   onError:
+ 	Py_DECREF(list);
+ 	return NULL;
+ }
  
  PyDoc_STRVAR(split__doc__,
***************
*** 1363,1370 ****
--- 1402,1412 ----
  	else if (PyObject_AsCharBuffer(subobj, &sub, &n))
  		return NULL;
+ 
  	if (n == 0) {
  		PyErr_SetString(PyExc_ValueError, "empty separator");
  		return NULL;
  	}
+ 	else if (n == 1)
+ 		return split_char(s, len, sub[0], maxsplit);
  
  	list = PyList_New(0);
***************
*** 1407,1412 ****
  rsplit_whitespace(const char *s, int len, int maxsplit)
  {
! 	int i, j, err;
! 	PyObject* item;
  	PyObject *list = PyList_New(0);
  
--- 1449,1454 ----
  rsplit_whitespace(const char *s, int len, int maxsplit)
  {
! 	int i, j;
! 	PyObject *str;
  	PyObject *list = PyList_New(0);
  
***************
*** 1423,1433 ****
  			if (maxsplit-- <= 0)
  				break;
! 			item = PyString_FromStringAndSize(s+i+1, (int)(j-i));
! 			if (item == NULL)
! 				goto finally;
! 			err = PyList_Insert(list, 0, item);
! 			Py_DECREF(item);
! 			if (err < 0)
! 				goto finally;
  			while (i >= 0 && isspace(Py_CHARMASK(s[i])))
  				i--;
--- 1465,1469 ----
  			if (maxsplit-- <= 0)
  				break;
! 			SPLIT_INSERT(s, i + 1, j + 1);
  			while (i >= 0 && isspace(Py_CHARMASK(s[i])))
  				i--;
***************
*** 1436,1453 ****
  	}
  	if (j >= 0) {
! 		item = PyString_FromStringAndSize(s, (int)(j + 1));
! 		if (item == NULL)
! 			goto finally;
! 		err = PyList_Insert(list, 0, item);
! 		Py_DECREF(item);
! 		if (err < 0)
! 			goto finally;
  	}
  	return list;
!   finally:
  	Py_DECREF(list);
  	return NULL;
  }
  
  
  PyDoc_STRVAR(rsplit__doc__,
--- 1472,1511 ----
  	}
  	if (j >= 0) {
! 		SPLIT_INSERT(s, 0, j + 1);
  	}
  	return list;
!   onError:
  	Py_DECREF(list);
  	return NULL;
  }
  
+ static PyObject *
+ rsplit_char(const char *s, int len, char ch, int maxcount)
+ {
+ 	register int i, j;
+ 	PyObject *str;
+ 	PyObject *list = PyList_New(0);
+ 
+ 	if (list == NULL)
+ 		return NULL;
+ 
+ 	for (i = j = len - 1; i >= 0; ) {
+ 		if (s[i] == ch) {
+ 			if (maxcount-- <= 0)
+ 				break;
+ 			SPLIT_INSERT(s, i + 1, j + 1);
+ 			j = i = i - 1;
+ 		} else
+ 			i--;
+ 	}
+ 	if (j >= -1) {
+ 		SPLIT_INSERT(s, 0, j + 1);
+ 	}
+ 	return list;
+ 
+  onError:
+ 	Py_DECREF(list);
+ 	return NULL;
+ }
  
  PyDoc_STRVAR(rsplit__doc__,
***************
*** 1484,1491 ****
--- 1542,1552 ----
  	else if (PyObject_AsCharBuffer(subobj, &sub, &n))
  		return NULL;
+ 
  	if (n == 0) {
  		PyErr_SetString(PyExc_ValueError, "empty separator");
  		return NULL;
  	}
+ 	else if (n == 1)
+ 		return rsplit_char(s, len, sub[0], maxsplit);
  
  	list = PyList_New(0);
***************
*** 3104,3118 ****
  Line breaks are not included in the resulting list unless keepends\n\
  is given and true.");
- 
- #define SPLIT_APPEND(data, left, right)					\
- 	str = PyString_FromStringAndSize(data + left, right - left);	\
- 	if (!str)							\
- 	    goto onError;						\
- 	if (PyList_Append(list, str)) {					\
- 	    Py_DECREF(str);						\
- 	    goto onError;						\
- 	}								\
-         else								\
-             Py_DECREF(str);
  
  static PyObject*
--- 3165,3168 ----





More information about the Python-checkins mailing list