SF 1193128: Let str.translate(None) be an identity transformation

This commit is contained in:
Raymond Hettinger 2007-04-12 04:10:00 +00:00
parent 5176180610
commit 4db5fe970c
6 changed files with 33 additions and 10 deletions

View file

@ -2344,10 +2344,10 @@ static PyObject *
string_translate(PyStringObject *self, PyObject *args)
{
register char *input, *output;
register const char *table;
const char *table;
register Py_ssize_t i, c, changed = 0;
PyObject *input_obj = (PyObject*)self;
const char *table1, *output_start, *del_table=NULL;
const char *output_start, *del_table=NULL;
Py_ssize_t inlen, tablen, dellen = 0;
PyObject *result;
int trans_table[256];
@ -2358,9 +2358,13 @@ string_translate(PyStringObject *self, PyObject *args)
return NULL;
if (PyString_Check(tableobj)) {
table1 = PyString_AS_STRING(tableobj);
table = PyString_AS_STRING(tableobj);
tablen = PyString_GET_SIZE(tableobj);
}
else if (tableobj == Py_None) {
table = NULL;
tablen = 256;
}
#ifdef Py_USING_UNICODE
else if (PyUnicode_Check(tableobj)) {
/* Unicode .translate() does not support the deletechars
@ -2374,7 +2378,7 @@ string_translate(PyStringObject *self, PyObject *args)
return PyUnicode_Translate((PyObject *)self, tableobj, NULL);
}
#endif
else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen))
else if (PyObject_AsCharBuffer(tableobj, &table, &tablen))
return NULL;
if (tablen != 256) {
@ -2403,7 +2407,6 @@ string_translate(PyStringObject *self, PyObject *args)
dellen = 0;
}
table = table1;
inlen = PyString_GET_SIZE(input_obj);
result = PyString_FromStringAndSize((char *)NULL, inlen);
if (result == NULL)
@ -2411,7 +2414,7 @@ string_translate(PyStringObject *self, PyObject *args)
output_start = output = PyString_AsString(result);
input = PyString_AS_STRING(input_obj);
if (dellen == 0) {
if (dellen == 0 && table != NULL) {
/* If no deletions are required, use faster code */
for (i = inlen; --i >= 0; ) {
c = Py_CHARMASK(*input++);
@ -2425,8 +2428,13 @@ string_translate(PyStringObject *self, PyObject *args)
return input_obj;
}
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(table[i]);
if (table == NULL) {
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(i);
} else {
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(table[i]);
}
for (i = 0; i < dellen; i++)
trans_table[(int) Py_CHARMASK(del_table[i])] = -1;