]> granicus.if.org Git - python/commitdiff
Attempt to make all the various string *strip methods the same.
authorNeal Norwitz <nnorwitz@gmail.com>
Thu, 10 Apr 2003 22:35:32 +0000 (22:35 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Thu, 10 Apr 2003 22:35:32 +0000 (22:35 +0000)
 * Doc - add doc for when functions were added
 * UserString
 * string object methods
 * string module functions
'chars' is used for the last parameter everywhere.

These changes will be backported, since part of the changes
have already been made, but they were inconsistent.

Doc/lib/libstring.tex
Lib/UserString.py
Lib/string.py
Lib/test/string_tests.py
Objects/stringobject.c
Objects/unicodeobject.c

index 99bb5eefa35e46158f5b1482967f914c8f9e4e81..1f1a89e9cf6aa14ac0e27d78b42cd621592e167e 100644 (file)
@@ -243,6 +243,8 @@ Return a copy of the string with leading characters removed.  If
 removed.  If given and not \code{None}, \var{chars} must be a string;
 the characters in the string will be stripped from the beginning of
 the string this method is called on.
+\versionchanged[The \var{chars} parameter was added.  The \var{chars}
+parameter cannot be passed in earlier 2.2 versions]{2.2.3}
 \end{funcdesc}
 
 \begin{funcdesc}{rstrip}{s\optional{, chars}}
@@ -251,6 +253,8 @@ Return a copy of the string with trailing characters removed.  If
 removed.  If given and not \code{None}, \var{chars} must be a string;
 the characters in the string will be stripped from the end of the
 string this method is called on.
+\versionchanged[The \var{chars} parameter was added.  The \var{chars}
+parameter cannot be passed in 2.2 versions]{2.2.3}
 \end{funcdesc}
 
 \begin{funcdesc}{strip}{s\optional{, chars}}
@@ -259,6 +263,8 @@ removed.  If \var{chars} is omitted or \code{None}, whitespace
 characters are removed.  If given and not \code{None}, \var{chars}
 must be a string; the characters in the string will be stripped from
 the both ends of the string this method is called on.
+\versionchanged[The \var{chars} parameter was added.  The \var{chars}
+parameter cannot be passed in 2.2 or 2.2.1]{2.2.2}
 \end{funcdesc}
 
 \begin{funcdesc}{swapcase}{s}
index 2819ab0f8f005dce9a59e07eddb4434dca3be7ab..fcd115d005fa27fca33e5e9ec44360b65a958852 100755 (executable)
@@ -99,7 +99,7 @@ class UserString:
     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, sep=None): return self.__class__(self.data.lstrip(sep))
+    def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
     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):
@@ -107,13 +107,13 @@ class UserString:
     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, sep=None): return self.__class__(self.data.rstrip(sep))
+    def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars))
     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, sep=None): return self.__class__(self.data.strip(sep))
+    def strip(self, chars=None): return self.__class__(self.data.strip(chars))
     def swapcase(self): return self.__class__(self.data.swapcase())
     def title(self): return self.__class__(self.data.title())
     def translate(self, *args):
index 021469c78c7bb97bc5de712752e2cf105febbe33..a375249418cc9f467e00db063a7983452d3901db 100644 (file)
@@ -79,30 +79,31 @@ def strip(s, chars=None):
 
     Return a copy of the string s with leading and trailing
     whitespace removed.
-    If chars is given and not None, remove characters in sep instead.
+    If chars is given and not None, remove characters in chars instead.
     If chars is unicode, S will be converted to unicode before stripping.
 
     """
     return s.strip(chars)
 
 # Strip leading tabs and spaces
-def lstrip(s):
-    """lstrip(s) -> string
+def lstrip(s, chars=None):
+    """lstrip(s [,chars]) -> string
 
     Return a copy of the string s with leading whitespace removed.
+    If chars is given and not None, remove characters in chars instead.
 
     """
-    return s.lstrip()
+    return s.lstrip(chars)
 
 # Strip trailing tabs and spaces
-def rstrip(s):
-    """rstrip(s) -> string
+def rstrip(s, chars=None):
+    """rstrip(s [,chars]) -> string
 
