Commit 5a7f2177 authored by Guido van Rossum's avatar Guido van Rossum

Fix big ineficciency in regobj.search/match (introduced by Barry in an

otherwise laudible attempt to rationalize the argument parsing): it
would save a copy of the original string instead of a reference to it.
Go back to saving a reference, but keep the "s#" format (using a hack
that involves two argument parsing steps, first using "O", then using
"s#").
parent 63bfe34d
...@@ -113,12 +113,15 @@ regobj_match(re, args) ...@@ -113,12 +113,15 @@ regobj_match(re, args)
regexobject *re; regexobject *re;
PyObject *args; PyObject *args;
{ {
PyObject *argstring;
char *buffer; char *buffer;
int size; int size;
int offset = 0; int offset = 0;
int result; int result;
if (!PyArg_ParseTuple(args, "s#|i", &buffer, &size, &offset)) if (!PyArg_ParseTuple(args, "O|i", &argstring, &offset))
return NULL;
if (!PyArg_Parse(argstring, "s#", &buffer, &size))
return NULL; return NULL;
if (offset < 0 || offset > size) { if (offset < 0 || offset > size) {
...@@ -134,10 +137,8 @@ regobj_match(re, args) ...@@ -134,10 +137,8 @@ regobj_match(re, args)
return NULL; return NULL;
} }
if (result >= 0) { if (result >= 0) {
PyObject* str = PyString_FromStringAndSize(buffer, size); Py_INCREF(argstring);
if (!str) re->re_lastok = argstring;
return NULL;
re->re_lastok = str;
} }
return PyInt_FromLong((long)result); /* Length of the match or -1 */ return PyInt_FromLong((long)result); /* Length of the match or -1 */
} }
...@@ -147,13 +148,16 @@ regobj_search(re, args) ...@@ -147,13 +148,16 @@ regobj_search(re, args)
regexobject *re; regexobject *re;
PyObject *args; PyObject *args;
{ {
PyObject *argstring;
char *buffer; char *buffer;
int size; int size;
int offset = 0; int offset = 0;
int range; int range;
int result; int result;
if (!PyArg_ParseTuple(args, "s#|i", &buffer, &size, &offset)) if (!PyArg_ParseTuple(args, "O|i", &argstring, &offset))
return NULL;
if (!PyArg_Parse(argstring, "s#", &buffer, &size))
return NULL; return NULL;
if (offset < 0 || offset > size) { if (offset < 0 || offset > size) {
...@@ -175,10 +179,8 @@ regobj_search(re, args) ...@@ -175,10 +179,8 @@ regobj_search(re, args)
return NULL; return NULL;
} }
if (result >= 0) { if (result >= 0) {
PyObject* str = PyString_FromStringAndSize(buffer, size); Py_INCREF(argstring);
if (!str) re->re_lastok = argstring;
return NULL;
re->re_lastok = str;
} }
return PyInt_FromLong((long)result); /* Position of the match or -1 */ return PyInt_FromLong((long)result); /* Position of the match or -1 */
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment