Diff for /parser3/src/main/pa_string.C between versions 1.172.2.6 and 1.172.2.21.2.7

version 1.172.2.6, 2003/01/27 15:07:48 version 1.172.2.21.2.7, 2003/03/20 11:11:55
Line 1 Line 1
 /** @file  /** @file
         Parser: string class. @see untasize_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_exception.h"  #include "pa_exception.h"
 #include "pa_array.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"
Line 23  static const char* IDENT_STRING_C="$Date Line 21  static const char* IDENT_STRING_C="$Date
   
 const int MAX_MATCH_GROUPS=100;  const int MAX_MATCH_GROUPS=100;
   
 class String_match_table_template_columns: public Array<ConstStringPtr> {  class String_match_table_template_columns: public ArrayString {
         Pool pool;  
 public:  public:
         String_match_table_template_columns() {          String_match_table_template_columns() {
                 *this+=ConstStringPtr(new String("prematch"));                  *this+=new String("prematch");
                 *this+=ConstStringPtr(new String("match"));                  *this+=new String("match");
                 *this+=ConstStringPtr(new String("postmatch"));                  *this+=new String("postmatch");
                 for(int i=0; i<MAX_MATCH_GROUPS; i++) {                  for(int i=0; i<MAX_MATCH_GROUPS; i++) {
                         char *cname=new(pool) char[3/*strlen("100")*/+1/*terminating 0*/];                          char *cname=new(PointerFreeGC) char[3/*strlen("100")*/+1/*terminating 0*/];
                         ConstStringPtr sname(new String(cname, sprintf(cname, "%d", 1+i)));                          *this+=new String(cname, sprintf(cname, "%d", 1+i));
                         *this+=sname;  
                 }                  }
         }          }
 };  };
   
 Table string_match_table_template(  Table string_match_table_template(new String_match_table_template_columns);
                                                                   String::undefined_origin,   
                                                                   Table::columns_type(new String_match_table_template_columns));  
   
 // methods  // String::ArrayFragment methods
   
 String::String(const char *src, size_t src_size, bool tainted): Array<String_fragment>(1), fsize(0) {  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 fragment_start=0;
           size_t fragment_end;
           for(Array_iterator<element_type> i(src); ; fragment_start=fragment_end) {
                   const element_type& fragment=i.next();
                   fragment_end=fragment_start+fragment.size;              
   
                   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;
                           }
                   }
   
                   break;
           }       
   break2:
           
           return *this;
   }
   /*
   void String::ArrayFragment::mid(ArrayFragment& result, size_t substr_start, size_t substr_end) {
   }
   */
   
   // String methods
   
   String::String(const char* src, size_t src_size, bool tainted): body(CORD_EMPTY) {
         if(src)          if(src)
                 if(tainted)                  if(tainted)
                         APPEND_TAINTED(src, src_size, 0, 0);                          APPEND_TAINTED(src, src_size, 0, 0);
Line 52  String::String(const char *src, size_t s Line 89  String::String(const char *src, size_t s
                         APPEND_CLEAN(src, src_size, 0, 0);                          APPEND_CLEAN(src, src_size, 0, 0);
 }  }
   
 String::String(const String& src): Array<String_fragment>(src.count()) {  String::String(const String& src): body(src.body), fragments(src.fragments) {}
         append(src, UL_PASS_APPENDED);  
 }  
   
 String& String::real_append(STRING_APPEND_PARAMS) {  String& String::real_append(STRING_APPEND_PARAMS) {
         if(!src)          if(!src)
Line 64  String& String::real_append(STRING_APPEN Line 99  String& String::real_append(STRING_APPEN
         if(!size)          if(!size)
                 return *this;                  return *this;
   
         if(is_full())          body=CORD_cat_char_star(body, src, size);
                 expand(fdelta);          fragments+=Fragment(lang, size);
   
         String_fragment *fragment=&felements[fused++];  
         fragment->ptr=src;  
         fragment->size=size;  
         fragment->lang=lang;  
 #ifndef NO_STRING_ORIGIN  
         fragment->origin.file=file;  
         fragment->origin.line=line;  
 #endif  
   
         return *this;          return *this;
 }  }
   
   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 String::hash_code() const {  uint String::hash_code() const {
         uint result=0;          uint result=0;
         STRING_FOREACH_FRAGMENT(          CORD_iter5(body, 0, 0, CORD_batched_iter_fn_generic_hash_code, &result);
                 result=generic_hash_code(result, fragment->ptr, fragment->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 String_fragment *a_current=felements;  
         const String_fragment *b_current=src.felements;  
         size_t a_offset=this_offset;  
         size_t b_offset=0;  
         String_fragment *a_end=felements+fused;  
         String_fragment *b_end=src.felements+src.fused;  
         int result;  
         size_t pos=0;   
   
         bool a_break;          substr_start=min(substr_start, size());
         bool b_break;          substr_end=max(substr_start, substr_end);
         while(!(          if(substr_start==substr_end)
                 (a_break=(a_current==a_end))                  return result;
                 || (b_break=(b_current==b_end)))) {  
                 if(pos+a_current->size > this_offset) {  
                         if(lang!=UL_UNSPECIFIED && a_current->lang>(String_UL)lang)   
                                 return -1; // wrong lang -- bail out  
   
                         int size_diff=  
                                 (a_current->size-a_offset)-  
                                 (b_current->size-b_offset);  
                           
                         if(size_diff==0) { // a has same size as b  
                                 result=memcmp(a_current->ptr+a_offset, b_current->ptr+b_offset,   
                                         a_current->size-a_offset);  
                                 if(result)  
                                         return result;  
                                 pos+=a_current->size;  
                                 a_current++; a_offset=0;  
                                 b_current++; b_offset=0;  
                         } else if(size_diff>0) { // a longer  
                                 result=memcmp(a_current->ptr+a_offset, b_current->ptr+b_offset,   
                                         b_current->size-b_offset);  
                                 if(result)  
                                         return result;  
                                 a_offset+=b_current->size-b_offset;  
                                 b_current++; b_offset=0;  
                         } else { // b longer  
                                 result=memcmp(a_current->ptr+a_offset, b_current->ptr+b_offset,   
                                         a_current->size-a_offset);  
                                 if(result)  
                                         return result;  
                                 b_offset+=a_current->size-a_offset;  
                                 pos+=a_current->size;  
                                 a_current++; a_offset=0;  
                         }  
                 } else {  
                         a_offset-=a_current->size;  
                         pos+=a_current->size;  
                         a_current++;  
                 }  
         }  
         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          // first: letters themselves
 int String::cmp(int& partial, const char* b_ptr, size_t src_size,           result.body=CORD_substr(body, substr_start, substr_end-substr_start);
                                 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 String_fragment *a_current=felements;  
         size_t a_offset=this_offset;  
         size_t b_offset=0;  
         String_fragment *a_end=felements+fused;  
         size_t pos=0;  
   
         bool a_break;  
         bool b_break=b_size==0;  
         while(!(  
                 (a_break=(a_current==a_end))  
                 || b_break)) {  
                 if(pos+a_current->size > this_offset) {  
                         if(lang!=UL_UNSPECIFIED && a_current->lang>(String_UL)lang)   
                                 return -1; // wrong lang -- bail out  
   
                         int size_diff=  
                                 (a_current->size-a_offset)-  
                                 (b_size-b_offset);  
                           
                         if(size_diff==0) { // a has same size as b  
                                 if(int result=memcmp(a_current->ptr+a_offset, b_ptr+b_offset,   
                                         a_current->size-a_offset)!=0)  
                                         return result;  
                                 pos+=a_current->size;  
                                 a_current++; a_offset=0;  
                                 b_break=true;  
                         } else if (size_diff>0) { // a longer  
                                 if(int result=memcmp(a_current->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_current->ptr+a_offset, b_ptr+b_offset,   
                                         a_current->size-a_offset)!=0)  
                                         return result;  
                                 b_offset+=a_current->size-a_offset;  
                                 pos+=a_current->size;  
                                 a_current++; a_offset=0;  
                         }  
                 } else {  
                         a_offset-=a_current->size;   
                         pos+=a_current->size;  
                         a_current++;   
                 }  
         }  
         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          // next: their langs
 const String_fragment::Origin& String::origin() const {           result.fragments.append(fragments, substr_start, substr_end);
         if(is_empty()) {  
                 static const String_fragment::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  
         String_fragment::Origin& first_origin=felements[0].origin;  
         return first_origin.file ? first_origin : felements[fused-1].origin;  
 }  
 #endif  
   
 StringPtr String::mid(size_t start, size_t finish) const {  
         StringPtr result(new String());  
   
         start=min(start, size());  
         finish=max(start, finish);  
         if(start==finish)  
                 return result;  
   
         size_t pos=0;  
         STRING_FOREACH_FRAGMENT(  
                 size_t item_finish=pos+fragment->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:fragment->size;  
                         result->APPEND(  
                                 fragment->ptr+offset, size-offset,   
                                 fragment->lang,  
                                 fragment->origin.file, fragment->origin.line);  
                         if(finished)  
                                 goto break2;  
                 }  
                 pos+=fragment->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<ConstStringPtr>& 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;
                 }                  }
                 // last piece                  // last piece
                 if(pos_after<self_size && limit) {                  if(pos_after<self_size && limit) {
                         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+=ConstStringPtr(this);                  result+=this;
                 if(pos_after_ref)                  pos_after+=self_size;
                         *pos_after_ref+=self_size;  
         }          }
 }  }
   
 void String::split(Array<ConstStringPtr>& 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--) {
                         result+=mid(pos_after, pos_before);                          result+=&mid(pos_after, pos_before);
                         pos_after=pos_before+delim.size();                          pos_after=pos_before+delim.size();
                 }                  }
                 // last piece                  // last piece
                 if(pos_after<size() && limit) {                  if(pos_after<size() && limit) {
                         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+=ConstStringPtr(this);                  result+=this;
                 if(pos_after_ref)                  pos_after+=size();
                         *pos_after_ref+=size();  
         }          }
 }  }
   
 static void regex_options(ConstStringPtr 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 369  static void regex_options(ConstStringPtr Line 238  static void regex_options(ConstStringPtr
   
     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 380  static void regex_options(ConstStringPtr Line 249  static void regex_options(ConstStringPtr
                         }                          }
 }  }
   
 TablePtr String::match(Charset& source_charset,  Table* String::match(Charset& source_charset,
                                            ConstStringPtr aorigin,                
                                            const String& regexp,                                              const String& regexp, 
                                            ConstStringPtr options,                                             const String& options,
                                            Row_action row_action, void *info,                                             Row_action row_action, void *info,
                                            bool *was_global) const {                                              bool& just_matched) const { 
           if(!regexp)
         if(regexp.is_empty())  
                 throw Exception(0,                  throw Exception(0,
                         aorigin,                          0,
                         "regexp is empty");                          "regexp is empty");
   
         CharPtr 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.get(), option_bits[0],   
                 &errptr, &erroffset,                  &errptr, &erroffset,
                 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);
         }          }
   
         CharPtr subject=cstr();          const char* subject=cstr();
         int length=size();          size_t subject_size=strlen(subject);
         const int ovecsize=(1/*match*/+MAX_MATCH_GROUPS)*3;          const int ovecsize=(1/*match*/+MAX_MATCH_GROUPS)*3;
         int ovector[ovecsize];          int ovector[ovecsize];
   
         // create table          // create table
         TablePtr result(new Table(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 431  TablePtr String::match(Charset& source_c Line 297  TablePtr String::match(Charset& source_c
         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.get(), 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(*result.get(), 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);                          row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);
                         if(option_bits[1])                          if(global || subpatterns)
                                 return result; // global=true+result                                  return table; // global or with subpatterns=true+result
                         else                          else {
                                 return TablePtr(0);// not global=no result                                  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];
                 object_ptr<Array<ConstStringPtr> > row;                  ArrayString* row=new ArrayString;
                 if(need_pre_post_match) {                  if(need_pre_post_match) {
                         *row+=mid(0, prefinish); // .prematch column value                          *row+=&mid(0, prefinish); // .prematch column value
                         *row+=mid(prefinish, poststart); // .match                          *row+=&mid(prefinish, poststart); // .match
                         *row+=mid(poststart, postfinish); // .postmatch                          *row+=&mid(poststart, postfinish); // .postmatch
                 } else {                  } else {
                         *row+=ConstStringPtrZero; // .prematch column value                          *row+=0; // .prematch column value
                         *row+=ConstStringPtrZero; // .match                          *row+=0; // .match
                         *row+=ConstStringPtrZero; // .postmatch                          *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(*result, row.get(), 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(*result, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);                          row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
                         return result;                          return table;
                 }                  }
                 prestart=poststart;                  prestart=poststart;
   
Line 485  TablePtr String::match(Charset& source_c Line 352  TablePtr String::match(Charset& source_c
         }          }
 }  }
   
 StringPtr String::change_case(Pool& pool, Charset& source_charset, Change_case_kind kind) const {  String& String::change_case(Charset& source_charset, Change_case_kind kind) {
         StringPtr result(new String());          String& result=*new String();
   
         const unsigned char *tables=source_charset.pcre_tables;          const unsigned char *tables=source_charset.pcre_tables;
   
Line 503  StringPtr String::change_case(Pool& pool Line 370  StringPtr String::change_case(Pool& pool
                 break;                  break;
         default:          default:
                 throw Exception(0,                   throw Exception(0, 
                         ConstStringPtr(this),                           this, 
                         "unknown change case kind #%d",                           "unknown change case kind #%d", 
                                 static_cast<int>(kind)); // never                                  static_cast<int>(kind)); // never
                 a=b=0; // calm, compiler                  a=b=0; // calm, compiler
                 break; // never                  break; // never
         }                 }       
   
         STRING_FOREACH_FRAGMENT(          char* new_cstr=cstrm();
                 char *new_cstr=new(pool) char[fragment->size];          char *dest=new_cstr;
                 char *dest=new_cstr;          unsigned char index;
                 const char *end=fragment->ptr+fragment->size;          for(const char* current=new_cstr; index=(unsigned char)*current; current++) {
                 for(const char *current=fragment->ptr; current<end; current++) {                  unsigned char c=a[index];
                         unsigned char c=a[(unsigned char)*current];                  if(b)
                         if(b)                          c=b[c];
                                 c=b[c];  
   
                         *dest++=(char)c;                  *dest++=(char)c;
                 }          }
                           result.body=new_cstr;
                 result->APPEND(new_cstr, fragment->size,           result.fragments.append(fragments, 0, fragments.count());
                         fragment->lang,  
                         fragment->origin.file, fragment->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 {
 StringPtr String::replace(Pool& pool, Dictionary& dict) const {          String& result=*new String();
         StringPtr result(new String());          const char* old_cstr=cstr();
         CharPtr lcstr(cstr());          const char* prematch_start=old_cstr;
         const char *current=lcstr.get();  
           for(const char* current=old_cstr; *current; ) {
         STRING_FOREACH_FRAGMENT(                  if(Table::element_type row=dict.first_that_starts(current)) {
 IFNDEF_NO_STRING_ORIGIN(                          // prematch
                 const char *joined_origin_file=fragment->origin.file;                          if(size_t prematch_size=current-prematch_start) {
                 const size_t joined_origin_line=fragment->origin.line;                                  result.body=CORD_cat_char_star(result.body, prematch_start, prematch_size);
 );                                  result.fragments.append(fragments, prematch_start-old_cstr, prematch_size);
                 uchar joined_lang=fragment->lang;  
                 const char *joined_ptr=current;  
                 // calc size  
                 size_t joined_size=0;  
                 STRING_PREPARED_FOREACH_FRAGMENT(  
                         if(fragment->lang==joined_lang)  
                                 joined_size+=fragment->size;  
                         else  
                                 break; // before non-ours  
                 );  
                 current+=joined_size;  
   
                 // pointers are after joined piece  
                 // & one step back, see STRING_PREPARED_FOREACH_FRAGMENT  
                 --fragment;  
                   
                 char *new_cstr=new(pool) char[(size_t)ceil(joined_size*dict.max_ratio())];  
                 char *dest=new_cstr;  
                 while(joined_size) {  
                         // there is a fragment where first column starts 'joined_ptr'  
                         if(Table::element_type row=dict.first_that_starts(joined_ptr, joined_size)) {  
                                 // get a=>b values  
                                 ConstStringPtr a=row->get(0);  
                                 ConstStringPtr b=row->get(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, String::UL_AS_IS);  dest+=b->size();  
                         } else {  
                                 // write a char to b && reduce work size  
                                 *dest++=*joined_ptr++;  joined_size--;  
                         }                          }
                 }  
   
                 result->APPEND(new_cstr, dest-new_cstr, joined_lang,                          // match
                         joined_origin_file, joined_origin_line);  
         );  
   
         return result;                          const String* a=row->get(0);
 }                          // skip 'a' in 'current'; move prematch_start
                           current+=a->size(); prematch_start=current;
   
                           if(row->count()>1) { // are there any b?
                                   const String* b=row->get(1);
                                   if(size_t b_size=b->size()) {
                                           result.body=CORD_cat(result.body, b->cstr_to_cord());
                                           result.fragments.append(b->fragments, 0, b->fragments.count());
                                   }
                           }
                   } else // simply advance
                           current++; 
           }
   
 StringPtr String::join_chains(Pool& pool) const {          // postmatch
         StringPtr result(new String());          if(size_t prematch_size=current-prematch_start) {
                           result.body=CORD_cat_char_star(result.body, prematch_start, prematch_size);
         char *pooled_buf=new(pool) char[cstr_bufsize()];                  result.fragments.append(fragments, prematch_start-old_cstr, prematch_size);
         store_to(pooled_buf, String::UL_AS_IS);          }
         const char *current=pooled_buf;  
   
         STRING_FOREACH_FRAGMENT(  
 IFNDEF_NO_STRING_ORIGIN(  
                 const char *joined_origin_file=fragment->origin.file;  
                 const size_t joined_origin_line=fragment->origin.line;  
 );  
                 uchar joined_lang=fragment->lang;  
                 const char *joined_ptr=current;  
                 // calc size  
                 size_t joined_size=0;  
                 STRING_PREPARED_FOREACH_FRAGMENT(  
                         if(fragment->lang==joined_lang)  
                                 joined_size+=fragment->size;  
                         else  
                                 break; // before non-ours  
                 );  
                 current+=joined_size;  
   
                 // pointers are after joined piece  
                 // & one step back, see STRING_PREPARED_FOREACH_FRAGMENT  
                 --fragment;  
                   
                 result->APPEND(joined_ptr, joined_size, joined_lang,  
                         joined_origin_file, joined_origin_line);  
         );  
   
         return result;          return result;
 }  }
   
 double String::as_double() const {   double String::as_double() const { 
         double result;          double result;
         char buf[MAX_STRING];          const char *str=cstr();
         if(size()>MAX_STRING-1)  
                 throw Exception("number.format",          while(*str && isspace(*str))
                         ConstStringPtr(this),                  str++;
                         "invalid number too long a string (%u>%u)", size(), MAX_STRING-1);          if(!*str)
         char *eol=store_to(buf, String::UL_AS_IS); *eol=0;  
         const char *cstr=buf;  
   
         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))
                         throw Exception("number.format",                          throw Exception("number.format",
                                 ConstStringPtr(this),                                  this,
                                 "invalid number (double)");                                  "invalid number (double)");
   
         return result;          return result;
 }  }
 int String::as_int() const {   int String::as_int() const { 
         int result;          int result;
         char buf[MAX_STRING];          const char *str=cstr();
         if(size()>MAX_STRING-1)  
                 throw Exception("number.format",          while(*str && isspace(*str))
                         ConstStringPtr(this),                  str++;
                         "invalid number too long a string (%u>%u)", size(), MAX_STRING-1);          if(!*str)
         char *eol=store_to(buf, String::UL_AS_IS); *eol=0;  
         const char *cstr=buf;  
   
         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))
                         throw Exception("number.format",                          throw Exception("number.format",
                                 ConstStringPtr(this),                                  this,
                                 "invalid number (int)");                                  "invalid number (int)");
   
         return result;          return result;
Line 697  inline uint uchars2uint(uchar *bytes) { Line 500  inline uint uchars2uint(uchar *bytes) {
                 | bytes[0];                  | bytes[0];
 }  }
   
 void String::serialize(Pool& pool, size_t prolog_size, char *& buf, size_t& buf_size) const {  static int CORD_batched_iter_fn_append(const char* s, void* client_data) {
         buf_size=          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
                 +fused*(sizeof(uchar)+sizeof(size_t))                  +fragments.count()*(sizeof(Untaint_lang)+sizeof(size_t))
                 +size();                  +size();
         buf=new(pool) char[buf_size];          C result(new(PointerFreeGC) char[buf_size], buf_size);
         char *cur=buf+prolog_size;  
           // 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);
   
         STRING_FOREACH_FRAGMENT(          // 3: lang info
           for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); ) {
                   const Fragment& fragment=i.next();
                 // lang                  // lang
                 memcpy(cur, &fragment->lang, sizeof(fragment->lang));                  memcpy(cur, &fragment.lang, sizeof(fragment.lang));  cur+=sizeof(fragment.lang);
                 cur+=sizeof(fragment->lang);  
                 // size                  // size
                 // bug on some sparc platform [you can't work with integers on odd pointers]                  memcpy(cur, &fragment.size, sizeof(fragment.size));  cur+=sizeof(fragment.size);
                 // forces us to use byte array instead          }
                 uchar bytes[4];  
                 uint2uchars(fragment->size, bytes);          // 4: letters
                 memcpy(cur, &bytes, sizeof(bytes)); cur+=sizeof(bytes);          CORD_iter5(body, 0, 0, CORD_batched_iter_fn_append, &cur);
                 // bytes  
                 memcpy(cur, fragment->ptr, fragment->size);          return result;
                 cur+=fragment->size;  
         );  
 }  }
 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(size_t)>buf_size) // lang+size          const char* cur=(const char* )buf+prolog_size;
                         return false;  
   
                 String_UL lang=*(String_UL *)(cur);                       // 2: fragments.count
                 size_t size=uchars2uint((uchar *)cur);          if(buf_size<sizeof(size_t)) // fragments.count don't fit?
                   return false;
           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(size_t)+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)+sizeof(size_t));                   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.172.2.6  
changed lines
  Added in v.1.172.2.21.2.7


E-mail: