Skip to content

Commit

Permalink
make expandtabs not quadratic for bytes either
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbolz committed Mar 2, 2024
1 parent 0d2d72e commit a553e8d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pypy/objspace/std/stringmethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,19 @@ def descr_expandtabs(self, space, tabsize=8):
ovfcheck(len(splitted) * tabsize)
except OverflowError:
raise oefmt(space.w_OverflowError, "new string is too long")
expanded = oldtoken = splitted.pop(0)

for token in splitted:
expanded += self._multi_chr(self._chr(' ')) * self._tabindent(oldtoken,
tabsize) + token
newlen = self._len() - len(splitted) + 1
builder = self._builder(len(value))
oldtoken = splitted[0]
builder.append(oldtoken)

for index in range(1, len(splitted)):
token = splitted[index]
dist = self._tabindent(oldtoken, tabsize)
builder.append_multiple_char(' ', dist)
builder.append(token)
newlen += dist
oldtoken = token

return self._new(expanded)
return self._new(builder.build())

def _tabindent(self, token, tabsize):
"""calculates distance behind the token to the next tabstop"""
Expand Down
1 change: 1 addition & 0 deletions pypy/objspace/std/unicodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ def descr_expandtabs(self, space, tabsize=8):
if not replacements and type(self) is W_UnicodeObject:
return self
newlength = self._length - replacements
assert res is not None
return W_UnicodeObject(res, newlength)

splitted = value.split('\t')
Expand Down

0 comments on commit a553e8d

Please sign in to comment.