Diff for /parser3/src/main/pa_string.C between versions 1.172.2.21.2.26 and 1.173

version 1.172.2.21.2.26, 2003/03/24 16:08:35 version 1.173, 2003/04/11 15:00:05
Line 1 Line 1
 /** @file  /** @file
         Parser: string class. @see untalength_t.C.          Parser: string class. @see untasize_t.C.
   
         Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001, 2003 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)
 */  */
   
Line 9  static const char* IDENT_STRING_C="$Date Line 9  static const char* IDENT_STRING_C="$Date
   
 #include "pcre.h"  #include "pcre.h"
   
   #include "pa_pool.h"
 #include "pa_string.h"  #include "pa_string.h"
   #include "pa_hash.h"
 #include "pa_exception.h"  #include "pa_exception.h"
   #include "pa_common.h"
   #include "pa_array.h"
   #include "pa_globals.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"
   
 // helpers  #define DEBUG_STRING_APPENDS_VS_EXPANDS
   
 /// String::match uses this as replace & global search table columns  
   
 const int MAX_MATCH_GROUPS=100;  #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
   ulong string_piece_appends=0;
   #endif
   
 class String_match_table_template_columns: public ArrayString {  String& String::OnPool(Pool& apool, const char *local_src, size_t src_size, bool tainted) {
 public:          if(local_src && *local_src) {
         String_match_table_template_columns() {                  if(src_size==0)
                 *this+=new String("prematch");                          src_size=strlen(local_src);
                 *this+=new String("match");  
                 *this+=new String("postmatch");  
                 for(int i=0; i<MAX_MATCH_GROUPS; i++) {  
                         char *cname=new(PointerFreeGC) char[3/*strlen("100")*/+1/*terminating 0*/];  
                         *this+=new String(cname, sprintf(cname, "%d", 1+i));  
                 }  
         }  
 };  
   
 Table string_match_table_template(new String_match_table_template_columns);  
   
 // String::ArrayFragment methods  
   
 void String::ArrayFragment::append_positions(const ArrayFragment& src,   
                                              size_t substr_begin, size_t substr_end) {  
         if(substr_begin==substr_end)  
                 return;  
   
         size_t fragment_begin=0;  
         size_t fragment_end;  
         for(Array_iterator<element_type> i(src); ; fragment_begin=fragment_end) {  
                 const element_type& fragment=i.next();  
                 fragment_end=fragment_begin+fragment.length;              
   
                 if(substr_begin<fragment_begin) // not reached fragments which may include 'substr'?  
                         continue;  
   
                 if(substr_begin>fragment_end) // not reached fragments which may include 'substr'?  
                         continue;  
   
                 // found first fragment including piece of 'substr'  
                 size_t piece_end=min(substr_end, fragment_end);  
                 *this+=Fragment(fragment.lang, piece_end-substr_begin);  
                                   
                 while(substr_end>fragment_end) { // are there more fragments including pieces of 'substr'?                  char *pooled_src=(char *)apool.malloc(src_size);
                         fragment_begin=fragment_end;                  memcpy(pooled_src, local_src, src_size);
                   return *new(apool) String(apool, pooled_src, src_size, tainted);
                         const element_type& fragment=i.next();          } else
                         fragment_end=fragment_begin+fragment.length;                  return *new(apool) String(apool);
   }
   String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
           Pooled(apool) {
           last_chunk=&head.chunk;
           head.chunk.count=CR_PREALLOCATED_COUNT;
           append_here=head.chunk.rows;
   
           if(src)
                   if(tainted)
                           APPEND_TAINTED(src, src_size, 0, 0);
                   else
                           APPEND_CLEAN(src, src_size, 0, 0);
   }
   
                         if(substr_end>fragment_end) // are there still more?  String::String(const String& src) :     
                                 append(Fragment(fragment.lang, fragment.length)); // appending whole fragment          Pooled(src.pool()) {
                         else { // no, it was last          last_chunk=&head.chunk;
                                 append(Fragment(fragment.lang, substr_end-fragment_begin));          head.chunk.count=CR_PREALLOCATED_COUNT;
                                 return;          append_here=head.chunk.rows;
                         }  
                 }  
   
                 break;          append(src, UL_PASS_APPENDED);
         }         
 }  }
   
 // StringSimple methods  size_t  String::size() const {
           size_t result=0;
           STRING_FOREACH_ROW(
                           result+=row->item.size;
           );
           return result;
   }
   
 static int CORD_batched_iter_fn_generic_hash_code(char c, void * client_data) {  /// @todo not very optimal
         uint& result=*static_cast<uint*>(client_data);  uint String::used_rows() const {
         generic_hash_code(result, c);  
         return 0;  
 }  
 static int CORD_batched_iter_fn_generic_hash_code(const char*  s, void * client_data) {  
         uint& result=*static_cast<uint*>(client_data);  
         generic_hash_code(result, s);  
         return 0;  
 };  
 uint StringBody::hash_code() const {  
         uint result=0;          uint result=0;
         CORD_iter5(body, 0,          STRING_FOREACH_ROW(
                 CORD_batched_iter_fn_generic_hash_code,                   result++;
                 CORD_batched_iter_fn_generic_hash_code, &result);          );
         return result;          return result;
 }  }
   void String::expand() {
 // String methods          uint new_chunk_count=last_chunk->count+CR_GROW_COUNT;
           if(new_chunk_count>max_integral(Chunk::count_type))
 String::String(const char* cstr, size_t helper_length, bool tainted): body(CORD_EMPTY) {                  new_chunk_count=max_integral(Chunk::count_type);
         append(cstr, helper_length, tainted?L_TAINTED:L_CLEAN);  
           Chunk *new_chunk=static_cast<Chunk *>(malloc(
                           sizeof(Chunk)// count+interpadding(?)+rows[CR_PREALLOCATED_COUNT]+tailpadding(??)
                           -sizeof(Chunk::rows_type) // PREALLOCATED rows
                           +sizeof(Chunk::Row)*new_chunk_count // neaded rows
                           +sizeof(Chunk *) // link size
                   , 10));
           new_chunk->rows[new_chunk->count=new_chunk_count].link=0;
           last_chunk->rows[last_chunk->count].link=new_chunk;
           
           last_chunk=new_chunk;
           append_here=last_chunk->rows;
 }  }
   
 String::String(const String& src): body(src.body), fragments(src.fragments) {}  String& String::real_append(STRING_APPEND_PARAMS) {
           if(!last_chunk) // growth stopped [we're appended as string to somebody]
                   throw Exception(0,
                           this,
                           "string growth stopped (append cstr)");
   
 String& String::append(const char* str, size_t length, Language lang) {          if(!src)
         if(!str)  
                 return *this;                  return *this;
         size_t use_length=length?length:strlen(str);          if(!size)
         if(!use_length)                  size=strlen(src);
           if(!size)
                 return *this;                  return *this;
   
         body.append(str, use_length);  #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
         fragments+=Fragment(lang, use_length);          string_piece_appends++;
   #endif
   
           // manually unrolled to avoid extra check
           while(size>max_integral(Chunk::Row::item_size_type)) {
                   if(chunk_is_full())
                           expand();
   
                   append_here->item.ptr=src;
                   append_here->item.size=max_integral(Chunk::Row::item_size_type);
                   append_here->item.lang=lang;
   #ifndef NO_STRING_ORIGIN
                   append_here->item.origin.file=file;
                   append_here->item.origin.line=line;
   #endif
                   append_here++;
   
         return *this;                  src+=max_integral(Chunk::Row::item_size_type);
 }                  size-=max_integral(Chunk::Row::item_size_type);
           }
   
 /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab"          if(chunk_is_full())
 String& String::mid(size_t substr_begin, size_t substr_end) const {                  expand();
         String& result=*new String;  
   
         size_t self_length=length();  
         substr_begin=max(min(substr_begin, self_length), (size_t)0);  
         substr_end=min(max(substr_end, substr_begin), self_length);  
         if(substr_begin==substr_end)  
                 return result;  
   
         // first: letters themselves          append_here->item.ptr=src;
         result.body=body.mid(substr_begin, substr_end-substr_begin);          append_here->item.size=size;
           append_here->item.lang=lang;
   #ifndef NO_STRING_ORIGIN
           append_here->item.origin.file=file;
           append_here->item.origin.line=line;
   #endif
           append_here++;
   
         // next: their langs          return *this;
         result.fragments.append_positions(fragments, substr_begin, substr_end);  }
   
 //      SAPI::log("piece of '%s' from %d to %d is '%s'",  uint String::hash_code() const {
                 //cstr(), substr_begin, substr_end, result.cstr());          uint result=0;
           STRING_FOREACH_ROW(
                           result=Hash::generic_code(result, row->item.ptr, row->item.size);
           );
         return result;          return result;
 }  }
   
 size_t String::pos(const StringBody substr,   /// @todo move 'lang' skipping to pos
                    size_t this_offset, Language lang) const {  int String::cmp(int& partial, const String& src, 
         // first: letters themselves                                  size_t this_offset, Untaint_lang lang) const {
         size_t substr_begin=body.pos(substr, this_offset);          partial=-1;
         if(substr_begin==CORD_NOT_FOUND)          size_t a_size=size();
                 return STRING_NOT_FOUND;          this_offset=min(this_offset, a_size-1);
   
         // next: check the lang when specified          const Chunk *a_chunk=&head.chunk;
           const Chunk *b_chunk=&src.head.chunk;
         if(lang==L_UNSPECIFIED) // ignore lang?          const Chunk::Row *a_row=a_chunk->rows;
                 return substr_begin;          const Chunk::Row *b_row=b_chunk->rows;
           size_t a_offset=this_offset;
         // substr must be in one fragment, and fragments' lang must = lang          size_t b_offset=0;
         size_t substr_end=substr_begin+substr.length();          Chunk::Row *a_end=append_here;
         size_t fragment_begin=0;          Chunk::Row *b_end=src.append_here;
         size_t fragment_end;          uint a_countdown=a_chunk->count;
         for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); fragment_begin=fragment_end) {          uint b_countdown=b_chunk->count;
                 const Fragment& fragment=i.next();          int result;
                 fragment_end=fragment_begin+fragment.length;          size_t pos=0; 
   
                 if(substr_begin<fragment_begin) // not reached fragments which may include 'result'?          bool a_break=a_size==0;
                         continue;          bool b_break=src.is_empty();
                 if(substr_begin>=fragment_end) // begin of substr OUT of current fragment?          if(!(a_break || b_break)) while(true) {
                         continue;                  if(pos+a_row->item.size > this_offset) {
                                           if(lang!=UL_UNSPECIFIED && a_row->item.lang>lang) 
                 if(substr_end>fragment_end) // end of substr OUT of current fragment?                                  return -1; // wrong lang -- bail out
                         throw Exception(0, // (*) see below  
                                 this,                          int size_diff=
                                 "searching for '%s' starting from %ud problem: found begin in one fragment, but end in another",                                  (a_row->item.size-a_offset)-
                                         substr.cstr(), this_offset);                                  (b_row->item.size-b_offset);
                           
                           if(size_diff==0) { // a has same size as b
                                   result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                                           a_row->item.size-a_offset);
                                   if(result)
                                           return result;
                                   pos+=a_row->item.size;
                                   a_row++; a_countdown--; a_offset=0;
                                   b_row++; b_countdown--; b_offset=0;
                           } else if (size_diff>0) { // a longer
                                   result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                                           b_row->item.size-b_offset);
                                   if(result)
                                           return result;
                                   a_offset+=b_row->item.size-b_offset;
                                   b_row++; b_countdown--; b_offset=0;
                           } else { // b longer
                                   result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                                           a_row->item.size-a_offset);
                                   if(result)
                                           return result;
                                   b_offset+=a_row->item.size-a_offset;
                                   pos+=a_row->item.size;
                                   a_row++; a_countdown--; a_offset=0;
                           }
                           if(b_break=b_row==b_end) {
                                   a_break=a_row==a_end;
                                   break;                  
                           }
                           if(!b_countdown) {
                                   b_chunk=b_row->link;
                                   b_row=b_chunk->rows;
                                   b_countdown=b_chunk->count;
                           }
                   } else {
                           a_offset-=a_row->item.size;
                           pos+=a_row->item.size;
                           a_row++; a_countdown--; 
                   }
   
                 if(fragment.lang<=lang)                  if(a_break=a_row==a_end) {
                         return substr_begin;                          b_break=b_row==b_end;
                 else { // bad lang...                          break;
                         /// WARNING: this possibly skips assert (*), but it's fast                  }
                         substr_begin=body.pos(substr, fragment_end/*...search AFTER for more*/);                  if(!a_countdown) {
                         if(substr_begin==CORD_NOT_FOUND)                          a_chunk=a_row->link;
                                 return STRING_NOT_FOUND;                          a_row=a_chunk->rows;
                           a_countdown=a_chunk->count;
                   }
           }
           if(a_break==b_break) { // ended simultaneously
                   partial=0; return 0;
           } else if(a_break) { // first bytes equal, but a ended before b
                   partial=1; return -1;
           } else {
                   partial=2; return +1;
           }
   }
   
                         size_t substr_end=substr_begin+substr.length();  /// @todo move 'lang' skipping to pos
                         // and continuing with next fragment  int String::cmp(int& partial, const char* b_ptr, size_t src_size, 
                                   size_t this_offset, Untaint_lang lang) const {
           partial=-1;
           size_t a_size=size();
           size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
           this_offset=min(this_offset, a_size-1);
   
           const Chunk *a_chunk=&head.chunk;
           const Chunk::Row *a_row=a_chunk->rows;
           size_t a_offset=this_offset;
           size_t b_offset=0;
           Chunk::Row *a_end=append_here;
           uint a_countdown=a_chunk->count;
           size_t pos=0;
   
           bool a_break=a_size==0;
           bool b_break=b_size==0;
           if(!(a_break || b_break)) while(true) {
                   if(pos+a_row->item.size > this_offset) {
                           if(lang!=UL_UNSPECIFIED && a_row->item.lang>lang) 
                                   return -1; // wrong lang -- bail out
   
                           int size_diff=
                                   (a_row->item.size-a_offset)-
                                   (b_size-b_offset);
                           
                           if(size_diff==0) { // a has same size as b
                                   if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
                                           a_row->item.size-a_offset)!=0)
                                           return result;
                                   pos+=a_row->item.size;
                                   a_row++; a_countdown--; a_offset=0;
                                   b_break=true;
                           } else if (size_diff>0) { // a longer
                                   if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
                                           b_size-b_offset)!=0)
                                           return result;
                                   a_offset+=b_size-b_offset;
                                   b_break=true;
                           } else { // b longer
                                   if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
                                           a_row->item.size-a_offset)!=0)
                                           return result;
                                   b_offset+=a_row->item.size-a_offset;
                                   pos+=a_row->item.size;
                                   a_row++; a_countdown--; a_offset=0;
                           }
                   } else {
                           a_offset-=a_row->item.size; 
                           pos+=a_row->item.size;
                           a_row++; a_countdown--; 
                 }                  }
         }         
   
         return STRING_NOT_FOUND;                  a_break=a_row==a_end;
                   if(a_break || b_break)
                           break;
                   if(!a_countdown) {
                           a_chunk=a_row->link;
                           a_row=a_chunk->rows;
                           a_countdown=a_chunk->count;
                   }
           }
           if(a_break==b_break) { // ended simultaneously
                   partial=0; return 0;
           } else if(a_break) { // first bytes equal, but a ended before b
                   partial=1; return -1;
           } else {
                   partial=2; return +1;
           }
   }
   
   #ifndef NO_STRING_ORIGIN
   const Origin& String::origin() const { 
           if(is_empty()) {
                   static const Origin empty_origin={"empty string"};
                   return empty_origin;
           }
           
           // determining origin by first piece or last appended piece
           // because any of them can be constant=without origin: 
           // ex: ^load[/file] "document_root" + "/file"
           // when last peice is constant, 
           // ex: parser_root_auto_path{dynamic} / auto.p{const}
           // using first piece
           Origin& first_origin=head.chunk.rows[0].item.origin;
           return first_origin.file ? first_origin : append_here[-1].item.origin;
   }
   #endif
   
   String& String::mid(size_t start, size_t finish) const {
           String& result=*NEW String(pool());
   
           start=min(start, size());
           finish=max(start, finish);
           if(start==finish)
                   return result;
   
           size_t pos=0;
           STRING_FOREACH_ROW(
                   size_t item_finish=pos+row->item.size;
                   if(item_finish > start) { // started now or already?
                           bool started=result.is_empty(); // started now?
                           bool finished=finish <= item_finish; // finished now?
                           size_t offset=started?start-pos:0;
                           size_t size=finished?finish-pos:row->item.size;
                           result.APPEND(
                                   row->item.ptr+offset, size-offset, 
                                   row->item.lang,
                                   row->item.origin.file, row->item.origin.line);
                           if(finished)
                                   goto break2;
                   }
                   pos+=row->item.size;
           );
   break2:
   //      SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
                   //cstr(), start, finish, result.cstr());
           return result;
   }
   
   int String::pos(const String& substr, 
                                   int result, Untaint_lang lang) const {
           size_t self_size=size();
           for(; size_t(result)<self_size; result++) {
                   int partial; cmp(partial, substr, result, lang);
                   if(
                           partial==0 || // full match
                           partial==2) // 'substr' starts 'this'+'result'
                           return result;
           }
           
           return -1;
 }  }
   
 size_t String::pos(const String& substr,   int String::pos(const char *substr, size_t substr_size, 
                                 size_t this_offset, Language lang) const {                                  int result, Untaint_lang lang) const {
         return pos(substr.body, this_offset, lang);          size_t self_size=size();
           for(; size_t(result)<self_size; result++) {
                   int partial; cmp(partial, substr, substr_size, result, lang);
                   if(
                           partial==0 || // full match
                           partial==2) // 'substr' starts 'this'+'result'
                           return result;
           }
           
           return -1;
 }  }
   
 void String::split(ArrayString& result,   void String::split(Array& result, 
                                    size_t& pos_after,                                      size_t* pos_after_ref, 
                                    const char* delim,                                      const char *delim, size_t delim_size, 
                                    Language lang, int limit) const {                                     Untaint_lang lang, int limit) const {
         size_t self_length=length();          size_t self_size=size();
         if(size_t delim_length=strlen(delim)) {          if(delim_size) {
                   size_t pos_after=pos_after_ref?*pos_after_ref:0;
                 int pos_before;                  int pos_before;
                 // while we have 'delim'...                  // while we have 'delim'...
                 for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {                  for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
                         result+=&mid(pos_after, pos_before);                          result+=&mid(pos_after, pos_before);
                         pos_after=pos_before+delim_length;                          pos_after=pos_before+delim_size;
                 }                  }
                 // last piece                  // last piece
                 if(pos_after<self_length && limit) {                  if(pos_after<self_size && limit) {
                         result+=&mid(pos_after, self_length);                          result+=&mid(pos_after, self_size);
                         pos_after=self_length;                          pos_after=self_size;
                 }                  }
                   if(pos_after_ref)
                           *pos_after_ref=pos_after;
         } else { // empty delim          } else { // empty delim
                 result+=this;                  result+=this;
                 pos_after+=self_length;                  if(pos_after_ref)
                           *pos_after_ref+=self_size;
         }          }
 }  }
   
 void String::split(ArrayString& result,   void String::split(Array& result, 
                                    size_t& pos_after,                                      size_t* pos_after_ref, 
                                    const String& delim, Language lang,                                      const String& delim, Untaint_lang lang, 
                                    int limit) const {                                     int limit) const {
         if(!delim.is_empty()) {          if(!delim.is_empty()) {
                   size_t pos_after=pos_after_ref?*pos_after_ref:0;
                 int pos_before;                  int pos_before;
                 // while we have 'delim'...                  // while we have 'delim'...
                 for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {                  for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
                         result+=&mid(pos_after, pos_before);                          result+=&mid(pos_after, pos_before);
                         pos_after=pos_before+delim.length();                          pos_after=pos_before+delim.size();
                 }                  }
                 // last piece                  // last piece
                 if(pos_after<length() && limit) {                  if(pos_after<size() && limit) {
                         result+=&mid(pos_after, length());                          result+=&mid(pos_after, size());
                         pos_after=length();                          pos_after=size();
                 }                  }
                   if(pos_after_ref)
                           *pos_after_ref=pos_after;
         } else { // empty delim          } else { // empty delim
                 result+=this;                  result+=this;
                 pos_after+=length();                  if(pos_after_ref)
                           *pos_after_ref+=size();
         }          }
 }  }
   
 static void regex_options(const String* options, int *result, bool& need_pre_post_match){  static void regex_options(const String *options, int *result, bool& need_pre_post_match){
     struct Regex_option {      struct Regex_option {
                 const char* keyL;                  const char *keyL;
                 const char* keyU;                  const char *keyU;
                 int clear, set;                  int clear, set;
                 int *result;                  int *result;
                 bool *flag;                  bool *flag;
Line 256  static void regex_options(const String* Line 462  static void regex_options(const String*
         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) 
                 for(Regex_option *o=regex_option; o->keyL; o++)                   for(Regex_option *o=regex_option; o->keyL; o++) 
                         if(options->pos(o->keyL)>=0                          if(options->pos(o->keyL)>=0
                                 || (o->keyU && options->pos(o->keyU)>=0)) {                                  || (o->keyU && options->pos(o->keyU)>=0)) {
Line 269  static void regex_options(const String* Line 475  static void regex_options(const String*
                         }                          }
 }  }
   
 Table* String::match(Charset& source_charset,  /// @todo make replacement Table stacked
                                            const String& regexp,   bool String::match(
                                            const String* options,                     const String *aorigin,
                                            Row_action row_action, void *info,                     const String& regexp, 
                                            bool& just_matched) const {                      const String *options,
                      Table **table,
                      Row_action row_action, void *info,
                      bool *was_global) const { 
   
         if(regexp.is_empty())          if(regexp.is_empty())
                 throw Exception(0,                  throw Exception(0,
                         0,                          aorigin,
                         "regexp is empty");                          "regexp is empty");
   
         const char* pattern=regexp.cstr();          const char *pattern=regexp.cstr();
         const char* errptr;          const char *errptr;
         int erroffset;          int erroffset;
         bool need_pre_post_match=false;          bool need_pre_post_match=false;
         int option_bits[2];  regex_options(options, option_bits, need_pre_post_match);          int option_bits[2];  regex_options(options, option_bits, need_pre_post_match);
         bool global=option_bits[1]!=0;          if(was_global)
                   *was_global=option_bits[1]!=0;
         pcre *code=pcre_compile(pattern, option_bits[0],           pcre *code=pcre_compile(pattern, option_bits[0], 
                 &errptr, &erroffset,                  &errptr, &erroffset,
                 source_charset.pcre_tables);                  pool().get_source_charset().pcre_tables);
   
         if(!code)          if(!code)
                 throw Exception(0,                  throw Exception(0,
                         &regexp.mid(erroffset, regexp.length()),                          &regexp.mid(erroffset, regexp.size()),
                         "regular expression syntax error - %s", errptr);                          "regular expression syntax error - %s", errptr);
                   
         int subpatterns=pcre_info(code, 0, 0);          int info_substrings=pcre_info(code, 0, 0);
         if(subpatterns<0) {          if(info_substrings<0) {
                 pcre_free(code);                  pcre_free(code);
                 throw Exception(0,                  throw Exception(0,
                         &regexp,                          aorigin,
                         "pcre_info error (%d)",                           "pcre_info error (%d)", 
                                 subpatterns);                                  info_substrings);
         }          }
   
         const char* subject=cstr();          const char *subject=cstr();
         size_t subject_length=strlen(subject);          int length=strlen(subject);
         const int oveclength=(1/*match*/+MAX_MATCH_GROUPS)*3;          const int ovecsize=(1/*match*/+MAX_STRING_MATCH_TABLE_COLUMNS)*3;
         int ovector[oveclength];          int ovector[ovecsize];
   
         // create table          // create table
         Table& table=*new Table(string_match_table_template);          Table::Action_options table_options;
           *table=NEW Table(pool(), *string_match_table_template, table_options);
   
         int exec_option_bits=0;          int exec_option_bits=0;
         int prestart=0;          int prestart=0;
         int poststart=0;          int poststart=0;
         int postfinish=length();          int postfinish=size();
         while(true) {          while(true) {
                 int exec_substrings=pcre_exec(code, 0,                  int exec_substrings=pcre_exec(code, 0,
                         subject, subject_length, prestart,                          subject, length, prestart,
                         exec_option_bits, ovector, oveclength);                          exec_option_bits, ovector, ovecsize);
                                   
                 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 row*/, 0, 0, poststart, postfinish, info);
                         if(global || subpatterns)                          return option_bits[1]!=0; // global=true+table, not global=false
                                 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(0,
                                 &regexp,                                  aorigin,
                                 "regular expression execute error (%d)",                                   "regular expression execute error (%d)", 
                                         exec_substrings);                                          exec_substrings);
                 }                  }
   
                 int prefinish=ovector[0];                  int prefinish=ovector[0];
                 poststart=ovector[1];                  poststart=ovector[1];
                 ArrayString* row=new ArrayString;                  Array& row=*NEW Array(pool());
                 if(need_pre_post_match) {                  row+=need_pre_post_match?&mid(0, prefinish):0; // .prematch column value
                         *row+=&mid(0, prefinish); // .prematch column value                  row+=need_pre_post_match?&mid(prefinish, poststart):0; // .match
                         *row+=&mid(prefinish, poststart); // .match                  row+=need_pre_post_match?&mid(poststart, postfinish):0; // .postmatch
                         *row+=&mid(poststart, postfinish); // .postmatch  
                 } else {  
                         *row+=0; // .prematch column value  
                         *row+=0; // .match  
                         *row+=0; // .postmatch  
                 }  
                                   
                 for(int i=1; i<exec_substrings; i++) {                  for(int i=1; i<exec_substrings; i++) {
                         // -1:-1 case handled peacefully by mid() itself                          // -1:-1 case handled peacefully by mid() itself
                         *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
                 }                  }
                                   
                 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(!option_bits[1] || prestart==poststart) { // not global | going to hang
                         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 true;
                 }                  }
                 prestart=poststart;                  prestart=poststart;
   
Line 372  Table* String::match(Charset& source_cha Line 574  Table* String::match(Charset& source_cha
         }          }
 }  }
   
 String& String::change_case(Charset& source_charset, Change_case_kind kind) const {  String& String::change_case(Pool& pool, 
         String& result=*new String();                                                          Change_case_kind kind) const {
           const unsigned char *tables=pool.get_source_charset().pcre_tables;
         const unsigned char *tables=source_charset.pcre_tables;          String& result=*new(pool) String(pool);
   
         const unsigned char *a;          const unsigned char *a;
         const unsigned char *b;          const unsigned char *b;
Line 397  String& String::change_case(Charset& sou Line 599  String& String::change_case(Charset& sou
                 break; // never                  break; // never
         }                 }       
   
         char* new_cstr=cstrm();          STRING_FOREACH_ROW(
         char *dest=new_cstr;                  char *new_cstr=(char *)pool.malloc(row->item.size, 12);
         unsigned char index;                  char *dest=new_cstr;
         for(const char* current=new_cstr; index=(unsigned char)*current; current++) {                  const char *src=row->item.ptr; 
                 unsigned char c=a[index];                  for(int size=row->item.size; size--; src++) {
                 if(b)                          unsigned char c=a[(unsigned char)*src];
                         c=b[c];                          if(b)
                                   c=b[c];
   
                 *dest++=(char)c;                          *dest++=(char)c;
         }                  }
         result.body=new_cstr;                  
         result.fragments.append(fragments);                  result.APPEND(new_cstr, row->item.size, 
                           row->item.lang,
                           row->item.origin.file, row->item.origin.line);
           );
   
         return result;          return result;
 }  }
   
 const String& String::replace(const Dictionary& dict) const {  /// @test if in some piece were found no dict words, append it, not it's duplicate
         String& result=*new String();  String& String::replace(Pool& pool, Dictionary& dict) const {
         const char* old_cstr=cstr();          char *lcstr=cstr();
         const char* prematch_begin=old_cstr;          const char *current=lcstr;
   
         for(const char* current=old_cstr; *current; ) {          String& result=*new(pool) String(pool);
                 if(Table::element_type row=dict.first_that_begins(current)) {          STRING_FOREACH_ROW(
                         // prematch  IFNDEF_NO_STRING_ORIGIN(
                         if(size_t prematch_length=current-prematch_begin) {                  const char *joined_origin_file=row->item.origin.file;
                                 result.body.append(prematch_begin, prematch_length);                  const size_t joined_origin_line=row->item.origin.line;
                                 result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr);  );
                   uchar joined_lang=row->item.lang;
                   const char *joined_ptr=current;
                   // calc size
                   size_t joined_size=0;
                   STRING_PREPARED_FOREACH_ROW(*this, 
                           if(row->item.lang==joined_lang)
                                   joined_size+=row->item.size;
                           else
                                   break; // before non-ours
                   );
                   current+=joined_size;
   
                   // pointers are after joined piece
                   // & one step back, see STRING_FOREACH_ROW
                   --row;  ++countdown;
                   
                   char *new_cstr=(char *)pool.malloc((size_t)ceil(joined_size*dict.max_ratio()), 14);
                   char *dest=new_cstr;
                   while(joined_size) {
                           // there is a row where first column starts 'joined_ptr'
                           if(Table::Item *item=dict.first_that_starts(joined_ptr, joined_size)) {
                                   // get a=>b values
                                   const String& a=*static_cast<Array *>(item)->get_string(0);
                                   const String& b=*static_cast<Array *>(item)->get_string(1);
                                   // skip 'a' in 'joined_ptr' && reduce work size
                                   joined_ptr+=a.size();  joined_size-=a.size();
                                   // write 'b' to 'dest' && skip 'b' in 'dest'
                                   b.store_to(dest);  dest+=b.size();
                           } else {
                                   // write a char to b && reduce work size
                                   *dest++=*joined_ptr++;  joined_size--;
                         }                          }
                   }
   
                         // match                  result.APPEND(new_cstr, dest-new_cstr, joined_lang,
                           joined_origin_file, joined_origin_line);
           );
   
                         const String* a=row->get(0);          return result;
                         // skip 'a' in 'current'; move prematch_begin  }
                         current+=a->length(); prematch_begin=current;  
   
                         if(row->count()>1) { // are there any b?  
                                 const String* b=row->get(1);  
                                 if(size_t b_length=b->length()) {  
                                         result.body.append(b->cstr_to_string_body());  
                                         result.fragments.append(b->fragments);  
                                 }  
                         }  
                 } else // simply advance  
                         current++;   
         }  
   
         // postmatch  String& String::join_chains(Pool& pool, char** acstr) const {
         if(size_t postmatch_length=current-prematch_begin) {          char *lcstr=cstr();
                 result.body.append(prematch_begin, postmatch_length);          const char *current=lcstr;
                 result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr);  
         }          String& result=*new(pool) String(pool);
           STRING_FOREACH_ROW(
   IFNDEF_NO_STRING_ORIGIN(
                   const char *joined_origin_file=row->item.origin.file;
                   const size_t joined_origin_line=row->item.origin.line;
   );
                   uchar joined_lang=row->item.lang;
                   const char *joined_ptr=current;
                   // calc size
                   size_t joined_size=0;
                   STRING_PREPARED_FOREACH_ROW(*this, 
                           if(row->item.lang==joined_lang)
                                   joined_size+=row->item.size;
                           else
                                   break; // before non-ours
                   );
                   current+=joined_size;
   
                   // pointers are after joined piece
                   // & one step back, see STRING_FOREACH_ROW
                   --row;  ++countdown;
                   
                   result.APPEND(joined_ptr, joined_size, joined_lang,
                           joined_origin_file, joined_origin_line);
           );
   
           if(acstr)
                   *acstr=lcstr;
         return result;          return result;
 }  }
   
 double String::as_double() const {   double String::as_double() const { 
         double result;          double result;
         const char *str=cstr();          const char *cstr;
           char buf[MAX_NUMBER];
         while(*str && isspace(*str))          if(head.chunk.rows+1==append_here) {
                 str++;                  int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
         if(!*str)                  memcpy(buf, head.chunk.rows[0].item.ptr, size);
                   buf[size]=0;
                   cstr=buf;
           } else
                   cstr=this->cstr();
           while(*cstr && isspace(*cstr))
                   cstr++;
           if(!*cstr)
                 return 0;                  return 0;
   
         char *error_pos;          char *error_pos;
         // 0xABC          // 0xABC
         if(str[0]=='0')          if(cstr[0]=='0')
                 if(str[1]=='x' || str[1]=='X')                  if(cstr[1]=='x' || cstr[1]=='X')
                         result=(double)(unsigned long)strtol(str, &error_pos, 0);                          result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
                 else                  else
                         result=(double)strtod(str+1/*skip leading 0*/, &error_pos);                          result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
         else          else
                 result=(double)strtod(str, &error_pos);                  result=(double)strtod(cstr, &error_pos);
   
         while(char c=*error_pos++)          while(char c=*error_pos++)
                 if(!isspace(c))                  if(!isspace(c))
Line 481  double String::as_double() const { Line 742  double String::as_double() const {
 }  }
 int String::as_int() const {   int String::as_int() const { 
         int result;          int result;
         const char *str=cstr();          const char *cstr;
           char buf[MAX_NUMBER];
         while(*str && isspace(*str))          if(head.chunk.rows+1==append_here) {
                 str++;                  size_t size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
         if(!*str)                  memcpy(buf, head.chunk.rows[0].item.ptr, size);
                   buf[size]=0;
                   cstr=buf;
           } else
                   cstr=this->cstr();
           while(*cstr && isspace(*cstr))
                   cstr++;
           if(!*cstr)
                 return 0;                  return 0;
   
         char *error_pos;          char *error_pos;
         // 0xABC          // 0xABC
         if(str[0]=='0')          if(cstr[0]=='0')
                 if(str[1]=='x' || str[1]=='X')                  if(cstr[1]=='x' || cstr[1]=='X')
                         result=(int)(unsigned long)strtol(str, &error_pos, 0);                          result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
                 else                  else
                         result=(int)strtol(str+1/*skip leading 0*/, &error_pos, 0);                          result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
         else          else
                 result=(int)strtol(str, &error_pos, 0);                  result=(int)strtol(cstr, &error_pos, 0);
   
         while(char c=*error_pos++)          while(char c=*error_pos++)
                 if(!isspace(c))                  if(!isspace(c))
Line 507  int String::as_int() const { Line 775  int String::as_int() const {
         return result;          return result;
 }  }
   
 inline void uint2uchars(uint word, uchar *bytes) {  inline void ushort2uchars(ushort word, uchar& byte1, uchar& byte2) {
         bytes[0]=word&0xFF;          byte1=word&0xFF;
         bytes[1]=(word>>8)&0xFF;          byte2=word>>8;
         bytes[2]=(word>>16)&0xFF;  }
         bytes[3]=(word>>24)&0xFF;  inline ushort uchars2ushort(uchar byte1, uchar byte2) {
 }          return (byte2<<8) | byte1;
 inline uint uchars2uint(uchar *bytes) {  }
         return bytes[3]<<24  /* @todo maybe network order worth spending some effort?
                 | bytes[2]<<16          don't bothering myself with network byte order,
                 | bytes[1]<<8          am not planning to be able to move resulting file across platforms
                 | bytes[0];          for now
 }  */
   void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
 static int serialize_body_piece(const char* s, char** cur) {          buf_size=
         size_t length=strlen(s);                  prolog_size
         memcpy(*cur, s, length);  *cur+=length;                  +used_rows()*(sizeof(uchar)+sizeof(ushort))
         return 0;                  +size();
 };          buf=malloc(buf_size,15);
 String::C String::serialize(size_t prolog_length) const {          char *cur=(char *)buf+prolog_size;
         size_t buf_length=  
                 prolog_length  
                 +fragments.count()*(sizeof(Language)+sizeof(size_t))  
                 +length();  
         C result(new(PointerFreeGC) char[buf_length], buf_length);  
   
         // 1: prolog  
         char *cur=result.str+prolog_length;  
   
   
         // 2: fragments.count  
         size_t fragments_count=fragments.count();  
         memcpy(cur, &fragments_count, sizeof(fragments_count));  cur+=sizeof(fragments_count);  
   
         // 3: lang info  
         for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); ) {  
                 const Fragment& fragment=i.next();  
                 // lang  
                 memcpy(cur, &fragment.lang, sizeof(fragment.lang));  cur+=sizeof(fragment.lang);  
                 // length  
                 memcpy(cur, &fragment.length, sizeof(fragment.length));  cur+=sizeof(fragment.length);  
         }  
   
         // 4: letters  
         body.for_each(serialize_body_piece, &cur);  
   
         return result;          STRING_FOREACH_ROW(
                   // lang
                   memcpy(cur, &row->item.lang, sizeof(uchar));
                   cur+=sizeof(uchar);
                   // size
                   uchar byte1; uchar byte2;
                   ushort2uchars(row->item.size, byte1, byte2);
                   memcpy(cur, &byte1, sizeof(uchar)); cur+=sizeof(uchar);
                   memcpy(cur, &byte2, sizeof(uchar)); cur+=sizeof(uchar);
                   // bytes
                   memcpy(cur, row->item.ptr, row->item.size);
                   cur+=row->item.size;
           );
 }  }
 bool String::deserialize(size_t prolog_length, void *buf, size_t buf_length) {  bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
         if(buf_length<=prolog_length)          if(buf_size<=prolog_size)
                 return false;                  return false;
         buf_length-=prolog_length;  
   
         // 1: prolog  
         const char* cur=(const char* )buf+prolog_length;  
   
         // 2: fragments.count          char *cur=(char *)buf+prolog_size;
         if(buf_length<sizeof(size_t)) // fragments.count don't fit?          buf_size-=prolog_size;
                 return false;  
         size_t fragments_count=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);  
         buf_length-=sizeof(size_t);  
   
         // 3: lang info          while(buf_size) {
         size_t total_length=0;                  if(sizeof(uchar)+sizeof(ushort)>buf_size) // lang+size
         for(size_t f=0; f<fragments_count; f++) {  
                 size_t piece_length=sizeof(Language)+sizeof(size_t);  
                 if(buf_length<piece_length) // lang+length  
                         return false;                          return false;
   
                 Language lang=*reinterpret_cast<const Language *>(cur);  cur+=sizeof(Language);                  uchar lang=*(uchar *)(cur);             
                 size_t fragment_length=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);                  ushort size=uchars2ushort(
                 fragments+=Fragment(lang, fragment_length);                          *(uchar*)(cur+sizeof(uchar)*1),
                 total_length+=fragment_length;                          *(uchar*)(cur+sizeof(uchar)*2)
                   );
   
                 buf_length-=piece_length;                  size_t piece_size=sizeof(uchar)+sizeof(ushort)+size;
         }                  if(piece_size>buf_size) // buffer overrun, can be on incomplete cache files
                           return false;
         // 4: letters  
         if(buf_length!=total_length)  
                 return false;  
   
         body=CORD_cat_char_star(CORD_EMPTY, cur, buf_length);                  const char *ptr=(const char*)(cur+sizeof(uchar)*3); 
                   APPEND(ptr, size, lang, file, 0);
   
                   cur+=piece_size;
                   buf_size-=piece_size;
           }
         return true;          return true;
 }  }

Removed from v.1.172.2.21.2.26  
changed lines
  Added in v.1.173


E-mail: