? stWEREI3 ? stP3iP7P ? platform ? patch.txt ? Doc/x Index: Doc/lib/libstdtypes.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstdtypes.tex,v retrieving revision 1.58 diff -u -r1.58 libstdtypes.tex --- Doc/lib/libstdtypes.tex 2001/05/03 04:39:10 1.58 +++ Doc/lib/libstdtypes.tex 2001/05/16 18:21:30 @@ -558,8 +558,10 @@ Return a copy of the string converted to lowercase. \end{methoddesc} -\begin{methoddesc}[string]{lstrip}{} -Return a copy of the string with leading whitespace removed. +\begin{methoddesc}[string]{lstrip}{\optional{str}} +Return a copy of the string with leading occurences of +\var{str} removed. If \var{str} is not specified or None all +leading whitespace will be removed. \end{methoddesc} \begin{methoddesc}[string]{replace}{old, new\optional{, maxsplit}} @@ -587,8 +589,10 @@ \var{width} is less than \code{len(\var{s})}. \end{methoddesc} -\begin{methoddesc}[string]{rstrip}{} -Return a copy of the string with trailing whitespace removed. +\begin{methoddesc}[string]{rstrip}{\optional{str}} +Return a copy of the string with trailing occurences of +\var{str} removed. If \var{str} is not specified or None +all trailing whitespace will be removed. \end{methoddesc} \begin{methoddesc}[string]{split}{\optional{sep \optional{,maxsplit}}} @@ -611,9 +615,10 @@ position. \end{methoddesc} -\begin{methoddesc}[string]{strip}{} -Return a copy of the string with leading and trailing whitespace -removed. +\begin{methoddesc}[string]{strip}{\optional{str}} +Return a copy of the string with leading and trailing occurences of +\var{str} removed. If \var{str} is not specified or None all leading +and trailing whitespace will be removed. \end{methoddesc} \begin{methoddesc}[string]{swapcase}{} Index: Doc/lib/libstring.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstring.tex,v retrieving revision 1.43 diff -u -r1.43 libstring.tex --- Doc/lib/libstring.tex 2001/05/10 15:05:03 1.43 +++ Doc/lib/libstring.tex 2001/05/16 18:21:30 @@ -213,17 +213,22 @@ \function{joinfields()} was only used with two arguments.) \end{funcdesc} -\begin{funcdesc}{lstrip}{s} +\begin{funcdesc}{lstrip}{s\optional{, str}} Return a copy of \var{s} but without leading whitespace characters. + If \var{str} is specified and not \code{None} a copy of \var{s} without + leading occurrences of \var{str} will be returned. \end{funcdesc} -\begin{funcdesc}{rstrip}{s} +\begin{funcdesc}{rstrip}{s\optional{, str}} Return a copy of \var{s} but without trailing whitespace - characters. + characters. If \var{str} is specified and not \code{None} + a copy without trailing occurrences of \var{str} will be returned. \end{funcdesc} -\begin{funcdesc}{strip}{s} +\begin{funcdesc}{strip}{s\optional{, str}} Return a copy of \var{s} without leading or trailing whitespace. + If \var{str} is specified and not \code{None} a copy without + leading and trailing occurrences of \var{str} will be returned. \end{funcdesc} \begin{funcdesc}{swapcase}{s} Index: Include/stringobject.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/stringobject.h,v retrieving revision 2.26 diff -u -r2.26 stringobject.h --- Include/stringobject.h 2001/05/15 11:58:05 2.26 +++ Include/stringobject.h 2001/05/16 18:21:30 @@ -157,7 +157,16 @@ (only possible for 0-terminated strings) */ ); - + +/* Strip leading and/or trailing whilespace or all occurences of + another string from str and return the resulting string object; + if strip is a Unicode object, a Unicode object will be returned. */ +extern DL_IMPORT(PyObject *) PyString_Strip( + PyObject *str, /* String */ + PyObject *strip, /* String to be stripped off (or NULL/None for whitespace) */ + int left, /* strip leading occurences? */ + int right /* strip trailing occurences? */ + ); #ifdef __cplusplus } Index: Include/unicodeobject.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/unicodeobject.h,v retrieving revision 2.20 diff -u -r2.20 unicodeobject.h --- Include/unicodeobject.h 2001/04/23 14:44:21 2.20 +++ Include/unicodeobject.h 2001/05/16 18:21:31 @@ -814,6 +814,15 @@ -1 = all */ ); +/* Strip leading and/or trailing whilespace or all occurences of + another string from str and return the resulting Unicode object */ +extern DL_IMPORT(PyObject *) PyUnicode_Strip( + PyObject *str, /* String */ + PyObject *strip, /* String to be stripped off (or NULL/None for whitespace) */ + int left, /* strip leading occurences? */ + int right /* strip trailing occurences? */ + ); + /* Compare two strings and return -1, 0, 1 for less than, equal, greater than resp. */ Index: Lib/UserString.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/UserString.py,v retrieving revision 1.10 diff -u -r1.10 UserString.py --- Lib/UserString.py 2001/05/15 11:58:05 1.10 +++ Lib/UserString.py 2001/05/16 18:21:31 @@ -108,7 +108,7 @@ def join(self, seq): return self.data.join(seq) def ljust(self, width): return self.__class__(self.data.ljust(width)) def lower(self): return self.__class__(self.data.lower()) - def lstrip(self): return self.__class__(self.data.lstrip()) + def lstrip(self, str=None): return self.__class__(self.data.lstrip(str)) def replace(self, old, new, maxsplit=-1): return self.__class__(self.data.replace(old, new, maxsplit)) def rfind(self, sub, start=0, end=sys.maxint): @@ -116,13 +116,13 @@ def rindex(self, sub, start=0, end=sys.maxint): return self.data.rindex(sub, start, end) def rjust(self, width): return self.__class__(self.data.rjust(width)) - def rstrip(self): return self.__class__(self.data.rstrip()) + def rstrip(self, str=None): return self.__class__(self.data.rstrip(str)) def split(self, sep=None, maxsplit=-1): return self.data.split(sep, maxsplit) def splitlines(self, keepends=0): return self.data.splitlines(keepends) def startswith(self, prefix, start=0, end=sys.maxint): return self.data.startswith(prefix, start, end) - def strip(self): return self.__class__(self.data.strip()) + def strip(self, str=None): return self.__class__(self.data.strip(str)) def swapcase(self): return self.__class__(self.data.swapcase()) def title(self): return self.__class__(self.data.title()) def translate(self, *args): Index: Lib/string.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/string.py,v retrieving revision 1.59 diff -u -r1.59 string.py --- Lib/string.py 2001/02/18 03:30:53 1.59 +++ Lib/string.py 2001/05/16 18:21:31 @@ -69,34 +69,38 @@ """ return s.swapcase() -# Strip leading and trailing tabs and spaces -def strip(s): - """strip(s) -> string +# Strip leading and trailing tabs and spaces (or other strings) +def strip(s, str=None): + """strip(s [,str]) -> string Return a copy of the string s with leading and trailing - whitespace removed. + whitespace removed. If str is specified and not None + leading and trailing occurrences of str will be removed. """ - return s.strip() + return s.strip(str) -# Strip leading tabs and spaces -def lstrip(s): - """lstrip(s) -> string +# Strip leading tabs and spaces (or other strings) +def lstrip(s, str=None): + """lstrip(s [,str]) -> string + + Return a copy of the string s with leading whitespace + removed. If str is specified and not None leading + occurences of str will be removed. - Return a copy of the string s with leading whitespace removed. - """ - return s.lstrip() + return s.lstrip(str) -# Strip trailing tabs and spaces -def rstrip(s): - """rstrip(s) -> string +# Strip trailing tabs and spaces (or other strings) +def rstrip(s, str=None): + """rstrip(s [,str]) -> string Return a copy of the string s with trailing whitespace - removed. + removed. If str is specified and not None trailing + occurrences of str will be removed. """ - return s.rstrip() + return s.rstrip(str) # Split a string into a list of space/tab-separated words Index: Lib/test/string_tests.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/string_tests.py,v retrieving revision 1.9 diff -u -r1.9 string_tests.py --- Lib/test/string_tests.py 2001/05/15 11:58:06 1.9 +++ Lib/test/string_tests.py 2001/05/16 18:21:31 @@ -157,9 +157,30 @@ test('split', 'a b c d', ['a', 'b', 'c d'], None, 2) test('split', 'a b c d ', ['a', 'b', 'c', 'd']) + test('strip', ' hello ', TypeError, 42) test('strip', ' hello ', 'hello') + test('strip', ' hello ', 'hello', None) + test('strip', ' hello ', ValueError, '') + test('strip', 'xxxhelloxxx', 'hello', 'x') + test('strip', 'xxxhelloxxx', u'hello', u'x') + test('strip', 'xxxhelloxxx', 'xhellox', 'xx') + test('strip', 'xyxhelloxyx', 'xhelloxyx', 'xy') + test('lstrip', ' hello ', TypeError, 42) test('lstrip', ' hello ', 'hello ') + test('lstrip', ' hello ', ValueError, '') + test('lstrip', ' hello ', 'hello ', None) + test('lstrip', 'xxxhelloxxx', 'helloxxx', 'x') + test('lstrip', 'xxxhelloxxx', u'helloxxx', u'x') + test('lstrip', 'xxxhelloxxx', 'xhelloxxx', 'xx') + test('lstrip', 'xyxhelloxyx', 'xhelloxyx', 'xy') + test('rstrip', ' hello ', TypeError, 42) test('rstrip', ' hello ', ' hello') + test('rstrip', ' hello ', ValueError, '') + test('rstrip', ' hello ', ' hello', None) + test('rstrip', 'xxxhelloxxx', 'xxxhello', 'x') + test('rstrip', 'xxxhelloxxx', u'xxxhello', u'x') + test('rstrip', 'xxxhelloxxx', 'xxxhellox', 'xx') + test('rstrip', 'xyxhelloxyx', 'xyxhellox', 'yx') test('strip', 'hello', 'hello') test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS') Index: Lib/test/test_unicode.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_unicode.py,v retrieving revision 1.33 diff -u -r1.33 test_unicode.py --- Lib/test/test_unicode.py 2001/05/13 00:19:31 1.33 +++ Lib/test/test_unicode.py 2001/05/16 18:21:31 @@ -110,9 +110,27 @@ test('join', u':', result, [u'x' * 10] * 10) test('join', u':', result, (u'x' * 10,) * 10) +test('strip', u' hello ', TypeError, 42) test('strip', u' hello ', u'hello') +test('strip', u' hello ', u'hello', None) +test('strip', u' hello ', ValueError, u'') +test('strip', u'xxxhelloxxx', u'hello', u'x') +test('strip', u'xxxhelloxxx', u'xhellox', u'xx') +test('strip', u'xyxyxhelloxyxy', u'xhello', u'xy') +test('lstrip', u' hello ', TypeError, 42) test('lstrip', u' hello ', u'hello ') +test('lstrip', u' hello ', ValueError, u'') +test('lstrip', u' hello ', u'hello ', None) +test('lstrip', u'xxxhelloxxx', u'helloxxx', u'x') +test('lstrip', u'xxxhelloxxx', u'xhelloxxx', u'xx') +test('lstrip', u'xyxyxhelloxyxyx', u'xhelloxyxyx', u'xy') +test('rstrip', u' hello ', TypeError, 42) test('rstrip', u' hello ', u' hello') +test('rstrip', u' hello ', ValueError, u'') +test('rstrip', u' hello ', u' hello', None) +test('rstrip', u'xxxhelloxxx', u'xxxhello', u'x') +test('rstrip', u'xxxhelloxxx', u'xxxhellox', u'xx') +test('rstrip', u'xyxyxhelloxyxyx', u'xyxyxhellox', u'yx') test('strip', u'hello', u'hello') test('swapcase', u'HeLLo cOmpUteRs', u'hEllO CoMPuTErS') Index: Objects/stringobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v retrieving revision 2.116 diff -u -r2.116 stringobject.c --- Objects/stringobject.c 2001/05/15 11:58:06 2.116 +++ Objects/stringobject.c 2001/05/16 18:21:32 @@ -1103,28 +1103,59 @@ } -static PyObject * -do_strip(PyStringObject *self, PyObject *args, int striptype) +PyObject * +PyString_Strip(PyObject *self, PyObject *strip, int left, int right) { char *s = PyString_AS_STRING(self); int len = PyString_GET_SIZE(self), i, j; - if (!PyArg_ParseTuple(args, ":strip")) - return NULL; + char *stripthis = NULL; + int stripthislen = 0; + + if (strip != NULL) { + if (PyUnicode_Check(strip)) + return PyUnicode_Strip((PyObject *)self, strip, left, right); + else if (PyString_Check(strip)) { + stripthis = PyString_AS_STRING(strip); + stripthislen = PyString_GET_SIZE(strip); + + if (stripthislen==0) { + PyErr_SetString(PyExc_ValueError, "empty strip string"); + return NULL; + } + } + else if (strip != Py_None) { + PyErr_SetString(PyExc_TypeError, "strip string must be string, unicode or None"); + return NULL; + } + } i = 0; - if (striptype != RIGHTSTRIP) { - while (i < len && isspace(Py_CHARMASK(s[i]))) { - i++; + if (left) { + if (stripthis == NULL) { + while (i < len && isspace(Py_CHARMASK(s[i]))) + i++; } + else { + while (i+stripthislen < len && + !memcmp(s+i, stripthis, stripthislen)) + i += stripthislen; + } } j = len; - if (striptype != LEFTSTRIP) { - do { - j--; - } while (j >= i && isspace(Py_CHARMASK(s[j]))); - j++; + if (right) { + if (stripthis == NULL) { + do { + j--; + } while (j >= i && isspace(Py_CHARMASK(s[j]))); + j++; + } + else { + while (j >= i+stripthislen && + !memcmp(s+j-stripthislen, stripthis, stripthislen)) + j -= stripthislen; + } } if (i == 0 && j == len) { @@ -1137,39 +1168,53 @@ static char strip__doc__[] = -"S.strip() -> string\n\ +"S.strip([str]) -> string\n\ \n\ Return a copy of the string S with leading and trailing\n\ -whitespace removed."; +whitespace removed. If str is specified and not empty\n\ +leading and trailing occurrences of str will be removed."; static PyObject * string_strip(PyStringObject *self, PyObject *args) { - return do_strip(self, args, BOTHSTRIP); + PyObject *strip = NULL; + if (!PyArg_ParseTuple(args, "|O:strip", &strip)) + return NULL; + return PyString_Strip((PyObject *)self, strip, 1, 1); } static char lstrip__doc__[] = -"S.lstrip() -> string\n\ +"S.lstrip([str]) -> string\n\ \n\ -Return a copy of the string S with leading whitespace removed."; +Return a copy of the string S with leading whitespace removed.\n\ +If str is specified and not empty leading occurences of str\n\ +will be removed."; static PyObject * string_lstrip(PyStringObject *self, PyObject *args) { - return do_strip(self, args, LEFTSTRIP); + PyObject *strip = NULL; + if (!PyArg_ParseTuple(args, "|O:lstrip", &strip)) + return NULL; + return PyString_Strip((PyObject *)self, strip, 1, 0); } static char rstrip__doc__[] = -"S.rstrip() -> string\n\ +"S.rstrip([str]) -> string\n\ \n\ -Return a copy of the string S with trailing whitespace removed."; +Return a copy of the string S with trailing whitespace removed.\n\ +If str is specified and not empty trailing occurences of str\n\ +will be removed."; static PyObject * string_rstrip(PyStringObject *self, PyObject *args) { - return do_strip(self, args, RIGHTSTRIP); + PyObject *strip = NULL; + if (!PyArg_ParseTuple(args, "|O:rstrip", &strip)) + return NULL; + return PyString_Strip((PyObject *)self, strip, 0, 1); } Index: Objects/unicodeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v retrieving revision 2.90 diff -u -r2.90 unicodeobject.c --- Objects/unicodeobject.c 2001/05/08 04:00:45 2.90 +++ Objects/unicodeobject.c 2001/05/16 18:21:34 @@ -3018,22 +3018,46 @@ return split_substring(self,list,substring,maxcount); } -static PyObject *strip(PyUnicodeObject *self, + PyUnicodeObject *strip, int left, int right) { Py_UNICODE *p = self->str; int start = 0; int end = self->length; + Py_UNICODE *stripthis = NULL; + int stripthislen = 0; + + if (strip != NULL) { + stripthis = PyUnicode_AS_UNICODE(strip); + stripthislen = PyUnicode_GET_SIZE(strip); + + if (stripthislen==0) { + PyErr_SetString(PyExc_ValueError, "empty strip string"); + return NULL; + } + } - if (left) - while (start < end && Py_UNICODE_ISSPACE(p[start])) - start++; - - if (right) - while (end > start && Py_UNICODE_ISSPACE(p[end-1])) - end--; + if (left) { + if (stripthis == NULL) /* strip whitespace */ + while (start < end && Py_UNICODE_ISSPACE(p[start])) + start++; + else /* strip string */ + while (start+stripthislen < end && + !memcmp(p+start, stripthis, stripthislen*sizeof(Py_UNICODE))) + start += stripthislen; + } + + if (right) { + if (stripthis == NULL) /* strip whitespace */ + while (end > start && Py_UNICODE_ISSPACE(p[end-1])) + end--; + else /* strip string */ + while (end >= start+stripthislen && + !memcmp(p+end-stripthislen, stripthis, stripthislen*sizeof(Py_UNICODE))) + end -= stripthislen; + } if (start == 0 && end == self->length) { /* couldn't strip anything off, return original string */ @@ -4016,16 +4040,19 @@ } static char lstrip__doc__[] = -"S.lstrip() -> unicode\n\ +"S.lstrip([str]) -> unicode\n\ \n\ -Return a copy of the string S with leading whitespace removed."; +Return a copy of the string S with leading occurrences of\n\ +str removed. If str is not specified or empty all leading\n\ +whitespace will be removed."; static PyObject * unicode_lstrip(PyUnicodeObject *self, PyObject *args) { - if (!PyArg_NoArgs(args)) + PyObject *strip = NULL; + if (!PyArg_ParseTuple(args, "|O:lstrip", &strip)) return NULL; - return strip(self, 1, 0); + return PyUnicode_Strip((PyObject *)self, strip, 1, 0); } static PyObject* @@ -4074,6 +4101,35 @@ return (PyObject*) u; } +PyObject *PyUnicode_Strip(PyObject *obj, + PyObject *stripobj, + int left, + int right) +{ + PyObject *self; + PyObject *stripstr = NULL; + PyObject *result; + + self = PyUnicode_FromObject(obj); + if (self == NULL) + return NULL; + if (stripobj != NULL && stripobj != Py_None) { + stripstr = PyUnicode_FromObject(stripobj); + if (stripstr == NULL) { + Py_DECREF(self); + return NULL; + } + } + result = strip((PyUnicodeObject *)self, + (PyUnicodeObject *)stripstr, + left, + right); + Py_DECREF(self); + Py_XDECREF(stripstr); + return result; +} + + PyObject *PyUnicode_Replace(PyObject *obj, PyObject *subobj, PyObject *replobj, @@ -4231,16 +4287,19 @@ } static char rstrip__doc__[] = -"S.rstrip() -> unicode\n\ +"S.rstrip([str]) -> unicode\n\ \n\ -Return a copy of the string S with trailing whitespace removed."; +Return a copy of the string S with trailing occurrences of\n\ +str removed. If str is not specified or empty all trailing\n\ +whitespace will be removed."; static PyObject * unicode_rstrip(PyUnicodeObject *self, PyObject *args) { - if (!PyArg_NoArgs(args)) + PyObject *strip = NULL; + if (!PyArg_ParseTuple(args, "|O:rstrip", &strip)) return NULL; - return strip(self, 0, 1); + return PyUnicode_Strip((PyObject *)self, strip, 0, 1); } static PyObject* @@ -4339,16 +4398,19 @@ } static char strip__doc__[] = -"S.strip() -> unicode\n\ +"S.strip([str]) -> unicode\n\ \n\ -Return a copy of S with leading and trailing whitespace removed."; +Return a copy of the string S with leading and trailing occurrences\n\ +of str removed. If str is not specified or empty all leading and\n\ +trailing whitespace will be removed."; static PyObject * unicode_strip(PyUnicodeObject *self, PyObject *args) { - if (!PyArg_NoArgs(args)) + PyObject *strip = NULL; + if (!PyArg_ParseTuple(args, "|O:strip", &strip)) return NULL; - return strip(self, 1, 1); + return PyUnicode_Strip((PyObject *)self, strip, 1, 1); } static char swapcase__doc__[] = @@ -4525,14 +4587,14 @@ {"index", (PyCFunction) unicode_index, 1, index__doc__}, {"ljust", (PyCFunction) unicode_ljust, 1, ljust__doc__}, {"lower", (PyCFunction) unicode_lower, 0, lower__doc__}, - {"lstrip", (PyCFunction) unicode_lstrip, 0, lstrip__doc__}, + {"lstrip", (PyCFunction) unicode_lstrip, 1, lstrip__doc__}, /* {"maketrans", (PyCFunction) unicode_maketrans, 1, maketrans__doc__}, */ {"rfind", (PyCFunction) unicode_rfind, 1, rfind__doc__}, {"rindex", (PyCFunction) unicode_rindex, 1, rindex__doc__}, {"rjust", (PyCFunction) unicode_rjust, 1, rjust__doc__}, - {"rstrip", (PyCFunction) unicode_rstrip, 0, rstrip__doc__}, + {"rstrip", (PyCFunction) unicode_rstrip, 1, rstrip__doc__}, {"splitlines", (PyCFunction) unicode_splitlines, 1, splitlines__doc__}, - {"strip", (PyCFunction) unicode_strip, 0, strip__doc__}, + {"strip", (PyCFunction) unicode_strip, 1, strip__doc__}, {"swapcase", (PyCFunction) unicode_swapcase, 0, swapcase__doc__}, {"translate", (PyCFunction) unicode_translate, 1, translate__doc__}, {"upper", (PyCFunction) unicode_upper, 0, upper__doc__},