Coding style.
- Re-implement termstr.splitlines. - Simpler and uses less memory.
This commit is contained in:
parent
2db439961d
commit
c5efd3a74b
1 changed files with 5 additions and 7 deletions
|
|
@ -273,15 +273,13 @@ class TermStr(collections.UserString):
|
||||||
return self._split_style(self.data.split(sep, maxsplit), len(sep))
|
return self._split_style(self.data.split(sep, maxsplit), len(sep))
|
||||||
|
|
||||||
def splitlines(self, keepends=0):
|
def splitlines(self, keepends=0):
|
||||||
lines_with_ends = self.data.splitlines(keepends=True)
|
|
||||||
lines_without_ends = self.data.splitlines()
|
|
||||||
result_parts = lines_with_ends if keepends else lines_without_ends
|
|
||||||
result = []
|
result = []
|
||||||
cursor = 0
|
cursor = 0
|
||||||
for line, line_with_end in zip(result_parts, lines_with_ends):
|
for line in self.data.splitlines(keepends=True):
|
||||||
style_part = self.style[cursor:cursor+len(line)]
|
result_line = line if keepends else line.rstrip("\r\n")
|
||||||
result.append(self.__class__(line, style_part))
|
style_part = self.style[cursor:cursor+len(result_line)]
|
||||||
cursor += len(line_with_end)
|
result.append(self.__class__(result_line, style_part))
|
||||||
|
cursor += len(line)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def capitalize(self):
|
def capitalize(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue