Skip to content

Commit

Permalink
a faster implementation if tabsize=0
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbolz committed Mar 2, 2024
1 parent 14c41dc commit 0d2d72e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 0 additions & 2 deletions pypy/objspace/std/test/test_bytesobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,6 @@ def test_expandtabs(self):

def test_expandtabs_overflows_gracefully(self):
import sys
if sys.maxint > (1 << 32):
skip("Wrong platform")
raises((MemoryError, OverflowError), b't\tt\t'.expandtabs, sys.maxint)

def test_expandtabs_0(self):
Expand Down
1 change: 1 addition & 0 deletions pypy/objspace/std/test/test_unicodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def test_expandtabs_0(self):

def test_expandtabs_bug(self):
assert u"a\u266f\ttest".expandtabs() == u'a\u266f test'
assert u"a\u266f\ttest".expandtabs(0) == u'a\u266ftest'

def test_translate(self):
assert u'bbbc' == u'abababc'.translate({ord('a'):None})
Expand Down
9 changes: 7 additions & 2 deletions pypy/objspace/std/unicodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,17 @@ def descr_expandtabs(self, space, tabsize=8):
value = self._utf8
if not value:
return self._empty()
if tabsize == 0:
res, replacements = replace_count(value, '\t', '')
if not replacements and type(self) is W_UnicodeObject:
return self
newlength = self._length - replacements
return W_UnicodeObject(res, newlength)

splitted = value.split('\t')

try:
if tabsize > 0:
ovfcheck(len(splitted) * tabsize)
ovfcheck(len(splitted) * tabsize)
except OverflowError:
raise oefmt(space.w_OverflowError, "new string is too long")
newlen = self._len() - len(splitted) + 1
Expand Down

0 comments on commit 0d2d72e

Please sign in to comment.