--- parser3/src/main/pa_string.C 2001/04/03 05:23:41 1.60 +++ parser3/src/main/pa_string.C 2001/04/03 09:58:10 1.62 @@ -5,11 +5,13 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_string.C,v 1.60 2001/04/03 05:23:41 paf Exp $ + $Id: pa_string.C,v 1.62 2001/04/03 09:58:10 paf Exp $ */ #include "pa_config_includes.h" +#include + #include "pa_pool.h" #include "pa_string.h" #include "pa_hash.h" @@ -17,9 +19,17 @@ #include "pa_common.h" #include "pa_array.h" #include "pa_globals.h" +#include "pa_table.h" +#include "pa_threads.h" + +#include "pcre.h" //#include "pa_sapi.h" +// consts + +const int MAX_MATCH_COLUMNS=20; + // String String::String(Pool& apool, const char *src, bool tasize_ted) : @@ -450,4 +460,68 @@ void String::split(Array& result, if(pos_after_ref) *pos_after_ref+=size(); } -} \ No newline at end of file +} + +/// @test setlocale param to auto.p @test pcre_malloc & pcre_free substs +bool String::match(const String *aorigin, + const String& regexp, + const String& options, + Table **table) const { + SYNCHRONIZED(true); + static const unsigned char *tables=0; + if(!tables) { + setlocale(LC_CTYPE, "ru"); + tables = pcre_maketables(); + } + const char *pattern=regexp.cstr(); + const char *errptr; + int erroffset; + pcre *code=pcre_compile(pattern, 0, + &errptr, &erroffset, + tables); + + if(!code) + THROW(0, 0, + ®exp.piece(erroffset, regexp.size()), + errptr); + + int ovecsize; + int *ovector=(int *)malloc(sizeof(int)*(ovecsize=(1/*.match*/+MAX_MATCH_COLUMNS)*3)); + const char *subject=cstr(); + int length=strlen(subject); + int exec_result=pcre_exec(code, 0, + subject, length, 0/*startoffset*/, + 0/*options*/, ovector, ovecsize); + + if(exec_result==PCRE_ERROR_NOMATCH) { + *table=0; + return false; + } + + if(exec_result<0) + THROW(0, 0, + 0, + "pcre_exec failed"); + + if(exec_result==0) + THROW(0, 0, + aorigin, + "produced more substrings than maximum handled by Parser, which is %d", + MAX_MATCH_COLUMNS); + + Array& columns=*NEW Array(pool()); + columns+=string_match_name; // .match column name + Array& row=*NEW Array(pool()); + row+=&piece(ovector[0], ovector[1]); // match column value + + for(int i=1; i