/*********************************************************** Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. 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 or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI 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. ******************************************************************/ /* $Id$ */ /* Regular expression objects */ /* This uses Tatu Ylonen's copyleft-free reimplementation of GNU regular expressions */ #include "Python.h" #include #include "regexpr.h" static PyObject *ReopError; /* Exception */ static PyObject * makeresult(regs, num_regs) struct re_registers *regs; int num_regs; { PyObject *v; int i; static PyObject *filler = NULL; if (filler == NULL) { filler = Py_BuildValue("(ii)", -1, -1); if (filler == NULL) return NULL; } v = PyTuple_New(num_regs); if (v == NULL) return NULL; for (i = 0; i < num_regs; i++) { int lo = regs->start[i]; int hi = regs->end[i]; PyObject *w; if (lo == -1 && hi == -1) { w = filler; Py_INCREF(w); } else w = Py_BuildValue("(ii)", lo, hi); if (w == NULL || PyTuple_SetItem(v, i, w) < 0) { Py_DECREF(v); return NULL; } } return v; } static PyObject * reop_match(self, args) PyObject *self; PyObject *args; { char *string; int fastmaplen, stringlen; int can_be_null, anchor, i; int num_regs, flags, pos, result; struct re_pattern_buffer bufp; struct re_registers re_regs; if (!PyArg_Parse(args, "(s#iiis#is#i)", &(bufp.buffer), &(bufp.allocated), &num_regs, &flags, &can_be_null, &(bufp.fastmap), &fastmaplen, &anchor, &string, &stringlen, &pos)) return NULL; /* XXX sanity-check the input data */ bufp.used=bufp.allocated; bufp.translate=NULL; bufp.fastmap_accurate=1; bufp.can_be_null=can_be_null; bufp.uses_registers=1; bufp.num_registers=num_regs; bufp.anchor=anchor; for(i=0; ire_patbuf, start, length, next, length-next, &pattern->re_regs); if (result < -1) { /* Erk... an error happened during the reop search */ Py_DECREF(newlist); PyErr_SetString(ReopError, "match failure"); return NULL; } if (next<=result) { int match_start=pattern->re_regs.start[0]; int oldmatch_end=match_end; match_end=pattern->re_regs.end[0]; if (match_start==match_end) { /* A zero-length match; increment to the next position */ next=result+1; match_end=oldmatch_end; continue; } /* Append the string up to the start of the match */ s=PyString_FromStringAndSize(start+oldmatch_end, match_start-oldmatch_end); if (!s) { Py_DECREF(newlist); return NULL; } PyList_Append(newlist, s); Py_DECREF(s); if (retain) { /* Append a string containing whatever matched */ s=PyString_FromStringAndSize(start+match_start, match_end-match_start); if (!s) { Py_DECREF(newlist); return NULL; } PyList_Append(newlist, s); Py_DECREF(s); } /* Update the pointer, and increment the count of splits */ next=match_end; count++; } } while (result!=-1 && !(maxsplit && maxsplit==count) && next