[Python-checkins] python/dist/src/Objects stringobject.c,2.147,2.147.6.1

anthonybaxter@sourceforge.net anthonybaxter@sourceforge.net
Wed, 17 Apr 2002 22:16:40 -0700


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

Modified Files:
      Tag: release22-maint
	stringobject.c 
Log Message:
backport gvanrossum's patch:

Partially implement SF feature request 444708.

Add optional arg to string methods strip(), lstrip(), rstrip().
The optional arg specifies characters to delete.

Also for UserString.

Still to do:

- Misc/NEWS
- LaTeX docs (I did the docstrings though)
- Unicode methods, and Unicode support in the string methods.




Original patches were:
python/dist/src/Objects/stringobject.c:2.156


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.147
retrieving revision 2.147.6.1
diff -C2 -d -r2.147 -r2.147.6.1
*** stringobject.c	10 Dec 2001 15:45:54 -0000	2.147
--- stringobject.c	18 Apr 2002 05:16:37 -0000	2.147.6.1
***************
*** 1036,1039 ****
--- 1036,1042 ----
  #define BOTHSTRIP 2
  
+ /* Arrays indexed by above */
+ static const char *stripname[] = {"lstrip", "rstrip", "strip"};
+ 
  
  static PyObject *
***************
*** 1411,1414 ****
--- 1414,1450 ----
  
  static PyObject *
