Diff for /parser3/src/main/pa_string.C between versions 1.78 and 1.105

version 1.78, 2001/04/23 13:38:31 version 1.105, 2001/10/05 14:48:58
Line 2 Line 2
         Parser: string class. @see untasize_t.C.          Parser: string class. @see untasize_t.C.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
   
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
   
         $Id$          $Id$
Line 11 Line 10
 #include "pa_config_includes.h"  #include "pa_config_includes.h"
   
 #include "pcre.h"  #include "pcre.h"
   #include "internal.h"
   
 #include "pa_pool.h"  #include "pa_pool.h"
 #include "pa_string.h"  #include "pa_string.h"
Line 20 Line 20
 #include "pa_array.h"  #include "pa_array.h"
 #include "pa_globals.h"  #include "pa_globals.h"
 #include "pa_table.h"  #include "pa_table.h"
 #include "pa_threads.h"  #include "pa_dictionary.h"
   
 //#include "pa_sapi.h"  
   
 // String  
   
 String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :  String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
         Pooled(apool) {          Pooled(apool),
           forigins_mode(false) {
         last_chunk=&head;          last_chunk=&head;
         head.count=CR_PREALLOCATED_COUNT;          head.count=CR_PREALLOCATED_COUNT;
         append_here=head.rows;          append_here=head.rows;
Line 42  String::String(Pool& apool, const char * Line 39  String::String(Pool& apool, const char *
                         APPEND_CLEAN(src, src_size, 0, 0);                          APPEND_CLEAN(src, src_size, 0, 0);
 }  }
   
 void String::expand() {  String::String(const String& src) :     
         size_t new_chunk_count=last_chunk->count+last_chunk->count*CR_GROW_PERCENT/100;          Pooled(src.pool()),
         last_chunk=static_cast<Chunk *>(          forigins_mode(false) {
                 malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));  
         last_chunk->count=new_chunk_count;  
         link_row->link=last_chunk;  
         append_here=last_chunk->rows;  
         link_row=&last_chunk->rows[last_chunk->count];  
         link_row->link=0;  
 }  
   
 String::String(const String& src) :     Pooled(src.pool()) {  
         head.count=CR_PREALLOCATED_COUNT;          head.count=CR_PREALLOCATED_COUNT;
                   
         size_t src_used_rows=src.fused_rows;          size_t src_used_rows=src.fused_rows;
         if(src_used_rows<=head.count) {          if(src_used_rows<=head.count) {
                 // all new rows fit size_to preallocated area                  // all new rows fit size_to preallocated area
                   last_chunk=&head;
                 size_t curr_chunk_rows=head.count;                  size_t curr_chunk_rows=head.count;
                 memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);                  memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
                 append_here=&head.rows[src_used_rows];                  append_here=&head.rows[src_used_rows];
Line 77  String::String(const String& src) : Pool Line 66  String::String(const String& src) : Pool
                 memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);                  memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
                 // remaining rows size_to new_chunk                  // remaining rows size_to new_chunk
                 size_t curr_chunk_rows=src_used_rows-head.count;                  size_t curr_chunk_rows=src_used_rows-head.count;
                 Chunk *new_chunk=static_cast<Chunk *>(                  last_chunk=static_cast<Chunk *>(
                         malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));                          malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
                 new_chunk->count=curr_chunk_rows;                  last_chunk->count=curr_chunk_rows;
                 head.preallocated_link=new_chunk;                  head.preallocated_link=last_chunk;
                 append_here=link_row=&new_chunk->rows[new_chunk->count];                  append_here=link_row=&last_chunk->rows[last_chunk->count];
   
                 Chunk *old_chunk=src.head.preallocated_link;                   Chunk *old_chunk=src.head.preallocated_link; 
                 Chunk::Row *new_rows=new_chunk->rows;                  Chunk::Row *new_rows=last_chunk->rows;
                 size_t rows_left_to_copy=new_chunk->count;                  size_t rows_left_to_copy=last_chunk->count;
                 while(true) {                  while(true) {
                         size_t old_count=old_chunk->count;                          size_t old_count=old_chunk->count;
                         Chunk *next_chunk=old_chunk->rows[old_count].link;                          Chunk *next_chunk=old_chunk->rows[old_count].link;
Line 110  String::String(const String& src) : Pool Line 99  String::String(const String& src) : Pool
         fsize=src.fsize;          fsize=src.fsize;
 }  }
   
   void String::expand() {
           size_t new_chunk_count=last_chunk->count+CR_GROW_COUNT;
           last_chunk=static_cast<Chunk *>(
                   malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
           last_chunk->count=new_chunk_count;
           link_row->link=last_chunk;
           append_here=last_chunk->rows;
           link_row=&last_chunk->rows[last_chunk->count];
           link_row->link=0;
   }
   
 String& String::append(const String& src, Untaint_lang lang, bool forced) {  String& String::append(const String& src, Untaint_lang lang, bool forced) {
         const Chunk *chunk=&src.head;           const Chunk *chunk=&src.head; 
         do {          do {
Line 151  String& String::real_append(STRING_APPEN Line 151  String& String::real_append(STRING_APPEN
         return *this;          return *this;
 }  }
   
   char String::first_char() const {
           if(!fused_rows)
                   THROW(0, 0,
                           this,
                           "getting first char of empty string");
   
           return *head.rows[0].item.ptr;
   }
   
 uint String::hash_code() const {  uint String::hash_code() const {
         uint result=0;          uint result=0;
   
Line 186  int String::cmp(int& partial, const Stri Line 195  int String::cmp(int& partial, const Stri
         Chunk::Row *b_end=src.append_here;          Chunk::Row *b_end=src.append_here;
         size_t a_countdown=a_chunk->count;          size_t a_countdown=a_chunk->count;
         size_t b_countdown=b_chunk->count;          size_t b_countdown=b_chunk->count;
         bool a_break=false;  
         bool b_break=false;  
         size_t result;          size_t result;
         size_t pos=0;           size_t pos=0; 
         while(true) {  
                 a_break=a_row==a_end;  
                 b_break=b_row==b_end;  
                 if(a_break || b_break)  
                         break;  
   
           bool a_break=size()==0;
           bool b_break=src.size()==0;
           if(!(a_break || b_break)) while(true) {
                 if(pos+a_row->item.size > this_offset) {                  if(pos+a_row->item.size > this_offset) {
                         if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang)                           if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
                                 return -1; // wrong lang -- bail out                                  return -1; // wrong lang -- bail out
Line 228  int String::cmp(int& partial, const Stri Line 233  int String::cmp(int& partial, const Stri
                                 pos+=a_row->item.size;                                  pos+=a_row->item.size;
                                 a_row++; a_countdown--; a_offset=0;                                  a_row++; a_countdown--; a_offset=0;
                         }                          }
                                                   if(b_break=b_row==b_end) {
                                   a_break=a_row==a_end;
                                   break;                  
                           }
                         if(!b_countdown) {                          if(!b_countdown) {
                                 b_chunk=b_row->link;                                  b_chunk=b_row->link;
                                 b_row=b_chunk->rows;                                  b_row=b_chunk->rows;
Line 240  int String::cmp(int& partial, const Stri Line 248  int String::cmp(int& partial, const Stri
                         a_row++; a_countdown--;                           a_row++; a_countdown--; 
                 }                  }
   
                   if(a_break=a_row==a_end) {
                           b_break=b_row==b_end;
                           break;
                   }
                 if(!a_countdown) {                  if(!a_countdown) {
                         a_chunk=a_row->link;                          a_chunk=a_row->link;
                         a_row=a_chunk->rows;                          a_row=a_chunk->rows;
Line 268  int String::cmp(int& partial, const char Line 280  int String::cmp(int& partial, const char
         size_t b_offset=0;          size_t b_offset=0;
         Chunk::Row *a_end=append_here;          Chunk::Row *a_end=append_here;
         size_t a_countdown=a_chunk->count;          size_t a_countdown=a_chunk->count;
         bool a_break=false;  
         bool b_break=false;  
         size_t pos=0;          size_t pos=0;
         while(true) {  
                 a_break=a_row==a_end;  
                 if(a_break || b_break)  
                         break;  
   
           bool a_break=size()==0;
           bool b_break=b_size==0;
           if(!(a_break || b_break)) while(true) {
                 if(pos+a_row->item.size > this_offset) {                  if(pos+a_row->item.size > this_offset) {
                         if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang)                           if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
                                 return -1; // wrong lang -- bail out                                  return -1; // wrong lang -- bail out
Line 311  int String::cmp(int& partial, const char Line 320  int String::cmp(int& partial, const char
                         a_row++; a_countdown--;                           a_row++; a_countdown--; 
                 }                  }
   
                   a_break=a_row==a_end;
                   if(a_break || b_break)
                           break;
                 if(!a_countdown) {                  if(!a_countdown) {
                         a_chunk=a_row->link;                          a_chunk=a_row->link;
                         a_row=a_chunk->rows;                          a_row=a_chunk->rows;
Line 328  int String::cmp(int& partial, const char Line 340  int String::cmp(int& partial, const char
   
 #ifndef NO_STRING_ORIGIN  #ifndef NO_STRING_ORIGIN
 const Origin& String::origin() const {   const Origin& String::origin() const { 
         if(!fused_rows)          if(!fused_rows) {
                 THROW(0, 0,                   static const Origin empty_origin={"empty string"};
                         0,                  return empty_origin;
                         "String::origin() of empty string called");          }
                   
         // determining origin by last appended piece          // determining origin by last appended piece
         // because first one frequently constant.           // because first one frequently constant. 
         // ex: ^load[/file] "document_root" + "/file"          // ex: ^load[/file] "document_root" + "/file"
         return append_here[-1].item.origin;           // when last peice is constant, 
           // ex: parser_root_auto_path{dynamic} / auto.p{const}
           // using first piece
           Origin& last_origin=append_here[-1].item.origin;
           return last_origin.file ? last_origin : head.rows[0].item.origin;
 }  }
 #endif  #endif
   
Line 463  static void regex_options(char *options, Line 479  static void regex_options(char *options,
                 int *result;                  int *result;
     } regex_option[]={      } regex_option[]={
                 {'i', 0, PCRE_CASELESS, result}, // a=A                  {'i', 0, PCRE_CASELESS, result}, // a=A
                 {'s', 0, PCRE_DOTALL, result}, // \n\n$                  {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
                 {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored                  {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                 {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$                  {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                 {'g', 0, true, result+1}, // many rows                  {'g', 0, true, result+1}, // many rows
Line 482  static void regex_options(char *options, Line 498  static void regex_options(char *options,
                         }                          }
 }  }
   
 /**  /// @todo maybe need speedup: some option to remove pre/match/post string generation
         returns true if fills table.  
         table format is defined and fixed[can be used by others]:   
         @verbatim  
                 pre-match/match/post-match/1/2/3/...  
         @endverbatim  
 */  
 bool String::match(const unsigned char *pcre_tables,  bool String::match(const unsigned char *pcre_tables,
                                    const String *aorigin,                                     const String *aorigin,
                                    const String& regexp,                                      const String& regexp, 
                                    const String *options,                                     const String *options,
                                    Table **table,                                     Table **table,
                                    Row_action row_action, void *info) const {                                      Row_action row_action, void *info,
                                      bool *was_global) const { 
   
         if(!regexp.size())          if(!regexp.size())
                 THROW(0, 0,                  THROW(0, 0,
Line 504  bool String::match(const unsigned char * Line 515  bool String::match(const unsigned char *
         const char *errptr;          const char *errptr;
         int erroffset;          int erroffset;
     int option_bits[2];  regex_options(options?options->cstr():0, option_bits);      int option_bits[2];  regex_options(options?options->cstr():0, option_bits);
           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,
                 pcre_tables);                  pcre_tables);
Line 515  bool String::match(const unsigned char * Line 528  bool String::match(const unsigned char *
                   
         int info_substrings=pcre_info(code, 0, 0);          int info_substrings=pcre_info(code, 0, 0);
         if(info_substrings<0) {          if(info_substrings<0) {
                 (*pcre_free)(code);                  pcre_free(code);
                 THROW(0, 0,                  THROW(0, 0,
                         aorigin,                          aorigin,
                         "pcre_info error (%d)",                           "pcre_info error (%d)", 
Line 549  bool String::match(const unsigned char * Line 562  bool String::match(const unsigned char *
                         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, info);                          row_action(**table, 0/*last time, no row*/, 0, 0, info);
                         return option_bits[1]!=0; // global=true+table, not global=false                          return option_bits[1]!=0; // global=true+table, not global=false
                 }                  }
   
                 if(exec_substrings<0) {                  if(exec_substrings<0) {
                         (*pcre_free)(code);                          pcre_free(code);
                         THROW(0, 0,                          THROW(0, 0,
                                 aorigin,                                  aorigin,
                                 "regular expression execute error (%d)",                                   "regular expression execute error (%d)", 
Line 563  bool String::match(const unsigned char * Line 576  bool String::match(const unsigned char *
                 }                  }
   
                 Array& row=*NEW Array(pool());                  Array& row=*NEW Array(pool());
                 row+=&mid(0, ovector[0]); // .pre-match column value                  row+=&mid(0, ovector[0]); // .prematch column value
                 row+=&mid(ovector[0], ovector[1]); // .match                  row+=&mid(ovector[0], ovector[1]); // .match
                 row+=&mid(ovector[1], size()); // .post-match                  row+=&mid(ovector[1], size()); // .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, startoffset, ovector[0], info);                  row_action(**table, &row, startoffset, ovector[0], info);
   
                 if(!option_bits[1] || !(startoffset=ovector[1])) { // not global | going to hang                  if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang
                         (*pcre_free)(code);                          pcre_free(code);
                         (*row_action)(**table, 0/*last time, no row*/, 0, 0, info);                          row_action(**table, 0/*last time, no row*/, 0, 0, info);
                         return true;                          return true;
                 }                  }
                   startoffset=ovector[1];
   
 /*  /*
                 if(option_bits[0] & PCRE_MULTILINE)                  if(option_bits[0] & PCRE_MULTILINE)
Line 586  bool String::match(const unsigned char * Line 600  bool String::match(const unsigned char *
 */  */
         }          }
 }  }
   
   String& String::change_case(Pool& pool, const unsigned char *tables, 
                                                           Change_case_kind kind) const {
           String& result=*new(pool) String(pool);
   
           const unsigned char *a;
           const unsigned char *b;
           switch(kind) {
           case CC_UPPER:
                   a=tables+lcc_offset;
                   b=tables+fcc_offset;
                   break;
           case CC_LOWER:
                   a=tables+lcc_offset;
                   b=0;
                   break;
           default:
                   PTHROW(0, 0, 
                           this, 
                           "unknown change case kind #%d", 
                                   static_cast<int>(kind)); // never
                   a=b=0; // calm, compiler
                   break; // never
           }       
   
           const Chunk *chunk=&head; 
           do {
                   const Chunk::Row *row=chunk->rows;
                   for(size_t i=0; i<chunk->count; i++, row++) {
                           if(row==append_here)
                                   goto break2;
   
                           char *new_cstr=(char *)pool.malloc(row->item.size);
                           char *dest=new_cstr;
                           const char *src=row->item.ptr; 
                           for(int size=row->item.size; size--; src++) {
                                   unsigned char c=a[(unsigned char)*src];
                                   if(b)
                                           c=b[c];
   
                                   *dest++=(char)c;
                           }
                           
                           result.APPEND(new_cstr, row->item.size, 
                                   row->item.lang,
                                   row->item.origin.file, row->item.origin.line);
                   }
                   chunk=row->link;
           } while(chunk);
   break2:
   
           return result;
   }
   
   String& String::replace(Pool& pool, Dictionary& dict) const {
           
           String& result=*new(pool) String(pool);
           const Chunk *chunk=&head; 
           do {
                   const Chunk::Row *row=chunk->rows;
                   for(size_t i=0; i<chunk->count; i++, row++) {
                           if(row==append_here)
                                   goto break2;
   
                           const char *src=row->item.ptr; 
                           size_t src_size=row->item.size;
                           char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()));
                           char *dest=new_cstr;
                           while(src_size) {
                                   // there is a row where first column starts 'src'
                                   if(Table::Item *item=dict.first_that_starts(src)) {
                                           // 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,
                                   row->item.origin.file, row->item.origin.line);
                   }
                   chunk=row->link;
           } while(chunk);
   
   break2:
           return result;
   }
   
   double String::as_double() const { 
           double result;
           const char *cstr=this->cstr();
           char *error_pos;
           // 0xABC
           if(cstr[0]=='0')
                   if(cstr[1]=='x' || cstr[1]=='X')
                           result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
                   else
                           result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
           else
                   result=(double)strtod(cstr, &error_pos);
   
           if(*error_pos/*not EOS*/)
                   THROW(0, 0,
                           this,
                           "invalid number (double)");
   
           return result;
   }
   int String::as_int() const { 
           int result;
           const char *cstr=this->cstr();
           char *error_pos;
           // 0xABC
           if(cstr[0]=='0')
                   if(cstr[1]=='x' || cstr[1]=='X')
                           result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
                   else
                           result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
           else
                   result=(int)strtol(cstr, &error_pos, 0);
   
           if(*error_pos/*not EOS*/)
                   THROW(0, 0,
                           this,
                           "invalid number (int)");
   
           return result;
   }

Removed from v.1.78  
changed lines
  Added in v.1.105


E-mail: