diff options
author | Raymond Hettinger <python@rcn.com> | 2007-04-12 04:10:00 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-04-12 04:10:00 (GMT) |
commit | 4db5fe970c695c373c59ac7152eb07b07249b0b5 (patch) | |
tree | b245934756e45a1dbc07fae5a07481373e560106 /Objects/stringobject.c | |
parent | 51761806105a4cc2f58325f46a6d33c8144ad617 (diff) | |
download | cpython-4db5fe970c695c373c59ac7152eb07b07249b0b5.zip cpython-4db5fe970c695c373c59ac7152eb07b07249b0b5.tar.gz cpython-4db5fe970c695c373c59ac7152eb07b07249b0b5.tar.bz2 |
SF 1193128: Let str.translate(None) be an identity transformation
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 64be0de..63e1381 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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; |