--- parser3/src/main/pa_string.C 2002/04/10 08:53:55 1.151 +++ parser3/src/main/pa_string.C 2002/04/19 08:28:35 1.154 @@ -4,7 +4,7 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - $Id: pa_string.C,v 1.151 2002/04/10 08:53:55 paf Exp $ + $Id: pa_string.C,v 1.154 2002/04/19 08:28:35 paf Exp $ */ #include "pcre.h" @@ -70,8 +70,7 @@ void String::expand() { if(new_chunk_count>max_integral(Chunk::count_type)) new_chunk_count=max_integral(Chunk::count_type); - Chunk *new_chunk=static_cast( - malloc( + Chunk *new_chunk=static_cast(malloc( sizeof(Chunk)// count+interpadding(?)+rows[CR_PREALLOCATED_COUNT]+tailpadding(??) -sizeof(Chunk::rows_type) // PREALLOCATED rows +sizeof(Chunk::Row)*new_chunk_count // neaded rows @@ -442,29 +441,35 @@ void String::split(Array& result, } } -static void regex_options(char *options, int *result){ +static void regex_options(const String *options, int *result, bool& need_pre_post_match){ struct Regex_option { - char key; + const char *keyL; + const char *keyU; int clear, set; int *result; + bool *flag; } regex_option[]={ - {'i', 0, PCRE_CASELESS, result}, // a=A - {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default] - {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored - {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$ - {'g', 0, true, result+1}, // many rows - {0}, + {"i", "I", 0, PCRE_CASELESS, result}, // a=A + {"s", "S", 0, PCRE_DOTALL, result}, // \n\n$ [default] + {"x", "U", 0, PCRE_EXTENDED, result}, // whitespace in regex ignored + {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$ + {"g", "G", 0, true, result+1}, // many rows + {"'", 0, 0, 0, 0, &need_pre_post_match}, + {0} }; result[0]=PCRE_EXTRA | PCRE_DOTALL; result[1]=0; if(options) - for(Regex_option *o=regex_option; o->key; o++) - if( - strchr(options, o->key) || - strchr(options, toupper(o->key))) { - *(o->result)&=~o->clear; - *(o->result)|=o->set; + for(Regex_option *o=regex_option; o->keyL; o++) + if(options->pos(o->keyL)>=0 + || (o->keyU && options->pos(o->keyU)>=0)) { + if(o->flag) + *o->flag=true; + else { // result + *o->result &= ~o->clear; + *o->result |= o->set; + } } } @@ -481,10 +486,12 @@ bool String::match( throw Exception(0, aorigin, "regexp is empty"); + const char *pattern=regexp.cstr(); const char *errptr; int erroffset; - int option_bits[2]; regex_options(options?options->cstr():0, option_bits); + bool need_pre_post_match=false; + int option_bits[2]; regex_options(options, option_bits, need_pre_post_match); if(was_global) *was_global=option_bits[1]!=0; pcre *code=pcre_compile(pattern, option_bits[0], @@ -505,7 +512,6 @@ bool String::match( info_substrings); } - int startoffset=0; const char *subject=cstr(); int length=strlen(subject); int ovecsize; @@ -526,14 +532,17 @@ bool String::match( } int exec_option_bits=0; + int prestart=0; + int poststart=0; + int postfinish=size(); while(true) { int exec_substrings=pcre_exec(code, 0, - subject, length, startoffset, + subject, length, prestart, exec_option_bits, ovector, ovecsize); if(exec_substrings==PCRE_ERROR_NOMATCH) { pcre_free(code); - row_action(**table, 0/*last time, no row*/, 0, 0, info); + row_action(**table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info); return option_bits[1]!=0; // global=true+table, not global=false } @@ -545,24 +554,26 @@ bool String::match( exec_substrings); } + int prefinish=ovector[0]; + poststart=ovector[1]; Array& row=*NEW Array(pool()); - row+=&mid(0, ovector[0]); // .prematch column value - row+=&mid(ovector[0], ovector[1]); // .match - row+=&mid(ovector[1], size()); // .postmatch + row+=need_pre_post_match?&mid(0, prefinish):0; // .prematch column value + row+=need_pre_post_match?&mid(prefinish, poststart):0; // .match + row+=need_pre_post_match?&mid(poststart, postfinish):0; // .postmatch for(int i=1; i