-    Return a copy of the string s with trailing whitespace
-    removed.
+    Return a copy of the string s with trailing whitespace removed.
+    If chars is given and not None, remove characters in chars instead.
 
     """
-    return s.rstrip()
+    return s.rstrip(chars)
 
 
 # Split a string into a list of space/tab-separated words
index dcf961d1ff0387056157a51eb1355b1f626b2743..591b409ae3122988d36646b8ab825188fb1b41b5 100644 (file)
@@ -195,6 +195,33 @@ class CommonTest(unittest.TestCase):
         self.checkequal('   hello', '   hello   ', 'rstrip')
         self.checkequal('hello', 'hello', 'strip')
 
+        # strip/lstrip/rstrip with None arg
+        self.checkequal('hello', '   hello   ', 'strip', None)
+        self.checkequal('hello   ', '   hello   ', 'lstrip', None)
+        self.checkequal('   hello', '   hello   ', 'rstrip', None)
+        self.checkequal('hello', 'hello', 'strip', None)
+
+        # strip/lstrip/rstrip with str arg
+        self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
+        self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
+        self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
+        self.checkequal('hello', 'hello', 'strip', 'xyz')
+
+        # strip/lstrip/rstrip with unicode arg
+        if test_support.have_unicode:
+            self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
+                 'strip', unicode('xyz', 'ascii'))
+            self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
+                 'lstrip', unicode('xyz', 'ascii'))
+            self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
+                 'rstrip', unicode('xyz', 'ascii'))
+            self.checkequal(unicode('hello', 'ascii'), 'hello',
+                 'strip', unicode('xyz', 'ascii'))
+
+        self.checkraises(TypeError, 'hello', 'strip', 42, 42)
+        self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
+        self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
+
     def test_ljust(self):
         self.checkequal('abc       ', 'abc', 'ljust', 10)
         self.checkequal('abc   ', 'abc', 'ljust', 6)
@@ -432,34 +459,6 @@ class MixinStrUnicodeUserStringTest:
         self.checkraises(TypeError, 'hello', 'endswith')
         self.checkraises(TypeError, 'hello', 'endswith', 42)
 
-    def test_strip_args(self):
-        # strip/lstrip/rstrip with None arg
-        self.checkequal('hello', '   hello   ', 'strip', None)
-        self.checkequal('hello   ', '   hello   ', 'lstrip', None)
-        self.checkequal('   hello', '   hello   ', 'rstrip', None)
-        self.checkequal('hello', 'hello', 'strip', None)
-
-        # strip/lstrip/rstrip with str arg
-        self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
-        self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
-        self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
-        self.checkequal('hello', 'hello', 'strip', 'xyz')
-
-        # strip/lstrip/rstrip with unicode arg
-        if test_support.have_unicode:
-            self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
-                 'strip', unicode('xyz', 'ascii'))
-            self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
-                 'lstrip', unicode('xyz', 'ascii'))
-            self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
-                 'rstrip', unicode('xyz', 'ascii'))
-            self.checkequal(unicode('hello', 'ascii'), 'hello',
-                 'strip', unicode('xyz', 'ascii'))
-
-        self.checkraises(TypeError, 'hello', 'strip', 42, 42)
-        self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
-        self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
-
     def test___contains__(self):
         self.checkequal(True, '', '__contains__', '')         # vereq('' in '', True)
         self.checkequal(True, 'abc', '__contains__', '')      # vereq('' in 'abc', True)
index ac4ad04f393f02cde16d0e9b483a2b8e6741c539..0425b1b14332a190f74ecb20b2d0b62ba595be54 100644 (file)
@@ -1770,12 +1770,12 @@ do_argstrip(PyStringObject *self, int striptype, PyObject *args)
 
 
 PyDoc_STRVAR(strip__doc__,
-"S.strip([sep]) -> string or unicode\n\
+"S.strip([chars]) -> string or unicode\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.\n\
-If sep is unicode, S will be converted to unicode before stripping");
+If chars is given and not None, remove characters in chars instead.\n\
+If chars is unicode, S will be converted to unicode before stripping");
 
 static PyObject *
 string_strip(PyStringObject *self, PyObject *args)
@@ -1788,11 +1788,11 @@ string_strip(PyStringObject *self, PyObject *args)
 
 
 PyDoc_STRVAR(lstrip__doc__,
-"S.lstrip([sep]) -> string or unicode\n\
+"S.lstrip([chars]) -> string or unicode\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.\n\
-If sep is unicode, S will be converted to unicode before stripping");
+If chars is given and not None, remove characters in chars instead.\n\
+If chars is unicode, S will be converted to unicode before stripping");
 
 static PyObject *
 string_lstrip(PyStringObject *self, PyObject *args)
@@ -1805,11 +1805,11 @@ string_lstrip(PyStringObject *self, PyObject *args)
 
 
 PyDoc_STRVAR(rstrip__doc__,
-"S.rstrip([sep]) -> string or unicode\n\
+"S.rstrip([chars]) -> string or unicode\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.\n\
-If sep is unicode, S will be converted to unicode before stripping");
+If chars is given and not None, remove characters in chars instead.\n\
+If chars is unicode, S will be converted to unicode before stripping");
 
 static PyObject *
 string_rstrip(PyStringObject *self, PyObject *args)
index 847580c064390df9f0acb2db934b66008bdab891..096dfcb7c9afc39059129eb765bca180438f3b8e 100644 (file)
@@ -5247,12 +5247,12 @@ do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args)
 
 
 PyDoc_STRVAR(strip__doc__,
-"S.strip([sep]) -> unicode\n\
+"S.strip([chars]) -> unicode\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.\n\
-If sep is a str, it will be converted to unicode before stripping");
+If chars is given and not None, remove characters in chars instead.\n\
+If chars is a str, it will be converted to unicode before stripping");
 
 static PyObject *
 unicode_strip(PyUnicodeObject *self, PyObject *args)
@@ -5265,11 +5265,11 @@ unicode_strip(PyUnicodeObject *self, PyObject *args)
 
 
 PyDoc_STRVAR(lstrip__doc__,
-"S.lstrip([sep]) -> unicode\n\
+"S.lstrip([chars]) -> unicode\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.\n\
-If sep is a str, it will be converted to unicode before stripping");
+If chars is given and not None, remove characters in chars instead.\n\
+If chars is a str, it will be converted to unicode before stripping");
 
 static PyObject *
 unicode_lstrip(PyUnicodeObject *self, PyObject *args)
@@ -5282,11 +5282,11 @@ unicode_lstrip(PyUnicodeObject *self, PyObject *args)
 
 
 PyDoc_STRVAR(rstrip__doc__,
-"S.rstrip([sep]) -> unicode\n\
+"S.rstrip([chars]) -> unicode\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.\n\
-If sep is a str, it will be converted to unicode before stripping");
+If chars is given and not None, remove characters in chars instead.\n\
+If chars is a str, it will be converted to unicode before stripping");
 
 static PyObject *
 unicode_rstrip(PyUnicodeObject *self, PyObject *args)