--- parser3/src/main/pa_string.C 2008/07/16 17:06:28 1.210 +++ parser3/src/main/pa_string.C 2008/08/15 15:30:12 1.215 @@ -5,9 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2008/07/16 17:06:28 $"; - -#include "pcre.h" +static const char * const IDENT_STRING_C="$Date: 2008/08/15 15:30:12 $"; #include "pa_string.h" #include "pa_exception.h" @@ -15,6 +13,8 @@ static const char * const IDENT_STRING_C #include "pa_dictionary.h" #include "pa_charset.h" +#include "pcre.h" + const String String::Empty; int pa_atoi(const char* str, const String* problem_source) { @@ -307,20 +307,28 @@ String& String::mid(size_t substr_begin, return result; } -String& String::mid(Charset& charset, size_t from, size_t to) const { +// 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=length(); // in bytes not in characters - from=min(from, self_length); + 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+self_length; + 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) @@ -359,13 +367,24 @@ size_t String::pos(const String& substr, size_t String::pos(Charset& charset, const String& substr, size_t this_offset, Language lang) const { - size_t result=pos(substr.body, this_offset, lang); - if(result==CORD_NOT_FOUND) - return STRING_NOT_FOUND; - + size_t result; if(charset.isUTF8()){ 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; @@ -476,6 +495,10 @@ 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; + + if(source_charset.isUTF8()) + option_bits[0]=option_bits[0] | PCRE_UTF8; + pcre *code=pcre_compile(pattern, option_bits[0], &errptr, &erroffset, source_charset.pcre_tables); @@ -623,6 +646,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();