Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,8 @@ def encode(self, encoding='utf-8', errors='strict'):
return self.data.encode(encoding, errors)

def endswith(self, suffix, start=0, end=_sys.maxsize):
if isinstance(suffix, UserString):
suffix = suffix.data
return self.data.endswith(suffix, start, end)

def expandtabs(self, tabsize=8):
Expand Down Expand Up @@ -1646,6 +1648,8 @@ def splitlines(self, keepends=False):
return self.data.splitlines(keepends)

def startswith(self, prefix, start=0, end=_sys.maxsize):
if isinstance(prefix, UserString):
prefix = prefix.data
return self.data.startswith(prefix, start, end)

def strip(self, chars=None):
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_userstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ def test_mixed_cmp(self):
self._assert_cmp(a, 'a', 1)
self._assert_cmp(a, 'ac', -1)

@support.subTests('str_class', (UserString, UserStringSubclass))
def test_startswith_userstring(self, str_class):
s = UserString('hello world')
self.assertTrue(s.startswith(str_class('hello')))
self.assertFalse(s.startswith(str_class('world')))
self.assertTrue(s.startswith(str_class('')))
self.assertFalse(s.startswith(str_class('hello world!')))
self.assertTrue(s.startswith(str_class('world'), 6))
self.assertTrue(s.startswith(str_class('world'), 6, 11))
self.assertFalse(s.startswith(str_class('world'), 6, 10))
self.assertTrue(s.startswith(str_class('world'), -5))
self.assertTrue(s.startswith(str_class('world'), start=6, end=11))

@support.subTests('str_class', (UserString, UserStringSubclass))
def test_endswith_userstring(self, str_class):
s = UserString('hello world')
self.assertTrue(s.endswith(str_class('world')))
self.assertFalse(s.endswith(str_class('hello')))
self.assertTrue(s.endswith(str_class('')))
self.assertFalse(s.endswith(str_class('hello world!')))
self.assertTrue(s.endswith(str_class('world'), 6))
self.assertTrue(s.endswith(str_class('hello'), 0, 5))
self.assertFalse(s.endswith(str_class('hello'), 1, 5))
self.assertTrue(s.endswith(str_class('hello'), -11, -6))
self.assertTrue(s.endswith(str_class('hello'), start=0, end=5))

def test_rmod(self):
class ustr2(UserString):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Allow :meth:`!collections.UserString.startswith` and
:meth:`!collections.UserString.endswith` to accept a
:class:`~collections.UserString` instance as the prefix or suffix argument.
Loading