--- parser3/src/main/pa_string.C 2004/10/07 09:22:01 1.201 +++ parser3/src/main/pa_string.C 2007/04/20 10:19:06 1.209 @@ -1,11 +1,11 @@ /** @file Parser: string class. @see untalength_t.C. - Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2004/10/07 09:22:01 $"; +static const char * const IDENT_STRING_C="$Date: 2007/04/20 10:19:06 $"; #include "pcre.h" @@ -104,7 +104,7 @@ double pa_atod(const char* str, const St #ifndef DOXYGEN typedef struct { ssize_t countdown; - char target; /* Character we're looking for */ + int target; /* Character we're looking for */ } chr_data; #endif static int CORD_range_contains_chr_greater_then_proc(char c, size_t size, void* client_data) @@ -364,20 +364,27 @@ void String::split(ArrayString& result, } } -static void regex_options(const String* options, int *result, bool& need_pre_post_match){ +enum Match_feature { + MF_NEED_PRE_POST_MATCH = 0x01, + MF_JUST_COUNT_MATCHES = 0x02 +}; + +static void regex_options(const String* options, int* result, int* match_features){ struct Regex_option { const char* keyL; const char* keyU; - int clear, set; + int clear; + int set; int *result; - bool *flag; + int flag; } regex_option[]={ {"i", "I", 0, PCRE_CASELESS, result, 0}, // a=A {"s", "S", 0, PCRE_DOTALL, result, 0}, // \n\n$ [default] {"x", "U", 0, PCRE_EXTENDED, result, 0}, // whitespace in regex ignored {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result, 0}, // ^aaa\n$^bbb\n$ {"g", "G", 0, 1, result+1, 0}, // many rows - {"'", 0, 0, 0, 0, &need_pre_post_match}, + {"'", 0, 0, 0, 0, MF_NEED_PRE_POST_MATCH}, + {"n", "N", 0, 0, 0, MF_JUST_COUNT_MATCHES}, {0, 0, 0, 0, 0, 0} }; result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY; @@ -385,11 +392,13 @@ static void regex_options(const String* if(options && !options->is_empty()) for(Regex_option *o=regex_option; o->keyL; o++) - if(options->pos(o->keyL)!=STRING_NOT_FOUND - || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) { - if(o->flag) - *o->flag=true; - else { // result + if( + options->pos(o->keyL)!=STRING_NOT_FOUND + || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND) + ){ + if(o->flag){ + (*match_features) |= o->flag; + } else { *o->result &= ~o->clear; *o->result |= o->set; } @@ -400,17 +409,20 @@ Table* String::match(Charset& source_cha const String& regexp, const String* options, Row_action row_action, void *info, - bool& just_matched) const { + int& matches_count) const { if(regexp.is_empty()) throw Exception(0, 0, "regexp is empty"); - const char* pattern=regexp.cstr(); + const char* pattern=regexp.cstr(String::L_UNSPECIFIED); // fix any tainted with L_REGEX const char* errptr; int erroffset; - bool need_pre_post_match=false; - int option_bits[2]={0}; regex_options(options, option_bits, need_pre_post_match); + int option_bits[2]={0}; + int match_features=0; + regex_options(options, option_bits, &match_features); + bool need_pre_post_match=(match_features & MF_NEED_PRE_POST_MATCH) != 0; + bool just_count_matches=(match_features & MF_JUST_COUNT_MATCHES) != 0; bool global=option_bits[1]!=0; pcre *code=pcre_compile(pattern, option_bits[0], &errptr, &erroffset, @@ -451,11 +463,12 @@ Table* String::match(Charset& source_cha if(exec_substrings==PCRE_ERROR_NOMATCH) { pcre_free(code); row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info); - if(global || subpatterns) - return &table; // global or with subpatterns=true+result - else { - just_matched=false; return 0; // not global=no result - } + // if(global || subpatterns) + // return &table; // global or with subpatterns=true+result + // else { + // just_matched=false; return 0; // not global=no result + // } + return just_count_matches ? 0 : &table; } if(exec_substrings<0) { @@ -484,12 +497,14 @@ Table* String::match(Charset& source_cha *row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value } + matches_count++; row_action(table, row, prestart, prefinish, poststart, postfinish, info); if(!global || prestart==poststart) { // not global | going to hang pcre_free(code); row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info); - return &table; + return just_count_matches ? 0 : &table; + // return &table; } prestart=poststart; @@ -609,52 +624,75 @@ static int serialize_lang_piece(char ala } String::Cm String::serialize(size_t prolog_length) const { size_t fragments_count=langs.count(); + size_t body_length=body.length(); size_t buf_length= prolog_length //1 +sizeof(size_t) //2 - +fragments_count*(sizeof(char)+sizeof(size_t)) //3 - +body.length() //4 - +1; // for zero terminator used in deserialize + +body_length //3 + +1 // 4 for zero terminator used in deserialize + +sizeof(size_t) //5 + +fragments_count*(sizeof(char)+sizeof(size_t)); //6 + String::Cm result(new(PointerFreeGC) char[buf_length], buf_length); // 1: prolog char *cur=result.str+prolog_length; - // 2: langs.count [WARNING: not cast, addresses must be %4=0 on sparc] + // 2: chars.count [WARNING: not cast, addresses must be %4=0 on sparc] + memcpy(cur, &body_length, sizeof(body_length)); cur+=sizeof(body_length); + // 3: letters + body.for_each(serialize_body_char, serialize_body_piece, &cur); + // 4: zero terminator + *cur++=0; + // 5: langs.count [WARNING: not cast, addresses must be %4=0 on sparc] memcpy(cur, &fragments_count, sizeof(fragments_count)); cur+=sizeof(fragments_count); - // 3: lang info + // 6: lang info langs.for_each(body, serialize_lang_piece, &cur); - // 4: letters - body.for_each(serialize_body_char, serialize_body_piece, &cur); - // 5: zero terminator - *cur=0; return result; } -bool String::deserialize(size_t prolog_length, void *buf, size_t buf_length) { - if(buf_length<=prolog_length) +bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size) { + size_t in_buf=buf_size; + if(in_buf<=prolog_size) return false; - buf_length-=prolog_length; - buf_length-=1; // 5: zero terminator + in_buf-=prolog_size; // 1: prolog - const char* cur=(const char* )buf+prolog_length; + const char* cur=(const char* )buf+prolog_size; - // 2: langs.count + // 2: chars.count + size_t body_length; + if(in_bufbody_length) + return false; // file curruption // uchar needed to prevent propagating 0x80 bit to upper bytes langs.append(total_length, (String::Language)(uchar)lang, fragment_length); - total_length+=fragment_length; - - buf_length-=piece_length; + total_length=combined_length; + in_buf-=piece_length; } - // 4: letters - if(buf_length!=total_length) + if(total_length!=body_length) // length(all language fragments) vs length(letters) return false; - - // serialize wrote extra zero byte there, we can rely on that - body=String::Body(cur, buf_length); } + if(in_buf!=0) // some strange extra bytes + return false; ASSERT_STRING_INVARIANT(*this); return true;