|
|
| version 1.211, 2008/07/17 09:11:25 | version 1.221, 2009/04/17 09:59:51 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class. @see untalength_t.C. | Parser: string class. @see untalength_t.C. |
| Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char * const IDENT_STRING_C="$Date$"; | static const char * const IDENT_STRING_C="$Date$"; |
| #include "pcre.h" | |
| #include "pa_string.h" | #include "pa_string.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_dictionary.h" | #include "pa_dictionary.h" |
| #include "pa_charset.h" | #include "pa_charset.h" |
| #include "pcre.h" | |
| const String String::Empty; | const String String::Empty; |
| int pa_atoi(const char* str, const String* problem_source) { | int pa_atoi(const char* str, const String* problem_source) { |
| Line 229 static int CORD_batched_iter_fn_generic_ | Line 229 static int CORD_batched_iter_fn_generic_ |
| }; | }; |
| uint String::Body::hash_code() const { | uint String::Body::hash_code() const { |
| uint result=0; | uint result=0; |
| CORD_iter5(body, 0, | if (body && CORD_IS_STRING(body)){ |
| CORD_batched_iter_fn_generic_hash_code, | generic_hash_code(result, body); |
| CORD_batched_iter_fn_generic_hash_code, &result); | } else { |
| CORD_iter5(body, 0, | |
| CORD_batched_iter_fn_generic_hash_code, | |
| CORD_batched_iter_fn_generic_hash_code, &result); | |
| } | |
| return result; | return result; |
| } | } |
| Line 279 String& String::append_strdup(const char | Line 283 String& String::append_strdup(const char |
| return *this; | return *this; |
| } | } |
| int CORD_batched_len(const char* s, size_t* len){ | |
| (*len) += lengthUTF8( (const XMLByte *)s, (const XMLByte *)s+strlen(s)); | |
| return 0; | |
| } | |
| // can be called only for IS_FUNCTION(CORD) which are used only in Lang | |
| int CORD_batched_len(const char, size_t *len){ | |
| (*len)++; | |
| return 0; | |
| } | |
| size_t String::length(Charset& charset) const { | size_t String::length(Charset& charset) const { |
| if(charset.isUTF8()){ | if(charset.isUTF8()){ |
| const XMLByte* srcPtr=(const XMLByte*)cstrm(); | size_t len=0; |
| return lengthUTF8(srcPtr, srcPtr+body.length()); | body.for_each(CORD_batched_len, CORD_batched_len, &len); |
| return len; | |
| } else | } else |
| return body.length(); | return body.length(); |
| } | } |
| Line 328 String& String::mid(Charset& charset, si | Line 344 String& String::mid(Charset& charset, si |
| const XMLByte* srcPtr=(const XMLByte*)cstrm(); | const XMLByte* srcPtr=(const XMLByte*)cstrm(); |
| const XMLByte* srcEnd=srcPtr+body.length(); | const XMLByte* srcEnd=srcPtr+body.length(); |
| // convert from and substr_length from 'characters' to 'bytes' | // convert 'from' and 'substr_length' from 'characters' to 'bytes' |
| from=getUTF8BytePos(srcPtr, srcEnd, from); | from=getUTF8BytePos(srcPtr, srcEnd, from); |
| substr_length=getUTF8BytePos(srcPtr+from, srcEnd, substr_length); | substr_length=getUTF8BytePos(srcPtr+from, srcEnd, substr_length); |
| if(!substr_length) | if(!substr_length) |
| Line 367 size_t String::pos(const String& substr, | Line 383 size_t String::pos(const String& substr, |
| size_t String::pos(Charset& charset, const String& substr, | size_t String::pos(Charset& charset, const String& substr, |
| size_t this_offset, Language lang) const { | size_t this_offset, Language lang) const { |
| size_t result=pos(substr.body, this_offset, lang); | size_t result; |
| if(result==CORD_NOT_FOUND) | |
| return STRING_NOT_FOUND; | |
| if(charset.isUTF8()){ | if(charset.isUTF8()){ |
| const XMLByte* srcPtr=(const XMLByte*)cstrm(); | const XMLByte* srcPtr=(const XMLByte*)cstrm(); |
| result=getUTF8CharPos(srcPtr, srcPtr+body.length(), result); | const XMLByte* srcEnd=srcPtr+body.length(); |
| // convert 'this_offset' from 'characters' to 'bytes' | |
| this_offset=getUTF8BytePos(srcPtr, srcEnd, this_offset); | |
| result=pos(substr.body, this_offset, lang); | |
| if(result==CORD_NOT_FOUND) | |
| return STRING_NOT_FOUND; | |
| // convert 'result' from 'bytes' to 'characters' | |
| result=getUTF8CharPos(srcPtr, srcEnd, result); | |
| } else { | |
| result=pos(substr.body, this_offset, lang); | |
| if(result==CORD_NOT_FOUND) | |
| return STRING_NOT_FOUND; | |
| } | } |
| return result; | return result; |
| Line 431 enum Match_feature { | Line 458 enum Match_feature { |
| static void regex_options(const String* options, int* result, int* match_features){ | static void regex_options(const String* options, int* result, int* match_features){ |
| struct Regex_option { | struct Regex_option { |
| const char* keyL; | const char* key; |
| const char* keyU; | const char* keyAlt; |
| int clear; | int clear; |
| int set; | int set; |
| int *result; | int *result; |
| int flag; | int flag; |
| } regex_option[]={ | } regex_option[]={ |
| {"i", "I", 0, PCRE_CASELESS, result, 0}, // a=A | {"i", "I", 0, PCRE_CASELESS, result, 0}, // a=A |
| {"s", "S", 0, PCRE_DOTALL, result, 0}, // \n\n$ [default] | {"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$ | {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result, 0}, // ^aaa\n$^bbb\n$ |
| {"x", 0, 0, PCRE_EXTENDED, result, 0}, // whitespace in regex ignored | |
| {"U", 0, 0, PCRE_UNGREEDY, result, 0}, // ungreedy patterns (greedy by default) | |
| {"g", "G", 0, 1, result+1, 0}, // many rows | {"g", "G", 0, 1, result+1, 0}, // many rows |
| {"'", 0, 0, 0, 0, MF_NEED_PRE_POST_MATCH}, | {"'", 0, 0, 0, 0, MF_NEED_PRE_POST_MATCH}, |
| {"n", "N", 0, 0, 0, MF_JUST_COUNT_MATCHES}, | {"n", 0, 0, 0, 0, MF_JUST_COUNT_MATCHES}, |
| {0, 0, 0, 0, 0, 0} | {0, 0, 0, 0, 0, 0} |
| }; | }; |
| result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY; | result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY; |
| result[1]=0; | result[1]=0; |
| if(options && !options->is_empty()) | if(options && !options->is_empty()) |
| for(Regex_option *o=regex_option; o->keyL; o++) | for(Regex_option *o=regex_option; o->key; o++) |
| if( | if( |
| options->pos(o->keyL)!=STRING_NOT_FOUND | options->pos(o->key)!=STRING_NOT_FOUND |
| || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND) | || (o->keyAlt && options->pos(o->keyAlt)!=STRING_NOT_FOUND) |
| ){ | ){ |
| if(o->flag){ | if(o->flag){ |
| (*match_features) |= o->flag; | (*match_features) |= o->flag; |
| Line 471 Table* String::match(Charset& source_cha | Line 499 Table* String::match(Charset& source_cha |
| Row_action row_action, void *info, | Row_action row_action, void *info, |
| int& matches_count) const { | int& matches_count) const { |
| if(regexp.is_empty()) | if(regexp.is_empty()) |
| throw Exception(0, | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "regexp is empty"); | "regexp is empty"); |
| Line 484 Table* String::match(Charset& source_cha | Line 512 Table* String::match(Charset& source_cha |
| bool need_pre_post_match=(match_features & MF_NEED_PRE_POST_MATCH) != 0; | 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 just_count_matches=(match_features & MF_JUST_COUNT_MATCHES) != 0; |
| bool global=option_bits[1]!=0; | bool global=option_bits[1]!=0; |
| if(source_charset.isUTF8()){ | |
| // @todo (for UTF-8): check string & pattern and use PCRE_NO_UTF8_CHECK option | |
| option_bits[0]|=PCRE_UTF8; | |
| } | |
| pcre *code=pcre_compile(pattern, option_bits[0], | pcre *code=pcre_compile(pattern, option_bits[0], |
| &errptr, &erroffset, | &errptr, &erroffset, |
| source_charset.pcre_tables); | source_charset.pcre_tables); |
| if(!code) | if(!code) |
| throw Exception(0, | throw Exception(PCRE_EXCEPTION_TYPE, |
| ®exp.mid(erroffset, regexp.length()), | ®exp.mid(erroffset, regexp.length()), |
| "regular expression syntax error - %s", errptr); | "regular expression syntax error - %s", errptr); |
| int subpatterns=pcre_info(code, 0, 0); | int subpatterns=pcre_info(code, 0, 0); |
| if(subpatterns<0) { | if(subpatterns<0) { |
| pcre_free(code); | pcre_free(code); |
| throw Exception(0, | throw Exception(PCRE_EXCEPTION_TYPE, |
| ®exp, | ®exp, |
| "pcre_info error (%d)", | "pcre_info error (%d)", |
| subpatterns); | subpatterns); |
| Line 523 Table* String::match(Charset& source_cha | Line 557 Table* String::match(Charset& source_cha |
| if(exec_substrings==PCRE_ERROR_NOMATCH) { | if(exec_substrings==PCRE_ERROR_NOMATCH) { |
| pcre_free(code); | pcre_free(code); |
| row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info); | 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 | |
| // } | |
| return just_count_matches ? 0 : &table; | return just_count_matches ? 0 : &table; |
| } | } |
| if(exec_substrings<0) { | if(exec_substrings<0) { |
| pcre_free(code); | pcre_free(code); |
| throw Exception(0, | throw Exception(PCRE_EXCEPTION_TYPE, |
| ®exp, | ®exp, |
| "regular expression execute error (%d)", | print_pcre_exec_error_text(exec_substrings), |
| exec_substrings); | exec_substrings); |
| } | } |
| Line 560 Table* String::match(Charset& source_cha | Line 589 Table* String::match(Charset& source_cha |
| matches_count++; | matches_count++; |
| row_action(table, row, prestart, prefinish, poststart, postfinish, info); | row_action(table, row, prestart, prefinish, poststart, postfinish, info); |
| if(!global || prestart==poststart) { // not global | going to hang | if(!global || prestart==poststart) { // last step |
| pcre_free(code); | pcre_free(code); |
| row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info); | row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info); |
| return just_count_matches ? 0 : &table; | return just_count_matches ? 0 : &table; |
| // return &table; | |
| } | } |
| prestart=poststart; | prestart=poststart; |
| Line 581 String& String::change_case(Charset& sou | Line 609 String& String::change_case(Charset& sou |
| return result; | return result; |
| char* new_cstr=cstrm(); | char* new_cstr=cstrm(); |
| size_t new_cstr_len=length(); | |
| if(source_charset.isUTF8()) { | if(source_charset.isUTF8()) { |
| size_t new_cstr_len=length(); | |
| switch(kind) { | switch(kind) { |
| case CC_UPPER: | case CC_UPPER: |
| change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToUpper); | change_case_UTF8((const XMLByte*)new_cstr, new_cstr_len, (XMLByte*)new_cstr, new_cstr_len, UTF8CaseToUpper); |
| Line 631 String& String::change_case(Charset& sou | Line 660 String& String::change_case(Charset& sou |
| return result; | return result; |
| } | } |
| const String& String::escape(Charset& source_charset) const { | |
| if(is_empty()) | |
| return *this; | |
| return Charset::escape(*this, source_charset); | |
| } | |
| const String& String::replace(const Dictionary& dict) const { | const String& String::replace(const Dictionary& dict) const { |
| String& result=*new String(); | String& result=*new String(); |
| const char* old_cstr=cstr(); | const char* old_cstr=cstr(); |