assert p.pattern.groups <= 100,\
"sorry, but this version only supports 100 named groups"
+ # map in either direction
+ groupindex = p.pattern.groupdict
+ indexgroup = [None] * p.pattern.groups
+ for k, i in groupindex.items():
+ indexgroup[i] = k
+
return _sre.compile(
pattern, flags,
array.array(WORDSIZE, code).tostring(),
- p.pattern.groups-1, p.pattern.groupdict
+ p.pattern.groups-1, groupindex, indexgroup
)
PyObject* code;
int groups = 0;
PyObject* groupindex = NULL;
- if (!PyArg_ParseTuple(args, "OiO!|iO", &pattern, &flags,
+ PyObject* indexgroup = NULL;
+ if (!PyArg_ParseTuple(args, "OiO!|iOO", &pattern, &flags,
&PyString_Type, &code,
- &groups, &groupindex))
+ &groups, &groupindex, &indexgroup))
return NULL;
self = PyObject_NEW(PatternObject, &Pattern_Type);
Py_XINCREF(groupindex);
self->groupindex = groupindex;
+ Py_XINCREF(indexgroup);
+ self->indexgroup = indexgroup;
+
return (PyObject*) self;
}
PyErr_Clear();
- /* attributes */
+ if (!strcmp(name, "lastindex")) {
+ /* experimental */
+ if (self->index >= 0)
+ return Py_BuildValue("i", self->index);
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
+ if (!strcmp(name, "lastgroup")) {
+ /* experimental */
+ if (self->pattern->indexgroup) {
+ PyObject* result = PySequence_GetItem(
+ self->pattern->indexgroup, self->index
+ );
+ if (result)
+ return result;
+ PyErr_Clear();
+ }
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
+
if (!strcmp(name, "string")) {
Py_INCREF(self->string);
return self->string;
if (!strcmp(name, "endpos"))
return Py_BuildValue("i", 0); /* FIXME */
- if (!strcmp(name, "index")) {
- /* experimental */
- if (self->index < 0) {
- Py_INCREF(Py_None);
- return Py_None;
- } else
- return Py_BuildValue("i", self->index);
- }
-
PyErr_SetString(PyExc_AttributeError, name);
return NULL;
}