Add support for ANSIString.strip() and .rstrip(), matching normal string equivalents. Resolve #1022.
This commit is contained in:
parent
6f02ab572f
commit
6b3f92dcbf
1 changed files with 13 additions and 2 deletions
|
|
@ -977,7 +977,7 @@ class ANSIString(with_metaclass(ANSIMeta, unicode)):
|
||||||
break
|
break
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def split(self, by, maxsplit=-1):
|
def split(self, by=None, maxsplit=-1):
|
||||||
"""
|
"""
|
||||||
Stolen from PyPy's pure Python string implementation, tweaked for
|
Stolen from PyPy's pure Python string implementation, tweaked for
|
||||||
ANSIString.
|
ANSIString.
|
||||||
|
|
@ -986,6 +986,10 @@ class ANSIString(with_metaclass(ANSIMeta, unicode)):
|
||||||
http://opensource.org/licenses/MIT
|
http://opensource.org/licenses/MIT
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
drop_spaces = by is None
|
||||||
|
if drop_spaces:
|
||||||
|
by = " "
|
||||||
|
|
||||||
bylen = len(by)
|
bylen = len(by)
|
||||||
if bylen == 0:
|
if bylen == 0:
|
||||||
raise ValueError("empty separator")
|
raise ValueError("empty separator")
|
||||||
|
|
@ -1002,6 +1006,8 @@ class ANSIString(with_metaclass(ANSIMeta, unicode)):
|
||||||
maxsplit -= 1 # NB. if it's already < 0, it stays < 0
|
maxsplit -= 1 # NB. if it's already < 0, it stays < 0
|
||||||
|
|
||||||
res.append(self[start:len(self)])
|
res.append(self[start:len(self)])
|
||||||
|
if drop_spaces:
|
||||||
|
return [part for part in res if part != ""]
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def __mul__(self, other):
|
def __mul__(self, other):
|
||||||
|
|
@ -1027,7 +1033,7 @@ class ANSIString(with_metaclass(ANSIMeta, unicode)):
|
||||||
def __rmul__(self, other):
|
def __rmul__(self, other):
|
||||||
return self.__mul__(other)
|
return self.__mul__(other)
|
||||||
|
|
||||||
def rsplit(self, by, maxsplit=-1):
|
def rsplit(self, by=None, maxsplit=-1):
|
||||||
"""
|
"""
|
||||||
Stolen from PyPy's pure Python string implementation, tweaked for
|
Stolen from PyPy's pure Python string implementation, tweaked for
|
||||||
ANSIString.
|
ANSIString.
|
||||||
|
|
@ -1038,6 +1044,9 @@ class ANSIString(with_metaclass(ANSIMeta, unicode)):
|
||||||
"""
|
"""
|
||||||
res = []
|
res = []
|
||||||
end = len(self)
|
end = len(self)
|
||||||
|
drop_spaces = by is None
|
||||||
|
if drop_spaces:
|
||||||
|
by = " "
|
||||||
bylen = len(by)
|
bylen = len(by)
|
||||||
if bylen == 0:
|
if bylen == 0:
|
||||||
raise ValueError("empty separator")
|
raise ValueError("empty separator")
|
||||||
|
|
@ -1053,6 +1062,8 @@ class ANSIString(with_metaclass(ANSIMeta, unicode)):
|
||||||
|
|
||||||
res.append(self[:end])
|
res.append(self[:end])
|
||||||
res.reverse()
|
res.reverse()
|
||||||
|
if drop_spaces:
|
||||||
|
return [part for part in res if part != ""]
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def join(self, iterable):
|
def join(self, iterable):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue