|
|
| version 1.202.6.2.2.1, 2006/03/04 18:04:03 | version 1.219, 2009/04/10 11:33:16 |
|---|---|
| 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 104 double pa_atod(const char* str, const St | Line 104 double pa_atod(const char* str, const St |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| typedef struct { | typedef struct { |
| ssize_t countdown; | ssize_t countdown; |
| char target; /* Character we're looking for */ | int target; /* Character we're looking for */ |
| } chr_data; | } chr_data; |
| #endif | #endif |
| static int CORD_range_contains_chr_greater_then_proc(char c, size_t size, void* client_data) | static int CORD_range_contains_chr_greater_then_proc(char c, size_t size, void* client_data) |
| Line 279 String& String::append_strdup(const char | Line 279 String& String::append_strdup(const char |
| return *this; | return *this; |
| } | } |
| size_t String::length(Charset& charset) const { | |
| if(charset.isUTF8()){ | |
| const XMLByte* srcPtr=(const XMLByte*)cstrm(); | |
| return lengthUTF8(srcPtr, srcPtr+body.length()); | |
| } else | |
| return body.length(); | |
| } | |
| /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab" | /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab" |
| String& String::mid(size_t substr_begin, size_t substr_end) const { | String& String::mid(size_t substr_begin, size_t substr_end) const { |
| String& result=*new String; | String& result=*new String; |
| Line 299 String& String::mid(size_t substr_begin, | Line 307 String& String::mid(size_t substr_begin, |
| return result; | return result; |
| } | } |
| // from, to and helper_length in characters, not in bytes (it's important for utf-8) | |
| String& String::mid(Charset& charset, size_t from, size_t to, size_t helper_length) const { | |
| String& result=*new String; | |
| size_t self_length=(helper_length)?helper_length:length(charset); | |
| if(!self_length) | |
| return result; | |
| from=min(min(to, from), self_length); | |
| to=min(max(to, from), self_length); | |
| size_t substr_length=to-from; | |
| if(!substr_length) | |
| return result; | |
| if(charset.isUTF8()){ | |
| const XMLByte* srcPtr=(const XMLByte*)cstrm(); | |
| const XMLByte* srcEnd=srcPtr+body.length(); | |
| // convert 'from' and 'substr_length' from 'characters' to 'bytes' | |
| from=getUTF8BytePos(srcPtr, srcEnd, from); | |
| substr_length=getUTF8BytePos(srcPtr+from, srcEnd, substr_length); | |
| if(!substr_length) | |
| return result; | |
| } | |
| // first: their langs | |
| result.langs.append(result.body, langs, from, substr_length); | |
| // next: letters themselves | |
| result.body=body.mid(from, substr_length); | |
| ASSERT_STRING_INVARIANT(result); | |
| return result; | |
| } | |
| size_t String::pos(const String::Body substr, size_t this_offset, Language lang) const { | size_t String::pos(const String::Body substr, size_t this_offset, Language lang) const { |
| size_t substr_length=substr.length(); | size_t substr_length=substr.length(); |
| while(true) { | while(true) { |
| Line 319 size_t String::pos(const String& substr, | Line 364 size_t String::pos(const String& substr, |
| return pos(substr.body, this_offset, lang); | return pos(substr.body, this_offset, lang); |
| } | } |
| size_t String::pos(Charset& charset, const String& substr, | |
| size_t this_offset, Language lang) const { | |
| size_t result; | |
| if(charset.isUTF8()){ | |
| const XMLByte* srcPtr=(const XMLByte*)cstrm(); | |
| 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; | |
| } | |
| void String::split(ArrayString& result, | void String::split(ArrayString& result, |
| size_t& pos_after, | size_t& pos_after, |
| const char* delim, | const char* delim, |
| Line 364 void String::split(ArrayString& result, | Line 435 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 { | struct Regex_option { |
| const char* keyL; | const char* key; |
| const char* keyU; | const char* keyAlt; |
| int clear, set; | int clear; |
| int set; | |
| int *result; | int *result; |
| bool *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, &need_pre_post_match}, | {"'", 0, 0, 0, 0, MF_NEED_PRE_POST_MATCH}, |
| {"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(options->pos(o->keyL)!=STRING_NOT_FOUND | if( |
| || (o->keyU && options->pos(o->keyU)!=STRING_NOT_FOUND)) { | options->pos(o->key)!=STRING_NOT_FOUND |
| if(o->flag) | || (o->keyAlt && options->pos(o->keyAlt)!=STRING_NOT_FOUND) |
| *o->flag=true; | ){ |
| else { // result | if(o->flag){ |
| (*match_features) |= o->flag; | |
| } else { | |
| *o->result &= ~o->clear; | *o->result &= ~o->clear; |
| *o->result |= o->set; | *o->result |= o->set; |
| } | } |
| Line 400 Table* String::match(Charset& source_cha | Line 481 Table* String::match(Charset& source_cha |
| const String& regexp, | const String& regexp, |
| const String* options, | const String* options, |
| Row_action row_action, void *info, | Row_action row_action, void *info, |
| bool& just_matched) 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"); |
| const char* pattern=regexp.cstr(String::L_UNSPECIFIED); // fix any tainted with L_REGEX | const char* pattern=regexp.cstr(String::L_UNSPECIFIED); // fix any tainted with L_REGEX |
| const char* errptr; | const char* errptr; |
| int erroffset; | int erroffset; |
| bool need_pre_post_match=false; | int option_bits[2]={0}; |
| int option_bits[2]={0}; regex_options(options, option_bits, need_pre_post_match); | 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; | 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 451 Table* String::match(Charset& source_cha | Line 541 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 just_count_matches ? 0 : &table; |
| return &table; // global or with subpatterns=true+result | |
| else { | |
| just_matched=false; return 0; // not global=no result | |
| } | |
| } | } |
| 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 484 Table* String::match(Charset& source_cha | Line 570 Table* String::match(Charset& source_cha |
| *row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value | *row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value |
| } | } |
| 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 &table; | return just_count_matches ? 0 : &table; |
| } | } |
| prestart=poststart; | prestart=poststart; |
| Line 506 String& String::change_case(Charset& sou | Line 593 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 556 String& String::change_case(Charset& sou | Line 644 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(); |
| Line 644 bool String::deserialize(size_t prolog_s | Line 739 bool String::deserialize(size_t prolog_s |
| // 1: prolog | // 1: prolog |
| const char* cur=(const char* )buf+prolog_size; | const char* cur=(const char* )buf+prolog_size; |
| // 2: langs.count | // 2: chars.count |
| size_t body_length; | size_t body_length; |
| if(in_buf<sizeof(body_length)) // body.length don't fit? | if(in_buf<sizeof(body_length)) // body.length don't fit? |
| return false; | return false; |