--- parser3/src/main/pa_string.C 2008/07/16 17:06:28 1.210 +++ parser3/src/main/pa_string.C 2008/07/18 08:24:01 1.212 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_STRING_C="$Date: 2008/07/16 17:06:28 $"; +static const char * const IDENT_STRING_C="$Date: 2008/07/18 08:24:01 $"; #include "pcre.h" @@ -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;