+ do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
+ {
+ 	char *s = PyString_AS_STRING(self);
+ 	int len = PyString_GET_SIZE(self);
+ 	char *sep = PyString_AS_STRING(sepobj);
+ 	int seplen = PyString_GET_SIZE(sepobj);
+ 	int i, j;
+ 
+ 	i = 0;
+ 	if (striptype != RIGHTSTRIP) {
+ 		while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
+ 			i++;
+ 		}
+ 	}
+ 
+ 	j = len;
+ 	if (striptype != LEFTSTRIP) {
+ 		do {
+ 			j--;
+ 		} while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
+ 		j++;
+ 	}
+ 
+ 	if (i == 0 && j == len && PyString_CheckExact(self)) {
+ 		Py_INCREF(self);
+ 		return (PyObject*)self;
+ 	}
+ 	else
+ 		return PyString_FromStringAndSize(s+i, j-i);
+ }
+ 
+ 
+ static PyObject *
  do_strip(PyStringObject *self, int striptype)
  {
***************
*** 1440,1477 ****
  
  
  static char strip__doc__[] =
! "S.strip() -> string\n\
  \n\
  Return a copy of the string S with leading and trailing\n\
! whitespace removed.";
  
  static PyObject *
! string_strip(PyStringObject *self)
  {
! 	return do_strip(self, BOTHSTRIP);
  }
  
  
  static char lstrip__doc__[] =
! "S.lstrip() -> string\n\
  \n\
! Return a copy of the string S with leading whitespace removed.";
  
  static PyObject *
! string_lstrip(PyStringObject *self)
  {
! 	return do_strip(self, LEFTSTRIP);
  }
  
  
  static char rstrip__doc__[] =
! "S.rstrip() -> string\n\
  \n\
! Return a copy of the string S with trailing whitespace removed.";
  
  static PyObject *
! string_rstrip(PyStringObject *self)
  {
! 	return do_strip(self, RIGHTSTRIP);
  }
  
--- 1476,1548 ----
  
  
+ static PyObject *
+ do_argstrip(PyStringObject *self, int striptype, PyObject *args)
+ {
+ 	PyObject *sep = NULL;
+ 
+ 	if (!PyArg_ParseTuple(args, "|O:[lr]strip", &sep))
+ 		return NULL;
+ 
+ 	if (sep != NULL && sep != Py_None) {
+ 		/* XXX What about Unicode? */
+ 		if (!PyString_Check(sep)) {
+ 			PyErr_Format(PyExc_TypeError,
+ 				     "%s arg must be None or string",
+ 				     stripname[striptype]);
+ 			return NULL;
+ 		}
+ 		return do_xstrip(self, striptype, sep);
+ 	}
+ 
+ 	return do_strip(self, striptype);
+ }
+ 
+ 
  static char strip__doc__[] =
! "S.strip([sep]) -> string\n\
  \n\
  Return a copy of the string S with leading and trailing\n\
! whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.";
  
  static PyObject *
! string_strip(PyStringObject *self, PyObject *args)
  {
! 	if (PyTuple_GET_SIZE(args) == 0)
! 		return do_strip(self, BOTHSTRIP); /* Common case */
! 	else
! 		return do_argstrip(self, BOTHSTRIP, args);
  }
  
  
  static char lstrip__doc__[] =
! "S.lstrip([sep]) -> string\n\
  \n\
! Return a copy of the string S with leading whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.";
  
  static PyObject *
! string_lstrip(PyStringObject *self, PyObject *args)
  {
! 	if (PyTuple_GET_SIZE(args) == 0)
! 		return do_strip(self, LEFTSTRIP); /* Common case */
! 	else
! 		return do_argstrip(self, LEFTSTRIP, args);
  }
  
  
  static char rstrip__doc__[] =
! "S.rstrip([sep]) -> string\n\
  \n\
! Return a copy of the string S with trailing whitespace removed.\n\
! If sep is given and not None, remove characters in sep instead.";
  
  static PyObject *
! string_rstrip(PyStringObject *self, PyObject *args)
  {
! 	if (PyTuple_GET_SIZE(args) == 0)
! 		return do_strip(self, RIGHTSTRIP); /* Common case */
! 	else
! 		return do_argstrip(self, RIGHTSTRIP, args);
  }
  
***************
*** 2678,2688 ****
  	{"find",       (PyCFunction)string_find,        METH_VARARGS, find__doc__},
  	{"index",      (PyCFunction)string_index,       METH_VARARGS, index__doc__},
! 	{"lstrip",     (PyCFunction)string_lstrip,      METH_NOARGS, lstrip__doc__},
  	{"replace",     (PyCFunction)string_replace,    METH_VARARGS, replace__doc__},
  	{"rfind",       (PyCFunction)string_rfind,      METH_VARARGS, rfind__doc__},
  	{"rindex",      (PyCFunction)string_rindex,     METH_VARARGS, rindex__doc__},
! 	{"rstrip",      (PyCFunction)string_rstrip,     METH_NOARGS, rstrip__doc__},
  	{"startswith",  (PyCFunction)string_startswith, METH_VARARGS, startswith__doc__},
! 	{"strip",       (PyCFunction)string_strip,      METH_NOARGS, strip__doc__},
  	{"swapcase",    (PyCFunction)string_swapcase,   METH_NOARGS, swapcase__doc__},
  	{"translate",   (PyCFunction)string_translate,  METH_VARARGS, translate__doc__},
--- 2749,2759 ----
  	{"find",       (PyCFunction)string_find,        METH_VARARGS, find__doc__},
  	{"index",      (PyCFunction)string_index,       METH_VARARGS, index__doc__},
! 	{"lstrip",     (PyCFunction)string_lstrip,      METH_VARARGS, lstrip__doc__},
  	{"replace",     (PyCFunction)string_replace,    METH_VARARGS, replace__doc__},
  	{"rfind",       (PyCFunction)string_rfind,      METH_VARARGS, rfind__doc__},
  	{"rindex",      (PyCFunction)string_rindex,     METH_VARARGS, rindex__doc__},
! 	{"rstrip",      (PyCFunction)string_rstrip,     METH_VARARGS, rstrip__doc__},
  	{"startswith",  (PyCFunction)string_startswith, METH_VARARGS, startswith__doc__},
! 	{"strip",       (PyCFunction)string_strip,      METH_VARARGS, strip__doc__},
  	{"swapcase",    (PyCFunction)string_swapcase,   METH_NOARGS, swapcase__doc__},
  	{"translate",   (PyCFunction)string_translate,  METH_VARARGS, translate__doc__},