Diff for /parser3/src/main/pa_string.C between versions 1.166 and 1.172.2.21.2.8

version 1.166, 2002/08/07 13:44:00 version 1.172.2.21.2.8, 2003/03/20 13:09:54
Line 1 Line 1
 /** @file  /** @file
         Parser: string class. @see untasize_t.C.          Parser: string class. @see untasize_t.C.
   
         Copyright (c) 2001, 2002 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"
   
 #define DEBUG_STRING_APPENDS_VS_EXPANDS  // helpers
   
   /// String::match uses this as replace & global search table columns
   
 #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS  const int MAX_MATCH_GROUPS=100;
 ulong string_piece_appends=0;  
 #endif  
   
 String& String::OnPool(Pool& apool, const char *local_src, size_t src_size, bool tainted) {  class String_match_table_template_columns: public ArrayString {
         if(local_src && *local_src) {  public:
                 if(src_size==0)          String_match_table_template_columns() {
                         src_size=strlen(local_src);                  *this+=new String("prematch");
                                   *this+=new String("match");
                 char *pooled_src=(char *)apool.malloc(src_size);                  *this+=new String("postmatch");
                 memcpy(pooled_src, local_src, src_size);                  for(int i=0; i<MAX_MATCH_GROUPS; i++) {
                 return *new(apool) String(apool, pooled_src, src_size, tainted);                          char *cname=new(PointerFreeGC) char[3/*strlen("100")*/+1/*terminating 0*/];
         } else                          *this+=new String(cname, sprintf(cname, "%d", 1+i));
                 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)  Table string_match_table_template(new String_match_table_template_columns);
                 if(tainted)  
                         APPEND_TAINTED(src, src_size, 0, 0);  
                 else  
                         APPEND_CLEAN(src, src_size, 0, 0);  
 }  
   
 String::String(const String& src) :       // String::ArrayFragment methods
         Pooled(src.pool()) {  
         last_chunk=&head.chunk;  
         head.chunk.count=CR_PREALLOCATED_COUNT;  
         append_here=head.chunk.rows;  
   
         append(src, UL_UNSPECIFIED);  String::ArrayFragment& String::ArrayFragment::append(const ArrayFragment& src, 
 }                                                       size_t substr_start, size_t substr_end) {
           if(substr_start==substr_end)
                   return *this;
   
 size_t  String::size() const {          size_t fragment_start=0;
         size_t result=0;          size_t fragment_end;
         STRING_FOREACH_ROW(          for(Array_iterator<element_type> i(src); ; fragment_start=fragment_end) {
                         result+=row->item.size;                  const element_type& fragment=i.next();
         );                  fragment_end=fragment_start+fragment.size;              
         return result;  
 }                  if(substr_start<fragment_start) // 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_start);
                   
                   while(substr_end>fragment_end) { // are there more fragments including pieces of 'substr'?
                           const element_type& fragment=i.next();
                           fragment_end=fragment_start+fragment.size;
   
                           if(substr_end>fragment_end) // are there still more?
                                   simple_append(Fragment(fragment.lang, fragment.size)); // appending whole fragment
                           else { // no, it was last
                                   simple_append(Fragment(fragment.lang, substr_end-fragment_start));
                                   goto break2;
                           }
                   }
   
 /// @todo not very optimal                  break;
 uint String::used_rows() const {          }       
         uint result=0;  break2:
         STRING_FOREACH_ROW(  
                 result++;  
         );  
         return result;  
 }  
 void String::expand() {  
         uint new_chunk_count=last_chunk->count+CR_GROW_COUNT;  
         if(new_chunk_count>max_integral(Chunk::count_type))  
                 new_chunk_count=max_integral(Chunk::count_type);  
   
         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;          return *this;
         append_here=last_chunk->rows;  
 }  }
   /*
   void String::ArrayFragment::mid(ArrayFragment& result, size_t substr_start, size_t substr_end) {
   }
   */
   
 String& String::real_append(STRING_APPEND_PARAMS) {  // String methods
         if(!last_chunk) // growth stopped [we're appended as string to somebody]  
                 throw Exception(0,  String::String(const char* src, size_t src_size, bool tainted): body(CORD_EMPTY) {
                         this,          if(src)
                         "string growth stopped (append cstr)");                  if(tainted)
                           APPEND_TAINTED(src, src_size);
                   else
                           APPEND_CLEAN(src, src_size);
   }
   
   String::String(const String& src): body(src.body), fragments(src.fragments) {}
   
   String& String::append(const char* src, size_t size, Untaint_lang lang) {
         if(!src)          if(!src)
                 return *this;                  return *this;
         if(!size)          if(!size)
Line 107  String& String::real_append(STRING_APPEN Line 99  String& String::real_append(STRING_APPEN
         if(!size)          if(!size)
                 return *this;                  return *this;
   
 #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS          body=CORD_cat_char_star(body, src, size);
         string_piece_appends++;          fragments+=Fragment(lang, size);
 #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++;  
   
                 src+=max_integral(Chunk::Row::item_size_type);  
                 size-=max_integral(Chunk::Row::item_size_type);  
         }  
   
         if(chunk_is_full())  
                 expand();  
   
         append_here->item.ptr=src;  
         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++;  
   
         return *this;          return *this;
 }  }
   
 char String::first_char() const {  static int CORD_batched_iter_fn_generic_hash_code(const char*  s, void * client_data) {
         if(is_empty())          uint& result=*static_cast<uint*>(client_data);
                 throw Exception(0,          generic_hash_code(result, s);
                         this,          return 0;
                         "getting first char of empty string");  };
   
         return *head.chunk.rows[0].item.ptr;  
 }  
   
 uint String::hash_code() const {  uint String::hash_code() const {
         uint result=0;          uint result=0;
         STRING_FOREACH_ROW(          CORD_iter5(body, 0, 0, CORD_batched_iter_fn_generic_hash_code, &result);
                         result=Hash::generic_code(result, row->item.ptr, row->item.size);  
         );  
         return result;          return result;
 }  }
   
 /// @todo move 'lang' skipping to pos  const String& String::mid(size_t substr_start, size_t substr_end) const {
 int String::cmp(int& partial, const String& src,           String& result=*new String;
                                 size_t this_offset, Untaint_lang lang) const {  
         partial=-1;  
         size_t a_size=size();  
         this_offset=min(this_offset, a_size-1);  
   
         const Chunk *a_chunk=&head.chunk;  
         const Chunk *b_chunk=&src.head.chunk;  
         const Chunk::Row *a_row=a_chunk->rows;  
         const Chunk::Row *b_row=b_chunk->rows;  
         size_t a_offset=this_offset;  
         size_t b_offset=0;  
         Chunk::Row *a_end=append_here;  
         Chunk::Row *b_end=src.append_here;  
         uint a_countdown=a_chunk->count;  
         uint b_countdown=b_chunk->count;  
         int result;  
         size_t pos=0;   
   
         bool a_break=a_size==0;          substr_start=min(substr_start, size());
         bool b_break=src.is_empty();          substr_end=max(substr_start, substr_end);
         if(!(a_break || b_break)) while(true) {          if(substr_start==substr_end)
                 if(pos+a_row->item.size > this_offset) {                  return result;
                         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_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(a_break=a_row==a_end) {          // first: letters themselves
                         b_break=b_row==b_end;          result.body=CORD_substr(body, substr_start, substr_end-substr_start);
                         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;  
         }  
 }  
   
 /// @todo move 'lang' skipping to pos          // next: their langs
 int String::cmp(int& partial, const char* b_ptr, size_t src_size,           result.fragments.append(fragments, substr_start, substr_end);
                                 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--;   
                 }  
   
                 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(finish, start);  
         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'",  //      SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
                 //cstr(), start, finish, result.cstr());                  //cstr(), substr_start, substr_end, result.cstr());
         return result;          return result;
 }  }
   
 int String::pos(const String& substr,   size_t String::pos(CORD substr, 
                                 int result, Untaint_lang lang) const {                     size_t this_offset, Untaint_lang lang) const {
         size_t self_size=size();          // first: letters themselves
         for(; size_t(result)<self_size; result++) {          size_t substr_start=CORD_str(body, this_offset, substr);
                 int partial; cmp(partial, substr, result, lang);          if(substr_start==CORD_NOT_FOUND)
                 if(                  return STRING_NOT_FOUND;
                         partial==0 || // full match  
                         partial==2) // 'substr' starts 'this'+'result'          // next: check the lang when specified
                         return result;  
         }          if(lang==UL_UNSPECIFIED) // ignore lang?
                           return substr_start;
         return -1;  
           // substr must be in one fragment, and fragments' lang must = lang
           size_t substr_end=substr_start+CORD_len(substr);
           size_t fragment_start=0;
           size_t fragment_end;
           for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); fragment_start=fragment_end) {
                   const Fragment& fragment=i.next();
                   fragment_end=fragment_start+fragment.size;
   
                   if(substr_start<fragment_start) // not reached fragments which may include 'result'?
                           continue;
                   
                   if(substr_end>fragment_end) // end of substr OUT of current fragment?
                           break;
   
                   return fragment.lang==lang?substr_end:STRING_NOT_FOUND;
           }       
   
           return STRING_NOT_FOUND;
 }  }
   
 int String::pos(const char *substr, size_t substr_size,   size_t String::pos(const String& substr, 
                                 int result, Untaint_lang lang) const {                                  size_t this_offset, Untaint_lang lang) const {
         size_t self_size=size();          return pos(substr.body, this_offset, lang);
         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(Array& result,   void String::split(ArrayString& result, 
                                    size_t* pos_after_ref,                                      size_t& pos_after, 
                                    const char *delim, size_t delim_size,                                      const char* delim, 
                                    Untaint_lang lang, int limit) const {                                     Untaint_lang lang, int limit) {
         size_t self_size=size();          size_t self_size=size();
         if(delim_size) {          if(size_t delim_size=strlen(delim)) {
                 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, delim_size, 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_size;                          pos_after=pos_before+delim_size;
                 }                  }
Line 417  void String::split(Array& result, Line 189  void String::split(Array& result,
                         result+=&mid(pos_after, self_size);                          result+=&mid(pos_after, self_size);
                         pos_after=self_size;                          pos_after=self_size;
                 }                  }
                 if(pos_after_ref)  
                         *pos_after_ref=pos_after;  
         } else { // empty delim          } else { // empty delim
                 result+=this;                  result+=this;
                 if(pos_after_ref)                  pos_after+=self_size;
                         *pos_after_ref+=self_size;  
         }          }
 }  }
   
 void String::split(Array& result,   void String::split(ArrayString& result, 
                                    size_t* pos_after_ref,                                      size_t& pos_after, 
                                    const String& delim, Untaint_lang lang,                                      const String& delim, Untaint_lang lang, 
                                    int limit) const {                                     int limit) const {
         if(!delim.is_empty()) {          if(delim) {
                 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--) {
Line 443  void String::split(Array& result, Line 211  void String::split(Array& result,
                         result+=&mid(pos_after, size());                          result+=&mid(pos_after, size());
                         pos_after=size();                          pos_after=size();
                 }                  }
                 if(pos_after_ref)  
                         *pos_after_ref=pos_after;  
         } else { // empty delim          } else { // empty delim
                 result+=this;                  result+=this;
                 if(pos_after_ref)                  pos_after+=size();
                         *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 468  static void regex_options(const String * Line 233  static void regex_options(const String *
                 {"'", 0, 0, 0, 0, &need_pre_post_match},                  {"'", 0, 0, 0, 0, &need_pre_post_match},
                 {0}                  {0}
     };      };
         result[0]=PCRE_EXTRA | PCRE_DOTALL;          result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY;
         result[1]=0;          result[1]=0;
   
     if(options)       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)) {
                                 if(o->flag)                                  if(o->flag)
                                         *o->flag=true;                                          *o->flag=true;
                                 else { // result                                  else { // result
Line 484  static void regex_options(const String * Line 249  static void regex_options(const String *
                         }                          }
 }  }
   
 /// @todo make replacement Table stacked  Table* String::match(Charset& source_charset,
 bool String::match(                                             const String& regexp, 
                                    const String *aorigin,                                             const String& options,
                                    const String& regexp,                                              Row_action row_action, void *info,
                                    const String *options,                                             bool& just_matched) const { 
                                    Table **table,          if(!regexp)
                                    Row_action row_action, void *info,  
                                    bool *was_global) const {   
   
         if(regexp.is_empty())  
                 throw Exception(0,                  throw Exception(0,
                         aorigin,                          0,
                         "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);
         if(was_global)          bool global=option_bits[1]!=0;
                 *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,
                 pool().get_source_charset().pcre_tables);                  source_charset.pcre_tables);
   
         if(!code)          if(!code)
                 throw Exception(0,                  throw Exception(0,
                         &regexp.mid(erroffset, regexp.size()),                          &regexp.mid(erroffset, regexp.size()),
                         "regular expression syntax error - %s", errptr);                          "regular expression syntax error - %s", errptr);
                   
         int info_substrings=pcre_info(code, 0, 0);          int subpatterns=pcre_info(code, 0, 0);
         if(info_substrings<0) {          if(subpatterns<0) {
                 pcre_free(code);                  pcre_free(code);
                 throw Exception(0,                  throw Exception(0,
                         aorigin,                          &regexp,
                         "pcre_info error (%d)",                           "pcre_info error (%d)", 
                                 info_substrings);                                  subpatterns);
         }          }
   
         const char *subject=cstr();          const char* subject=cstr();
         int length=strlen(subject);          size_t subject_size=strlen(subject);
         const int ovecsize=(1/*match*/+MAX_STRING_MATCH_TABLE_COLUMNS)*3;          const int ovecsize=(1/*match*/+MAX_MATCH_GROUPS)*3;
         int ovector[ovecsize];          int ovector[ovecsize];
   
         // create table          // create table
         *table=NEW Table(pool(), *string_match_table_template);          Table* table=new Table(string_match_table_template);
   
         int exec_option_bits=0;          int exec_option_bits=0;
         int prestart=0;          int prestart=0;
Line 537  bool String::match( Line 297  bool String::match(
         int postfinish=size();          int postfinish=size();
         while(true) {          while(true) {
                 int exec_substrings=pcre_exec(code, 0,                  int exec_substrings=pcre_exec(code, 0,
                         subject, length, prestart,                          subject, subject_size, prestart,
                         exec_option_bits, ovector, ovecsize);                          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 row*/, 0, 0, poststart, postfinish, info);                          row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);
                         return option_bits[1]!=0; // global=true+table, not global=false                          if(global || subpatterns)
                                   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,
                                 aorigin,                                  &regexp,
                                 "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];
                 Array& row=*NEW Array(pool());                  ArrayString* row=new ArrayString;
                 row+=need_pre_post_match?&mid(0, prefinish):0; // .prematch column value                  if(need_pre_post_match) {
                 row+=need_pre_post_match?&mid(prefinish, poststart):0; // .match                          *row+=&mid(0, prefinish); // .prematch column value
                 row+=need_pre_post_match?&mid(poststart, postfinish):0; // .postmatch                          *row+=&mid(prefinish, poststart); // .match
                           *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(!option_bits[1] || prestart==poststart) { // not global | going to hang                  if(!global || 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 true;                          return table;
                 }                  }
                 prestart=poststart;                  prestart=poststart;
   
Line 582  bool String::match( Line 352  bool String::match(
         }          }
 }  }
   
 String& String::change_case(Pool& pool,   String& String::change_case(Charset& source_charset, Change_case_kind kind) {
                                                         Change_case_kind kind) const {          String& result=*new String();
         const unsigned char *tables=pool.get_source_charset().pcre_tables;  
         String& result=*new(pool) String(pool);          const unsigned char *tables=source_charset.pcre_tables;
   
         const unsigned char *a;          const unsigned char *a;
         const unsigned char *b;          const unsigned char *b;
Line 607  String& String::change_case(Pool& pool, Line 377  String& String::change_case(Pool& pool,
                 break; // never                  break; // never
         }                 }       
   
         STRING_FOREACH_ROW(          char* new_cstr=cstrm();
                 char *new_cstr=(char *)pool.malloc(row->item.size, 12);          char *dest=new_cstr;
                 char *dest=new_cstr;          unsigned char index;
                 const char *src=row->item.ptr;           for(const char* current=new_cstr; index=(unsigned char)*current; current++) {
                 for(int size=row->item.size; size--; src++) {                  unsigned char c=a[index];
                         unsigned char c=a[(unsigned char)*src];                  if(b)
                         if(b)                          c=b[c];
                                 c=b[c];  
   
                         *dest++=(char)c;                  *dest++=(char)c;
                 }          }
                           result.body=new_cstr;
                 result.APPEND(new_cstr, row->item.size,           result.fragments.append(fragments, 0, fragments.count());
                         row->item.lang,  
                         row->item.origin.file, row->item.origin.line);  
         );  
   
         return result;          return result;
 }  }
   
 /// @test if in some piece were found no dict words, append it, not it's duplicate  const String& String::replace(const Dictionary& dict) const {
 String& String::replace(Pool& pool, Dictionary& dict) const {          String& result=*new String();
 //      return reconstruct(pool).replace_in_reconstructed(pool, dict);          const char* old_cstr=cstr();
         String& result=*new(pool) String(pool);          const char* prematch_start=old_cstr;
   
         STRING_FOREACH_ROW(          for(const char* current=old_cstr; *current; ) {
                 const char *src=row->item.ptr;                   if(Table::element_type row=dict.first_that_starts(current)) {
                 size_t src_size=row->item.size;                          // prematch
                 char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);                          if(size_t prematch_size=current-prematch_start) {
                 char *dest=new_cstr;                                  result.body=CORD_cat_char_star(result.body, prematch_start, prematch_size);
                 while(src_size) {                                  result.fragments.append(fragments, prematch_start-old_cstr, prematch_size);
                         // there is a row where first column starts 'src'  
                         if(Table::Item *item=dict.first_that_starts(src, src_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 'src' && reduce work size  
                                 src+=a.size();  src_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++=*src++;  src_size--;  
                         }                          }
                 }  
   
                 result.APPEND(new_cstr, dest-new_cstr, row->item.lang,                          // match
                         row->item.origin.file, row->item.origin.line);  
         );  
         return result;  
 }  
   
 String& String::join_chains(Pool& pool, char** acstr) const {                          const String* a=row->get(0);
         char *lcstr=cstr();                          // skip 'a' in 'current'; move prematch_start
         const char *current=lcstr;                          current+=a->size(); prematch_start=current;
   
         String& result=*new(pool) String(pool);                          if(row->count()>1) { // are there any b?
         STRING_FOREACH_ROW(                                  const String* b=row->get(1);
 IFNDEF_NO_STRING_ORIGIN(                                  if(size_t b_size=b->size()) {
                 const char *joined_origin_file=row->item.origin.file;                                          result.body=CORD_cat(result.body, b->cstr_to_cord());
                 const size_t joined_origin_line=row->item.origin.line;                                          result.fragments.append(b->fragments, 0, b->fragments.count());
 );                                  }
                 uchar joined_lang=row->item.lang;                          }
                 const char *joined_ptr=current;                  } else // simply advance
                 // calc size                          current++; 
                 size_t joined_size=0;          }
                 STRING_PREPARED_FOREACH_ROW(*this,   
                         if(row->item.lang==joined_lang)          // postmatch
                                 joined_size+=row->item.size;          if(size_t prematch_size=current-prematch_start) {
                         else                  result.body=CORD_cat_char_star(result.body, prematch_start, prematch_size);
                                 break; // before non-ours                  result.fragments.append(fragments, prematch_start-old_cstr, prematch_size);
                 );          }
                 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 *cstr;          const char *str=cstr();
         char buf[MAX_NUMBER];  
         if(head.chunk.rows+1==append_here) {          while(*str && isspace(*str))
                 int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);                  str++;
                 memcpy(buf, head.chunk.rows[0].item.ptr, size);          if(!*str)
                 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(cstr[0]=='0')          if(str[0]=='0')
                 if(cstr[1]=='x' || cstr[1]=='X')                  if(str[1]=='x' || str[1]=='X')
                         result=(double)(unsigned long)strtol(cstr, &error_pos, 0);                          result=(double)(unsigned long)strtol(str, &error_pos, 0);
                 else                  else
                         result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);                          result=(double)strtod(str+1/*skip leading 0*/, &error_pos);
         else          else
                 result=(double)strtod(cstr, &error_pos);                  result=(double)strtod(str, &error_pos);
   
         while(char c=*error_pos++)          while(char c=*error_pos++)
                 if(!isspace(c))                  if(!isspace(c))
Line 730  double String::as_double() const { Line 461  double String::as_double() const {
 }  }
 int String::as_int() const {   int String::as_int() const { 
         int result;          int result;
         const char *cstr;          const char *str=cstr();
         char buf[MAX_NUMBER];  
         if(head.chunk.rows+1==append_here) {          while(*str && isspace(*str))
                 size_t size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);                  str++;
                 memcpy(buf, head.chunk.rows[0].item.ptr, size);          if(!*str)
                 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(cstr[0]=='0')          if(str[0]=='0')
                 if(cstr[1]=='x' || cstr[1]=='X')                  if(str[1]=='x' || str[1]=='X')
                         result=(int)(unsigned long)strtol(cstr, &error_pos, 0);                          result=(int)(unsigned long)strtol(str, &error_pos, 0);
                 else                  else
                         result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);                          result=(int)strtol(str+1/*skip leading 0*/, &error_pos, 0);
         else          else
                 result=(int)strtol(cstr, &error_pos, 0);                  result=(int)strtol(str, &error_pos, 0);
   
         while(char c=*error_pos++)          while(char c=*error_pos++)
                 if(!isspace(c))                  if(!isspace(c))
Line 763  int String::as_int() const { Line 487  int String::as_int() const {
         return result;          return result;
 }  }
   
 inline void ushort2uchars(ushort word, uchar& byte1, uchar& byte2) {  inline void uint2uchars(uint word, uchar *bytes) {
         byte1=word&0xFF;          bytes[0]=word&0xFF;
         byte2=word>>8;          bytes[1]=(word>>8)&0xFF;
 }          bytes[2]=(word>>16)&0xFF;
 inline ushort uchars2ushort(uchar byte1, uchar byte2) {          bytes[3]=(word>>24)&0xFF;
         return (byte2<<8) | byte1;  }
 }  inline uint uchars2uint(uchar *bytes) {
 /* @todo maybe network order worth spending some effort?          return bytes[3]<<24
         don't bothering myself with network byte order,                  | bytes[2]<<16
         am not planning to be able to move resulting file across platforms                  | bytes[1]<<8
         for now                  | bytes[0];
 */  }
 void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {  
         buf_size=  static int CORD_batched_iter_fn_append(const char* s, void* client_data) {
           char*& cur=*static_cast<char**>(client_data);
   
           size_t size=strlen(s);
           memcpy(cur, s, size);  cur+=size;
           return 0;
   };
   String::C String::serialize(size_t prolog_size) const {
           size_t buf_size=
                 prolog_size                  prolog_size
                 +used_rows()*(sizeof(uchar)+sizeof(ushort))                  +fragments.count()*(sizeof(Untaint_lang)+sizeof(size_t))
                 +size();                  +size();
         buf=malloc(buf_size,15);          C result(new(PointerFreeGC) char[buf_size], buf_size);
         char *cur=(char *)buf+prolog_size;  
   
         STRING_FOREACH_ROW(          // 1: prolog
           char *cur=result.str+prolog_size;
   
   
           // 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                  // lang
                 memcpy(cur, &row->item.lang, sizeof(uchar));                  memcpy(cur, &fragment.lang, sizeof(fragment.lang));  cur+=sizeof(fragment.lang);
                 cur+=sizeof(uchar);  
                 // size                  // size
                 uchar byte1; uchar byte2;                  memcpy(cur, &fragment.size, sizeof(fragment.size));  cur+=sizeof(fragment.size);
                 ushort2uchars(row->item.size, byte1, byte2);          }
                 memcpy(cur, &byte1, sizeof(uchar)); cur+=sizeof(uchar);  
                 memcpy(cur, &byte2, sizeof(uchar)); cur+=sizeof(uchar);          // 4: letters
                 // bytes          CORD_iter5(body, 0, 0, CORD_batched_iter_fn_append, &cur);
                 memcpy(cur, row->item.ptr, row->item.size);  
                 cur+=row->item.size;          return result;
         );  
 }  }
 bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {  bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char* file) {
         if(buf_size<=prolog_size)          if(buf_size<=prolog_size)
                 return false;                  return false;
   
         char *cur=(char *)buf+prolog_size;  
         buf_size-=prolog_size;          buf_size-=prolog_size;
   
         while(buf_size) {          // 1: prolog
                 if(sizeof(uchar)+sizeof(ushort)>buf_size) // lang+size          const char* cur=(const char* )buf+prolog_size;
                         return false;  
   
                 uchar lang=*(uchar *)(cur);                       // 2: fragments.count
                 ushort size=uchars2ushort(          if(buf_size<sizeof(size_t)) // fragments.count don't fit?
                         *(uchar*)(cur+sizeof(uchar)*1),                  return false;
                         *(uchar*)(cur+sizeof(uchar)*2)          size_t fragments_count=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
                 );          buf_size-=sizeof(size_t);
   
                 size_t piece_size=sizeof(uchar)+sizeof(ushort)+size;          // 3: lang info
                 if(piece_size>buf_size) // buffer overrun, can be on incomplete cache files          size_t total_size=0;
           for(size_t f=0; f<fragments_count; f++) {
                   size_t piece_size=sizeof(Untaint_lang)+sizeof(size_t);
                   if(buf_size<piece_size) // lang+size
                         return false;                          return false;
   
                 const char *ptr=(const char*)(cur+sizeof(uchar)*3);                   Untaint_lang lang=*reinterpret_cast<const Untaint_lang *>(cur);  cur+=sizeof(Untaint_lang);
                 APPEND(ptr, size, lang, file, 0);                  size_t fragment_size=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
                   fragments+=Fragment(lang, fragment_size);
                   total_size+=fragment_size;
   
                 cur+=piece_size;  
                 buf_size-=piece_size;                  buf_size-=piece_size;
         }          }
   
           // 4: letters
           if(buf_size!=total_size)
                   return false;
   
           body=CORD_cat_char_star(CORD_EMPTY, cur, buf_size);
   
         return true;          return true;
 }  }

Removed from v.1.166  
changed lines
  Added in v.1.172.2.21.2.8


E-mail: