Coding style.

- Re-implement termstr.splitlines.
  - Simpler and uses less memory.
This commit is contained in:
Andrew Hamilton 2021-06-14 02:08:33 +10:00
parent 2db439961d
commit c5efd3a74b

View file

@ -273,15 +273,13 @@ class TermStr(collections.UserString):
return self._split_style(self.data.split(sep, maxsplit), len(sep))
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 = []
cursor = 0
for line, line_with_end in zip(result_parts, lines_with_ends):
style_part = self.style[cursor:cursor+len(line)]
result.append(self.__class__(line, style_part))
cursor += len(line_with_end)
for line in self.data.splitlines(keepends=True):
result_line = line if keepends else line.rstrip("\r\n")
style_part = self.style[cursor:cursor+len(result_line)]
result.append(self.__class__(result_line, style_part))
cursor += len(line)
return result
def capitalize(self):