--- parser3/src/main/pa_string.C 2007/04/20 10:19:06 1.209 +++ parser3/src/main/pa_string.C 2008/07/23 14:07:04 1.214 @@ -5,9 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2007/04/20 10:19:06 $"; - -#include "pcre.h" +static const char * const IDENT_STRING_C="$Date: 2008/07/23 14:07:04 $"; #include "pa_string.h" #include "pa_exception.h" @@ -279,6 +277,14 @@ String& String::append_strdup(const char 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" String& String::mid(size_t substr_begin, size_t substr_end) const { String& result=*new String; @@ -299,6 +305,43 @@ String& String::mid(size_t substr_begin, 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 substr_length=substr.length(); while(true) { @@ -319,6 +362,32 @@ size_t String::pos(const String& substr, 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, size_t& pos_after, const char* delim, @@ -424,7 +493,7 @@ Table* String::match(Charset& source_cha 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], + PCRE::pcre *code=PCRE::pcre_compile(pattern, option_bits[0], &errptr, &erroffset, source_charset.pcre_tables); @@ -433,9 +502,9 @@ Table* String::match(Charset& source_cha ®exp.mid(erroffset, regexp.length()), "regular expression syntax error - %s", errptr); - int subpatterns=pcre_info(code, 0, 0); + int subpatterns=PCRE::pcre_info(code, 0, 0); if(subpatterns<0) { - pcre_free(code); + PCRE::pcre_free(code); throw Exception(0, ®exp, "pcre_info error (%d)", @@ -456,12 +525,12 @@ Table* String::match(Charset& source_cha int poststart=0; int postfinish=length(); while(true) { - int exec_substrings=pcre_exec(code, 0, + int exec_substrings=PCRE::pcre_exec(code, 0, subject, subject_length, prestart, exec_option_bits, ovector, oveclength); if(exec_substrings==PCRE_ERROR_NOMATCH) { - pcre_free(code); + PCRE::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 @@ -472,7 +541,7 @@ Table* String::match(Charset& source_cha } if(exec_substrings<0) { - pcre_free(code); + PCRE::pcre_free(code); throw Exception(0, ®exp, "regular expression execute error (%d)", @@ -501,7 +570,7 @@ Table* String::match(Charset& source_cha row_action(table, row, prestart, prefinish, poststart, postfinish, info); if(!global || prestart==poststart) { // not global | going to hang - pcre_free(code); + PCRE::pcre_free(code); row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info); return just_count_matches ? 0 : &table; // return &table; @@ -571,6 +640,13 @@ String& String::change_case(Charset& sou 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 { String& result=*new String(); const char* old_cstr=cstr();