mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
When expandtabs() would be a no-op, don't create a duplicate string
This commit is contained in:
parent
87a484caf5
commit
e19aa388e8
2 changed files with 11 additions and 0 deletions
|
@ -1585,6 +1585,10 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
return
|
return
|
||||||
self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize)
|
self.assertRaises(OverflowError, 't\tt\t'.expandtabs, sys.maxsize)
|
||||||
|
|
||||||
|
def test_expandtabs_optimization(self):
|
||||||
|
s = 'abc'
|
||||||
|
self.assertIs(s.expandtabs(), s)
|
||||||
|
|
||||||
def test_raiseMemError(self):
|
def test_raiseMemError(self):
|
||||||
if struct.calcsize('P') == 8:
|
if struct.calcsize('P') == 8:
|
||||||
# 64 bits pointers
|
# 64 bits pointers
|
||||||
|
|
|
@ -10196,6 +10196,7 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
|
||||||
void *src_data, *dest_data;
|
void *src_data, *dest_data;
|
||||||
int tabsize = 8;
|
int tabsize = 8;
|
||||||
int kind;
|
int kind;
|
||||||
|
int found;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
|
if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -10205,9 +10206,11 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
|
||||||
i = j = line_pos = 0;
|
i = j = line_pos = 0;
|
||||||
kind = PyUnicode_KIND(self);
|
kind = PyUnicode_KIND(self);
|
||||||
src_data = PyUnicode_DATA(self);
|
src_data = PyUnicode_DATA(self);
|
||||||
|
found = 0;
|
||||||
for (; i < src_len; i++) {
|
for (; i < src_len; i++) {
|
||||||
ch = PyUnicode_READ(kind, src_data, i);
|
ch = PyUnicode_READ(kind, src_data, i);
|
||||||
if (ch == '\t') {
|
if (ch == '\t') {
|
||||||
|
found = 1;
|
||||||
if (tabsize > 0) {
|
if (tabsize > 0) {
|
||||||
incr = tabsize - (line_pos % tabsize); /* cannot overflow */
|
incr = tabsize - (line_pos % tabsize); /* cannot overflow */
|
||||||
if (j > PY_SSIZE_T_MAX - incr)
|
if (j > PY_SSIZE_T_MAX - incr)
|
||||||
|
@ -10225,6 +10228,10 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)
|
||||||
line_pos = 0;
|
line_pos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!found && PyUnicode_CheckExact(self)) {
|
||||||
|
Py_INCREF((PyObject *) self);
|
||||||
|
return (PyObject *) self;
|
||||||
|
}
|
||||||
|
|
||||||
/* Second pass: create output string and fill it */
|
/* Second pass: create output string and fill it */
|
||||||
u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self));
|
u = PyUnicode_New(j, PyUnicode_MAX_CHAR_VALUE(self));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue