2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
regexp.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
(c) 2004 Rob Kudla <pcre-component@kudla.org>
|
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __REGEXP_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include "regexp.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
2013-02-17 17:26:08 +01:00
|
|
|
#define OVECSIZE_INC 99
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
Regexp
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
static void compile(void *_object)
|
|
|
|
{
|
|
|
|
int errptr;
|
|
|
|
const char *errstr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-30 14:49:59 +01:00
|
|
|
if (!THIS->pattern) {
|
|
|
|
GB.Error("No pattern provided");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
THIS->code = pcre_compile(THIS->pattern, THIS->copts, &errstr, &errptr, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
if (!THIS->code)
|
|
|
|
GB.Error(errstr);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
static void exec(void *_object)
|
|
|
|
{
|
2013-02-17 17:26:08 +01:00
|
|
|
int ret;
|
|
|
|
char code[8];
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
if (!THIS->code)
|
|
|
|
{
|
|
|
|
GB.Error("No pattern compiled yet");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!THIS->subject)
|
|
|
|
{
|
|
|
|
GB.Error("No subject provided");
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-02-17 17:26:08 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
ret = pcre_exec(THIS->code,
|
|
|
|
NULL,
|
|
|
|
THIS->subject,
|
|
|
|
GB.StringLength(THIS->subject),
|
|
|
|
0,
|
|
|
|
THIS->eopts,
|
|
|
|
THIS->ovector,
|
|
|
|
THIS->ovecsize);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
THIS->count = ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case PCRE_ERROR_NOMATCH:
|
|
|
|
THIS->count = 0; return;
|
|
|
|
case PCRE_ERROR_NULL:
|
|
|
|
GB.Error("Pattern or subject is null"); return;
|
|
|
|
case PCRE_ERROR_BADOPTION:
|
|
|
|
GB.Error("Unknown option"); return;
|
|
|
|
case PCRE_ERROR_BADMAGIC:
|
|
|
|
case PCRE_ERROR_UNKNOWN_OPCODE:
|
|
|
|
GB.Error("Incorrect PCRE bytecode"); return;
|
|
|
|
case PCRE_ERROR_NOMEMORY:
|
|
|
|
GB.Error("Out of memory"); return;
|
|
|
|
case PCRE_ERROR_BADUTF8:
|
|
|
|
case PCRE_ERROR_SHORTUTF8:
|
|
|
|
GB.Error("Bad UTF-8 string"); return;
|
|
|
|
case PCRE_ERROR_BADUTF8_OFFSET:
|
|
|
|
GB.Error("Bad UTF-8 offset"); return;
|
|
|
|
case PCRE_ERROR_INTERNAL:
|
|
|
|
GB.Error("Unexpected internal error"); return;
|
|
|
|
case PCRE_ERROR_BADNEWLINE:
|
|
|
|
GB.Error("Invalid combination of newline options"); return;
|
|
|
|
case PCRE_ERROR_RECURSELOOP:
|
|
|
|
GB.Error("Recursion loop detected"); return;
|
|
|
|
//case PCRE_ERROR_JIT_STACKLIMIT:
|
|
|
|
// GB.Error("JIT stack limit reached"); return;
|
|
|
|
default:
|
|
|
|
sprintf(code, "%d", -ret);
|
|
|
|
GB.Error("Unable to exec regular expression: error #&1", code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
THIS->ovecsize += OVECSIZE_INC;
|
|
|
|
GB.Realloc(POINTER(&THIS->ovector), THIS->ovecsize * sizeof(int));
|
|
|
|
}
|
2011-02-27 10:13:32 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
static void return_match(void *_object, int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= THIS->count)
|
|
|
|
{
|
|
|
|
GB.Error("Out of bounds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
index *= 2;
|
|
|
|
GB.ReturnNewString(&THIS->subject[THIS->ovector[index]], THIS->ovector[index + 1] - THIS->ovector[index]);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_METHOD(RegExp_Compile, GB_STRING pattern; GB_INTEGER coptions)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-06-11 12:44:50 +02:00
|
|
|
THIS->copts = VARGOPT(coptions, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-06-11 12:44:50 +02:00
|
|
|
GB.FreeString(&THIS->pattern);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->pattern = GB.NewString(STRING(pattern), LENGTH(pattern));
|
2011-02-27 10:13:32 +01:00
|
|
|
compile(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
|
|
|
|
BEGIN_METHOD(RegExp_Exec, GB_STRING subject; GB_INTEGER eoptions)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-06-11 12:44:50 +02:00
|
|
|
THIS->eopts = VARGOPT(eoptions, 0);
|
|
|
|
|
|
|
|
GB.FreeString(&THIS->subject);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->subject = GB.NewString(STRING(subject), LENGTH(subject));
|
2011-02-27 10:13:32 +01:00
|
|
|
exec(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_METHOD(RegExp_new, GB_STRING subject; GB_STRING pattern; GB_INTEGER coptions; GB_INTEGER eoptions)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-02-17 17:26:08 +01:00
|
|
|
THIS->ovecsize = OVECSIZE_INC;
|
|
|
|
GB.Alloc(POINTER(&THIS->ovector), sizeof(int) * THIS->ovecsize);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
if (MISSING(pattern)) // the user didn't provide a pattern.
|
2008-06-11 12:44:50 +02:00
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
THIS->copts = VARGOPT(coptions, 0);
|
|
|
|
THIS->pattern = GB.NewString(STRING(pattern), LENGTH(pattern));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
compile(THIS);
|
|
|
|
if (!THIS->code) // we didn't get a compiled pattern back.
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (MISSING(subject)) // the user didn't specify any subject text.
|
|
|
|
return;
|
|
|
|
|
|
|
|
THIS->eopts = VARGOPT(eoptions, 0);
|
|
|
|
THIS->subject = GB.NewString(STRING(subject), LENGTH(subject));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
exec(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_METHOD_VOID(RegExp_free)
|
|
|
|
|
|
|
|
GB.FreeString(&THIS->subject);
|
|
|
|
GB.FreeString(&THIS->pattern);
|
|
|
|
GB.Free(POINTER(&THIS->ovector));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_PROPERTY(RegExp_Pattern)
|
|
|
|
|
|
|
|
GB.ReturnString(THIS->pattern);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_PROPERTY(RegExp_Subject)
|
|
|
|
|
|
|
|
GB.ReturnString(THIS->subject);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_PROPERTY(RegExp_Offset)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
GB.ReturnInteger(THIS->ovector[0]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
|
|
|
|
BEGIN_PROPERTY(RegExp_Text)
|
|
|
|
|
|
|
|
if (THIS->count == 0)
|
2011-10-24 21:33:41 +02:00
|
|
|
GB.ReturnVoidString();
|
2011-02-27 10:13:32 +01:00
|
|
|
else
|
|
|
|
return_match(THIS, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_PROPERTY(RegExp_Submatches_Count)
|
|
|
|
|
|
|
|
GB.ReturnInteger(THIS->count - 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_METHOD(RegExp_Submatches_get, GB_INTEGER index)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
int index = VARG(index);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-12 00:16:26 +01:00
|
|
|
if (index < 0 || index >= THIS->count)
|
2011-02-27 10:13:32 +01:00
|
|
|
{
|
|
|
|
GB.Error("Out of bounds");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
THIS->_submatch = index;
|
|
|
|
RETURN_SELF();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
END_METHOD
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_PROPERTY(RegExp_Submatch_Text)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-12 00:16:26 +01:00
|
|
|
return_match(THIS, THIS->_submatch);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
END_PROPERTY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
BEGIN_PROPERTY(RegExp_Submatch_Offset)
|
|
|
|
|
2011-03-12 00:16:26 +01:00
|
|
|
GB.ReturnInteger(THIS->ovector[2 * THIS->_submatch]);
|
2011-02-27 10:13:32 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
GB_DESC CRegexpDesc[] =
|
|
|
|
{
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_DECLARE("Regexp", sizeof(CREGEXP)),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, RegExp_new, "[(Subject)s(Pattern)s(CompileOptions)i(ExecOptions)i]"),
|
|
|
|
GB_METHOD("_free", NULL, RegExp_free, NULL),
|
|
|
|
|
|
|
|
GB_METHOD("Compile", NULL, RegExp_Compile, "(Pattern)s[(CompileOptions)i]"),
|
|
|
|
GB_METHOD("Exec", NULL, RegExp_Exec, "(Subject)s[(ExecOptions)i]"),
|
|
|
|
|
|
|
|
GB_CONSTANT("Caseless", "i", PCRE_CASELESS),
|
|
|
|
GB_CONSTANT("MultiLine", "i", PCRE_MULTILINE),
|
|
|
|
GB_CONSTANT("DotAll", "i", PCRE_DOTALL),
|
|
|
|
GB_CONSTANT("Extended", "i", PCRE_EXTENDED),
|
|
|
|
GB_CONSTANT("Anchored", "i", PCRE_ANCHORED),
|
|
|
|
GB_CONSTANT("DollarEndOnly", "i", PCRE_DOLLAR_ENDONLY),
|
|
|
|
GB_CONSTANT("Extra", "i", PCRE_EXTRA),
|
|
|
|
GB_CONSTANT("NotBOL", "i", PCRE_NOTBOL),
|
|
|
|
GB_CONSTANT("NotEOL", "i", PCRE_NOTEOL),
|
|
|
|
GB_CONSTANT("Ungreedy", "i", PCRE_UNGREEDY),
|
|
|
|
GB_CONSTANT("NotEmpty", "i", PCRE_NOTEMPTY),
|
|
|
|
GB_CONSTANT("UTF8", "i", PCRE_UTF8),
|
|
|
|
GB_CONSTANT("NoAutoCapture", "i", PCRE_NO_AUTO_CAPTURE),
|
|
|
|
GB_CONSTANT("NoUTF8Check", "i", PCRE_NO_UTF8_CHECK),
|
|
|
|
GB_CONSTANT("NoMatch", "i", PCRE_ERROR_NOMATCH),
|
|
|
|
GB_CONSTANT("Null", "i", PCRE_ERROR_NULL),
|
|
|
|
GB_CONSTANT("BadOption", "i", PCRE_ERROR_BADOPTION),
|
|
|
|
GB_CONSTANT("BadMagic", "i", PCRE_ERROR_BADMAGIC),
|
|
|
|
GB_CONSTANT("UnknownNode", "i", PCRE_ERROR_UNKNOWN_NODE),
|
|
|
|
GB_CONSTANT("NoMemory", "i", PCRE_ERROR_NOMEMORY),
|
|
|
|
GB_CONSTANT("NoSubstring", "i", PCRE_ERROR_NOSUBSTRING),
|
|
|
|
GB_CONSTANT("MatchLimit", "i", PCRE_ERROR_MATCHLIMIT),
|
|
|
|
GB_CONSTANT("Callout", "i", PCRE_ERROR_CALLOUT),
|
|
|
|
GB_CONSTANT("BadUTF8", "i", PCRE_ERROR_BADUTF8),
|
2007-12-30 17:41:49 +01:00
|
|
|
#if (((PCRE_MAJOR == 4) && (PCRE_MINOR < 5)) || (PCRE_MAJOR < 4))
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_CONSTANT("BadUTF8Offset", "i", 65535), /* PCRE_ERROR_BADUTF8_OFFSET not defined < 4.5 */
|
2007-12-30 17:41:49 +01:00
|
|
|
#else
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_CONSTANT("BadUTF8Offset", "i", PCRE_ERROR_BADUTF8_OFFSET),
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2011-08-21 23:46:20 +02:00
|
|
|
GB_PROPERTY_SELF("SubMatches", ".Regexp.Submatches"),
|
2011-02-27 10:13:32 +01:00
|
|
|
|
|
|
|
GB_PROPERTY_READ("Text", "s", RegExp_Text), /* this is the string matched by the entire pattern */
|
|
|
|
GB_PROPERTY_READ("Offset", "i", RegExp_Offset), /* this is the string matched by the entire pattern */
|
|
|
|
GB_PROPERTY_READ("Pattern", "s", RegExp_Pattern),
|
|
|
|
GB_PROPERTY_READ("Subject", "s", RegExp_Subject),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
GB_DESC CRegexpSubmatchesDesc[] =
|
|
|
|
{
|
2011-08-21 23:46:20 +02:00
|
|
|
GB_DECLARE(".Regexp.Submatches", 0), GB_VIRTUAL_CLASS(),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-08-21 23:46:20 +02:00
|
|
|
GB_METHOD("_get", ".Regexp.Submatch", RegExp_Submatches_get, "(Index)i"),
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_PROPERTY_READ("Count", "i", RegExp_Submatches_Count),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
GB_DESC CRegexpSubmatchDesc[] =
|
|
|
|
{
|
2011-08-21 23:46:20 +02:00
|
|
|
GB_DECLARE(".Regexp.Submatch", 0), GB_VIRTUAL_CLASS(),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_PROPERTY_READ("Offset", "i", RegExp_Submatch_Offset),
|
|
|
|
GB_PROPERTY_READ("Text", "s", RegExp_Submatch_Text),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-27 10:13:32 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|