1991-12-29 21:42:57 -04:00
|
|
|
/*
|
|
|
|
XXX support range parameter on search
|
|
|
|
XXX support mstop parameter on search
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***********************************************************
|
1995-01-04 15:10:35 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-12-29 21:42:57 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Regular expression objects */
|
1992-01-19 12:31:57 -04:00
|
|
|
/* This uses Tatu Ylonen's copyleft-free reimplementation of
|
|
|
|
GNU regular expressions */
|
1991-12-29 21:42:57 -04:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
#include "Python.h"
|
1991-12-29 21:42:57 -04:00
|
|
|
|
1992-01-19 12:31:57 -04:00
|
|
|
#include "regexpr.h"
|
1991-12-29 21:42:57 -04:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *RegexError; /* Exception */
|
1991-12-29 21:42:57 -04:00
|
|
|
|
|
|
|
typedef struct {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject_HEAD
|
1991-12-29 21:42:57 -04:00
|
|
|
struct re_pattern_buffer re_patbuf; /* The compiled expression */
|
|
|
|
struct re_registers re_regs; /* The registers from the last match */
|
|
|
|
char re_fastmap[256]; /* Storage for fastmap */
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *re_translate; /* String object for translate table */
|
|
|
|
PyObject *re_lastok; /* String object last matched/searched */
|
|
|
|
PyObject *re_groupindex; /* Group name to index dictionary */
|
|
|
|
PyObject *re_givenpat; /* Pattern with symbolic groups */
|
|
|
|
PyObject *re_realpat; /* Pattern without symbolic groups */
|
1991-12-29 21:42:57 -04:00
|
|
|
} regexobject;
|
|
|
|
|
|
|
|
/* Regex object methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
reg_dealloc(re)
|
|
|
|
regexobject *re;
|
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyMem_XDEL(re->re_patbuf.buffer);
|
|
|
|
Py_XDECREF(re->re_translate);
|
|
|
|
Py_XDECREF(re->re_lastok);
|
|
|
|
Py_XDECREF(re->re_groupindex);
|
|
|
|
Py_XDECREF(re->re_givenpat);
|
|
|
|
Py_XDECREF(re->re_realpat);
|
|
|
|
PyMem_DEL(re);
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
makeresult(regs)
|
|
|
|
struct re_registers *regs;
|
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *v = PyTuple_New(RE_NREGS);
|
1991-12-29 21:42:57 -04:00
|
|
|
if (v != NULL) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < RE_NREGS; i++) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *w;
|
|
|
|
w = Py_BuildValue("(ii)", regs->start[i], regs->end[i]);
|
1993-02-21 16:12:16 -04:00
|
|
|
if (w == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_XDECREF(v);
|
1993-02-21 16:12:16 -04:00
|
|
|
v = NULL;
|
|
|
|
break;
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
PyTuple_SetItem(v, i, w);
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
reg_match(re, args)
|
|
|
|
regexobject *re;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *argstring;
|
1991-12-29 21:42:57 -04:00
|
|
|
char *buffer;
|
1992-01-27 12:46:19 -04:00
|
|
|
int size;
|
1991-12-29 21:42:57 -04:00
|
|
|
int offset;
|
|
|
|
int result;
|
1996-07-23 21:51:20 -03:00
|
|
|
if (PyArg_Parse(args, "S", &argstring)) {
|
1991-12-29 21:42:57 -04:00
|
|
|
offset = 0;
|
|
|
|
}
|
1992-01-27 12:46:19 -04:00
|
|
|
else {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_Parse(args, "(Si)", &argstring, &offset))
|
1992-09-03 17:35:01 -03:00
|
|
|
return NULL;
|
1992-01-27 12:46:19 -04:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
buffer = PyString_AsString(argstring);
|
|
|
|
size = PyString_Size(argstring);
|
1993-02-21 16:12:16 -04:00
|
|
|
if (offset < 0 || offset > size) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, "match offset out of range");
|
1993-02-21 16:12:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_XDECREF(re->re_lastok);
|
1993-02-21 16:12:16 -04:00
|
|
|
re->re_lastok = NULL;
|
1992-01-27 12:46:19 -04:00
|
|
|
result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
|
1991-12-29 21:42:57 -04:00
|
|
|
if (result < -1) {
|
|
|
|
/* Failure like stack overflow */
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, "match failure");
|
1991-12-29 21:42:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-02-21 16:12:16 -04:00
|
|
|
if (result >= 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(argstring);
|
1993-02-21 16:12:16 -04:00
|
|
|
re->re_lastok = argstring;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyInt_FromLong((long)result); /* Length of the match or -1 */
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
1992-01-27 12:46:19 -04:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
reg_search(re, args)
|
|
|
|
regexobject *re;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *argstring;
|
1991-12-29 21:42:57 -04:00
|
|
|
char *buffer;
|
|
|
|
int size;
|
|
|
|
int offset;
|
|
|
|
int range;
|
|
|
|
int result;
|
1992-01-27 12:46:19 -04:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (PyArg_Parse(args, "S", &argstring)) {
|
1991-12-29 21:42:57 -04:00
|
|
|
offset = 0;
|
|
|
|
}
|
1992-01-27 12:46:19 -04:00
|
|
|
else {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_Parse(args, "(Si)", &argstring, &offset))
|
1992-09-03 17:35:01 -03:00
|
|
|
return NULL;
|
1993-02-21 16:12:16 -04:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
buffer = PyString_AsString(argstring);
|
|
|
|
size = PyString_Size(argstring);
|
1993-02-21 16:12:16 -04:00
|
|
|
if (offset < 0 || offset > size) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, "search offset out of range");
|
1993-02-21 16:12:16 -04:00
|
|
|
return NULL;
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
1992-01-27 12:46:19 -04:00
|
|
|
/* NB: In Emacs 18.57, the documentation for re_search[_2] and
|
|
|
|
the implementation don't match: the documentation states that
|
|
|
|
|range| positions are tried, while the code tries |range|+1
|
|
|
|
positions. It seems more productive to believe the code! */
|
1992-01-26 14:12:41 -04:00
|
|
|
range = size - offset;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_XDECREF(re->re_lastok);
|
1993-02-21 16:12:16 -04:00
|
|
|
re->re_lastok = NULL;
|
1991-12-29 21:42:57 -04:00
|
|
|
result = re_search(&re->re_patbuf, buffer, size, offset, range,
|
|
|
|
&re->re_regs);
|
|
|
|
if (result < -1) {
|
|
|
|
/* Failure like stack overflow */
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, "match failure");
|
1991-12-29 21:42:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1993-02-21 16:12:16 -04:00
|
|
|
if (result >= 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(argstring);
|
1993-02-21 16:12:16 -04:00
|
|
|
re->re_lastok = argstring;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyInt_FromLong((long)result); /* Position of the match or -1 */
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1993-02-23 09:42:39 -04:00
|
|
|
reg_group(re, args)
|
1993-02-21 16:12:16 -04:00
|
|
|
regexobject *re;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *args;
|
1993-02-21 16:12:16 -04:00
|
|
|
{
|
|
|
|
int i, a, b;
|
1996-07-23 21:51:20 -03:00
|
|
|
if (args != NULL && PyTuple_Check(args)) {
|
|
|
|
int n = PyTuple_Size(args);
|
|
|
|
PyObject *res = PyTuple_New(n);
|
1993-02-21 16:12:16 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *v = reg_group(re, PyTuple_GetItem(args, i));
|
1993-02-21 16:12:16 -04:00
|
|
|
if (v == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_DECREF(res);
|
1993-02-21 16:12:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
PyTuple_SetItem(res, i, v);
|
1993-02-21 16:12:16 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(args, "i", &i)) {
|
|
|
|
PyObject *n;
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_Parse(args, "S", &n))
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
else {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *index;
|
1994-08-01 08:34:53 -03:00
|
|
|
if (re->re_groupindex == NULL)
|
|
|
|
index = NULL;
|
|
|
|
else
|
1996-07-23 21:51:20 -03:00
|
|
|
index = PyDict_GetItem(re->re_groupindex, n);
|
1994-08-01 08:34:53 -03:00
|
|
|
if (index == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, "group() group name doesn't exist");
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
i = PyInt_AsLong(index);
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
|
|
|
}
|
1993-02-21 16:12:16 -04:00
|
|
|
if (i < 0 || i >= RE_NREGS) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, "group() index out of range");
|
1993-02-21 16:12:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (re->re_lastok == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError,
|
1993-02-23 09:42:39 -04:00
|
|
|
"group() only valid after successful match/search");
|
1993-02-21 16:12:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
a = re->re_regs.start[i];
|
|
|
|
b = re->re_regs.end[i];
|
|
|
|
if (a < 0 || b < 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1993-02-21 16:12:16 -04:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyString_FromStringAndSize(PyString_AsString(re->re_lastok)+a, b-a);
|
1993-02-21 16:12:16 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static struct PyMethodDef reg_methods[] = {
|
|
|
|
{"match", (PyCFunction)reg_match},
|
|
|
|
{"search", (PyCFunction)reg_search},
|
|
|
|
{"group", (PyCFunction)reg_group},
|
1991-12-29 21:42:57 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
reg_getattr(re, name)
|
|
|
|
regexobject *re;
|
|
|
|
char *name;
|
|
|
|
{
|
1992-01-01 10:52:16 -04:00
|
|
|
if (strcmp(name, "regs") == 0) {
|
1993-02-21 16:12:16 -04:00
|
|
|
if (re->re_lastok == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-01-01 10:52:16 -04:00
|
|
|
}
|
|
|
|
return makeresult(&re->re_regs);
|
|
|
|
}
|
1993-02-21 16:12:16 -04:00
|
|
|
if (strcmp(name, "last") == 0) {
|
|
|
|
if (re->re_lastok == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1993-02-21 16:12:16 -04:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(re->re_lastok);
|
1993-02-21 16:12:16 -04:00
|
|
|
return re->re_lastok;
|
|
|
|
}
|
1993-02-23 09:42:39 -04:00
|
|
|
if (strcmp(name, "translate") == 0) {
|
|
|
|
if (re->re_translate == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1993-02-23 09:42:39 -04:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(re->re_translate);
|
1993-02-23 09:42:39 -04:00
|
|
|
return re->re_translate;
|
|
|
|
}
|
1994-08-01 08:34:53 -03:00
|
|
|
if (strcmp(name, "groupindex") == 0) {
|
|
|
|
if (re->re_groupindex == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(re->re_groupindex);
|
1994-08-01 08:34:53 -03:00
|
|
|
return re->re_groupindex;
|
|
|
|
}
|
|
|
|
if (strcmp(name, "realpat") == 0) {
|
|
|
|
if (re->re_realpat == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(re->re_realpat);
|
1994-08-01 08:34:53 -03:00
|
|
|
return re->re_realpat;
|
|
|
|
}
|
|
|
|
if (strcmp(name, "givenpat") == 0) {
|
|
|
|
if (re->re_givenpat == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1994-08-01 08:34:53 -03:00
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(re->re_givenpat);
|
1994-08-01 08:34:53 -03:00
|
|
|
return re->re_givenpat;
|
|
|
|
}
|
1993-02-23 09:42:39 -04:00
|
|
|
if (strcmp(name, "__members__") == 0) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *list = PyList_New(6);
|
1993-02-23 09:42:39 -04:00
|
|
|
if (list) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyList_SetItem(list, 0, PyString_FromString("last"));
|
|
|
|
PyList_SetItem(list, 1, PyString_FromString("regs"));
|
|
|
|
PyList_SetItem(list, 2, PyString_FromString("translate"));
|
|
|
|
PyList_SetItem(list, 3, PyString_FromString("groupindex"));
|
|
|
|
PyList_SetItem(list, 4, PyString_FromString("realpat"));
|
|
|
|
PyList_SetItem(list, 5, PyString_FromString("givenpat"));
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(list);
|
1993-02-23 09:42:39 -04:00
|
|
|
list = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
return Py_FindMethod(reg_methods, (PyObject *)re, name);
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyTypeObject Regextype = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1991-12-29 21:42:57 -04:00
|
|
|
0, /*ob_size*/
|
|
|
|
"regex", /*tp_name*/
|
|
|
|
sizeof(regexobject), /*tp_size*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
1994-08-01 08:34:53 -03:00
|
|
|
(destructor)reg_dealloc, /*tp_dealloc*/
|
1991-12-29 21:42:57 -04:00
|
|
|
0, /*tp_print*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(getattrfunc)reg_getattr, /*tp_getattr*/
|
1991-12-29 21:42:57 -04:00
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
};
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1994-08-01 08:34:53 -03:00
|
|
|
newregexobject(pattern, translate, givenpat, groupindex)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pattern;
|
|
|
|
PyObject *translate;
|
|
|
|
PyObject *givenpat;
|
|
|
|
PyObject *groupindex;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
|
|
|
regexobject *re;
|
1996-07-23 21:51:20 -03:00
|
|
|
char *pat = PyString_AsString(pattern);
|
|
|
|
int size = PyString_Size(pattern);
|
1994-08-01 08:34:53 -03:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (translate != NULL && PyString_Size(translate) != 256) {
|
|
|
|
PyErr_SetString(RegexError,
|
1993-02-21 16:12:16 -04:00
|
|
|
"translation table must be 256 bytes");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
re = PyObject_NEW(regexobject, &Regextype);
|
1991-12-29 21:42:57 -04:00
|
|
|
if (re != NULL) {
|
|
|
|
char *error;
|
|
|
|
re->re_patbuf.buffer = NULL;
|
|
|
|
re->re_patbuf.allocated = 0;
|
|
|
|
re->re_patbuf.fastmap = re->re_fastmap;
|
1993-02-21 16:12:16 -04:00
|
|
|
if (translate)
|
1996-07-23 21:51:20 -03:00
|
|
|
re->re_patbuf.translate = PyString_AsString(translate);
|
1993-02-21 16:12:16 -04:00
|
|
|
else
|
|
|
|
re->re_patbuf.translate = NULL;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_XINCREF(translate);
|
1993-02-21 16:12:16 -04:00
|
|
|
re->re_translate = translate;
|
|
|
|
re->re_lastok = NULL;
|
1994-08-01 08:34:53 -03:00
|
|
|
re->re_groupindex = groupindex;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(pattern);
|
1994-08-01 08:34:53 -03:00
|
|
|
re->re_realpat = pattern;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(givenpat);
|
1994-08-01 08:34:53 -03:00
|
|
|
re->re_givenpat = givenpat;
|
1992-01-27 12:46:19 -04:00
|
|
|
error = re_compile_pattern(pat, size, &re->re_patbuf);
|
1991-12-29 21:42:57 -04:00
|
|
|
if (error != NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyErr_SetString(RegexError, error);
|
|
|
|
Py_DECREF(re);
|
1991-12-29 21:42:57 -04:00
|
|
|
re = NULL;
|
|
|
|
}
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
return (PyObject *)re;
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
regex_compile(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pat = NULL;
|
|
|
|
PyObject *tran = NULL;
|
|
|
|
if (!PyArg_Parse(args, "S", &pat)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_Parse(args, "(SS)", &pat, &tran))
|
1993-02-21 16:12:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-01 08:34:53 -03:00
|
|
|
return newregexobject(pat, tran, pat, NULL);
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1994-08-01 08:34:53 -03:00
|
|
|
symcomp(pattern, gdict)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pattern;
|
|
|
|
PyObject *gdict;
|
1994-08-01 08:34:53 -03:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
char *opat = PyString_AsString(pattern);
|
|
|
|
char *oend = opat + PyString_Size(pattern);
|
1994-08-01 08:34:53 -03:00
|
|
|
int group_count = 0;
|
|
|
|
int escaped = 0;
|
|
|
|
char *o = opat;
|
|
|
|
char *n;
|
|
|
|
char name_buf[128];
|
|
|
|
char *g;
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *npattern;
|
1994-08-01 08:34:53 -03:00
|
|
|
int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;
|
|
|
|
|
1996-06-11 15:33:14 -03:00
|
|
|
if (oend == opat) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(pattern);
|
1996-06-11 15:33:14 -03:00
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
npattern = PyString_FromStringAndSize((char*)NULL, PyString_Size(pattern));
|
1994-08-01 08:34:53 -03:00
|
|
|
if (npattern == NULL)
|
|
|
|
return NULL;
|
1996-07-23 21:51:20 -03:00
|
|
|
n = PyString_AsString(npattern);
|
1994-08-01 08:34:53 -03:00
|
|
|
|
|
|
|
while (o < oend) {
|
|
|
|
if (*o == '(' && escaped == require_escape) {
|
|
|
|
char *backtrack;
|
|
|
|
escaped = 0;
|
|
|
|
++group_count;
|
|
|
|
*n++ = *o;
|
|
|
|
if (++o >= oend || *o != '<')
|
|
|
|
continue;
|
|
|
|
/* *o == '<' */
|
|
|
|
if (o+1 < oend && *(o+1) == '>')
|
|
|
|
continue;
|
|
|
|
backtrack = o;
|
|
|
|
g = name_buf;
|
|
|
|
for (++o; o < oend;) {
|
|
|
|
if (*o == '>') {
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *group_name = NULL;
|
|
|
|
PyObject *group_index = NULL;
|
1994-08-01 08:34:53 -03:00
|
|
|
*g++ = '\0';
|
1996-07-23 21:51:20 -03:00
|
|
|
group_name = PyString_FromString(name_buf);
|
|
|
|
group_index = PyInt_FromLong(group_count);
|
1994-08-01 08:34:53 -03:00
|
|
|
if (group_name == NULL || group_index == NULL
|
1996-07-23 21:51:20 -03:00
|
|
|
|| PyDict_SetItem(gdict, group_name, group_index) != 0) {
|
|
|
|
Py_XDECREF(group_name);
|
|
|
|
Py_XDECREF(group_index);
|
|
|
|
Py_XDECREF(npattern);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
++o; /* eat the '>' */
|
|
|
|
break;
|
|
|
|
}
|
1995-02-10 13:01:56 -04:00
|
|
|
if (!isalnum(Py_CHARMASK(*o)) && *o != '_') {
|
1994-08-01 08:34:53 -03:00
|
|
|
o = backtrack;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*g++ = *o++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*o == '[' && !escaped) {
|
|
|
|
*n++ = *o;
|
|
|
|
++o; /* eat the char following '[' */
|
|
|
|
*n++ = *o;
|
|
|
|
while (o < oend && *o != ']') {
|
|
|
|
++o;
|
|
|
|
*n++ = *o;
|
|
|
|
}
|
|
|
|
if (o < oend)
|
|
|
|
++o;
|
|
|
|
}
|
|
|
|
else if (*o == '\\') {
|
|
|
|
escaped = 1;
|
|
|
|
*n++ = *o;
|
|
|
|
++o;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
escaped = 0;
|
|
|
|
*n++ = *o;
|
|
|
|
++o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
if (_PyString_Resize(&npattern, n - PyString_AsString(npattern)) == 0)
|
1994-08-01 08:34:53 -03:00
|
|
|
return npattern;
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1994-08-01 08:34:53 -03:00
|
|
|
regex_symcomp(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1994-08-01 08:34:53 -03:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pattern;
|
|
|
|
PyObject *tran = NULL;
|
|
|
|
PyObject *gdict = NULL;
|
|
|
|
PyObject *npattern;
|
|
|
|
if (!PyArg_Parse(args, "S", &pattern)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
if (!PyArg_Parse(args, "(SS)", &pattern, &tran))
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
gdict = PyDict_New();
|
1994-08-01 08:34:53 -03:00
|
|
|
if (gdict == NULL
|
|
|
|
|| (npattern = symcomp(pattern, gdict)) == NULL) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_DECREF(gdict);
|
|
|
|
Py_DECREF(pattern);
|
1994-08-01 08:34:53 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return newregexobject(npattern, tran, pattern, gdict);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *cache_pat;
|
|
|
|
static PyObject *cache_prog;
|
1991-12-29 21:42:57 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
update_cache(pat)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pat;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
|
|
|
if (pat != cache_pat) {
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_XDECREF(cache_pat);
|
1991-12-29 21:42:57 -04:00
|
|
|
cache_pat = NULL;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_XDECREF(cache_prog);
|
|
|
|
cache_prog = regex_compile((PyObject *)NULL, pat);
|
1991-12-29 21:42:57 -04:00
|
|
|
if (cache_prog == NULL)
|
|
|
|
return -1;
|
|
|
|
cache_pat = pat;
|
1996-07-23 21:51:20 -03:00
|
|
|
Py_INCREF(cache_pat);
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
regex_match(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pat, *string;
|
|
|
|
if (!PyArg_Parse(args, "(SS)", &pat, &string))
|
1991-12-29 21:42:57 -04:00
|
|
|
return NULL;
|
|
|
|
if (update_cache(pat) < 0)
|
|
|
|
return NULL;
|
|
|
|
return reg_match((regexobject *)cache_prog, string);
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
regex_search(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *pat, *string;
|
|
|
|
if (!PyArg_Parse(args, "(SS)", &pat, &string))
|
1991-12-29 21:42:57 -04:00
|
|
|
return NULL;
|
|
|
|
if (update_cache(pat) < 0)
|
|
|
|
return NULL;
|
|
|
|
return reg_search((regexobject *)cache_prog, string);
|
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static PyObject *
|
1991-12-29 21:42:57 -04:00
|
|
|
regex_set_syntax(self, args)
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *self, *args;
|
1991-12-29 21:42:57 -04:00
|
|
|
{
|
|
|
|
int syntax;
|
1996-07-23 21:51:20 -03:00
|
|
|
if (!PyArg_Parse(args, "i", &syntax))
|
1991-12-29 21:42:57 -04:00
|
|
|
return NULL;
|
|
|
|
syntax = re_set_syntax(syntax);
|
1996-07-23 21:51:20 -03:00
|
|
|
return PyInt_FromLong((long)syntax);
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
static struct PyMethodDef regex_global_methods[] = {
|
1995-02-19 11:55:19 -04:00
|
|
|
{"compile", regex_compile, 0},
|
|
|
|
{"symcomp", regex_symcomp, 0},
|
|
|
|
{"match", regex_match, 0},
|
|
|
|
{"search", regex_search, 0},
|
|
|
|
{"set_syntax", regex_set_syntax, 0},
|
1991-12-29 21:42:57 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1996-08-19 19:03:12 -03:00
|
|
|
void
|
1991-12-29 21:42:57 -04:00
|
|
|
initregex()
|
|
|
|
{
|
1996-07-23 21:51:20 -03:00
|
|
|
PyObject *m, *d, *v;
|
1991-12-29 21:42:57 -04:00
|
|
|
|
1996-07-23 21:51:20 -03:00
|
|
|
m = Py_InitModule("regex", regex_global_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
1991-12-29 21:42:57 -04:00
|
|
|
|
|
|
|
/* Initialize regex.error exception */
|
1996-07-23 21:51:20 -03:00
|
|
|
RegexError = PyString_FromString("regex.error");
|
|
|
|
if (RegexError == NULL || PyDict_SetItemString(d, "error", RegexError) != 0)
|
|
|
|
Py_FatalError("can't define regex.error");
|
1993-02-23 09:42:39 -04:00
|
|
|
|
|
|
|
/* Initialize regex.casefold constant */
|
1996-07-23 21:51:20 -03:00
|
|
|
v = PyString_FromStringAndSize((char *)NULL, 256);
|
1993-02-23 09:42:39 -04:00
|
|
|
if (v != NULL) {
|
|
|
|
int i;
|
1996-07-23 21:51:20 -03:00
|
|
|
char *s = PyString_AsString(v);
|
1993-02-23 09:42:39 -04:00
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (isupper(i))
|
|
|
|
s[i] = tolower(i);
|
|
|
|
else
|
|
|
|
s[i] = i;
|
|
|
|
}
|
1996-07-23 21:51:20 -03:00
|
|
|
PyDict_SetItemString(d, "casefold", v);
|
|
|
|
Py_DECREF(v);
|
1993-02-23 09:42:39 -04:00
|
|
|
}
|
1991-12-29 21:42:57 -04:00
|
|
|
}
|