Diff for /parser3/src/main/pa_string.C between versions 1.172.2.17 and 1.172.2.21.2.9

version 1.172.2.17, 2003/02/20 14:53:35 version 1.172.2.21.2.9, 2003/03/20 14:19:10
Line 1 Line 1
 /** @file  /** @file
         Parser: string class. @see untasize_t.C.          Parser: string class. @see untalength_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 22  static const char* IDENT_STRING_C="$Date Line 22  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 ArrayString {  class String_match_table_template_columns: public ArrayString {
         Pool pool;  
 public:  public:
         String_match_table_template_columns() {          String_match_table_template_columns() {
                 *this+=StringPtr(new String("prematch"));                  *this+=new String("prematch");
                 *this+=StringPtr(new String("match"));                  *this+=new String("match");
                 *this+=StringPtr(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*/];
                         StringPtr 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);
                                                                   Exception::undefined_source,   
                                                                   Table::columns_type(new String_match_table_template_columns));  // String::ArrayFragment methods
   
 // methods  String::ArrayFragment& String::ArrayFragment::append(const ArrayFragment& src, 
                                                        size_t substr_start, size_t substr_end) {
 String::String(const char* src, size_t src_size, bool tainted): Array<String_fragment>(1), fsize(0) {          if(substr_start==substr_end)
         if(src)                  return *this;
                 if(tainted)  
                         APPEND_TAINTED(src, src_size, 0, 0);          size_t fragment_start=0;
                 else          size_t fragment_end;
                         APPEND_CLEAN(src, src_size, 0, 0);          for(Array_iterator<element_type> i(src); ; fragment_start=fragment_end) {
                   const element_type& fragment=i.next();
                   fragment_end=fragment_start+fragment.length;            
   
                   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.length;
   
                           if(substr_end>fragment_end) // are there still more?
                                   simple_append(Fragment(fragment.lang, fragment.length)); // 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::String(const String& src): Array<String_fragment>(src.count()) {  // String methods
         append(src, UL_PASS_APPENDED);  
   String::String(const char* cstr, size_t helper_length, bool tainted): body(CORD_EMPTY) {
           append_cstr(cstr, helper_length, tainted?UL_TAINTED:UL_CLEAN);
 }  }
   
 String& String::real_append(STRING_APPEND_PARAMS) {  String::String(const String& src): body(src.body), fragments(src.fragments) {}
         if(!src)  
   String& String::append_cstr(const char* heap_variable, size_t helper_length, Untaint_lang lang) {
           if(!heap_variable)
                 return *this;                  return *this;
         if(!size)          size_t length=helper_length?helper_length:strlen(heap_variable);
                 size=strlen(src);          if(!length)
         if(!size)  
                 return *this;                  return *this;
   
         if(is_full())          body=CORD_cat_char_star(body, heap_variable, length);
                 expand(fdelta);          fragments+=Fragment(lang, length);
   
           return *this;
   }
   
         String_fragment *fragment=&felements[fused++];  String& String::append_copy(const char* auto_variable, size_t helper_length, Untaint_lang lang) {
         fragment->ptr=src;          if(!auto_variable)
         fragment->size=size;                  return *this;
         fragment->lang=lang;          size_t length=helper_length?helper_length:strlen(auto_variable);
 #ifndef NO_STRING_ORIGIN          if(!length)
         fragment->origin.file=file;                  return *this;
         fragment->origin.line=line;  
 #endif  
   
         fsize+=size;          body=pa_strdup(auto_variable, length);
           fragments+=Fragment(lang, length);
   
         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;  
         bool b_break;  
         while(true) {  
                 a_break=(a_current==a_end);  
                 b_break=(b_current==b_end);  
                 if(a_break || b_break)  
                         break;  
   
                 if(pos+a_current->size > this_offset) {          substr_start=min(substr_start, length());
                         if(lang!=UL_UNSPECIFIED && a_current->lang>(String_UL)lang)           substr_end=max(substr_start, substr_end);
                                 return -1; // wrong lang -- bail out          if(substr_start==substr_end)
                   return result;
                         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;          // next: their langs
         bool b_break=b_size==0;          result.fragments.append(fragments, substr_start, substr_end);
         while(true) {  
                 a_break=(a_current==a_end);  
                 if(a_break || b_break)  
                         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  
 const String_fragment::Origin& String::origin() const {   
         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.length;
   
                   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(ArrayString& 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) {                                     Untaint_lang lang, int limit) {
         size_t self_size=size();          size_t self_length=length();
         if(delim_size) {          if(size_t delim_length=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_length;
                 }                  }
                 // last piece                  // last piece
                 if(pos_after<self_size && limit) {                  if(pos_after<self_length && limit) {
                         result+=mid(pos_after, self_size);                          result+=&mid(pos_after, self_length);
                         pos_after=self_size;                          pos_after=self_length;
                 }                  }
                 if(pos_after_ref)  
                         *pos_after_ref=pos_after;  
         } else { // empty delim          } else { // empty delim
                 result+=StringPtr(this);                  result+=this;
                 if(pos_after_ref)                  pos_after+=self_length;
                         *pos_after_ref+=self_size;  
         }          }
 }  }
   
 void String::split(ArrayString& 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) {                                     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.length();
                 }                  }
                 // last piece                  // last piece
                 if(pos_after<size() && limit) {                  if(pos_after<length() && limit) {
                         result+=mid(pos_after, size());                          result+=&mid(pos_after, length());
                         pos_after=size();                          pos_after=length();
                 }                  }
                 if(pos_after_ref)  
                         *pos_after_ref=pos_after;  
         } else { // empty delim          } else { // empty delim
                 result+=StringPtr(this);                  result+=this;
                 if(pos_after_ref)                  pos_after+=length();
                         *pos_after_ref+=size();  
         }          }
 }  }
   
 static void regex_options(StringPtr 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;
Line 373  static void regex_options(StringPtr opti Line 246  static void regex_options(StringPtr opti
   
     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 384  static void regex_options(StringPtr opti Line 257  static void regex_options(StringPtr opti
                         }                          }
 }  }
   
 TablePtr String::match(Charset& source_charset,  Table* String::match(Charset& source_charset,
                                            StringPtr aorigin,             
                                            const String& regexp,                                              const String& regexp, 
                                            StringPtr 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.length()),
                         "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_length=strlen(subject);
         const int ovecsize=(1/*match*/+MAX_MATCH_GROUPS)*3;          const int oveclength=(1/*match*/+MAX_MATCH_GROUPS)*3;
         int ovector[ovecsize];          int ovector[oveclength];
   
         // 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;
         int poststart=0;          int poststart=0;
         int postfinish=size();          int postfinish=length();
         while(true) {          while(true) {
                 int exec_substrings=pcre_exec(code, 0,                  int exec_substrings=pcre_exec(code, 0,
                         subject.get(), length, prestart,                          subject, subject_length, prestart,
                         exec_option_bits, ovector, ovecsize);                          exec_option_bits, ovector, oveclength);
                                   
                 if(exec_substrings==PCRE_ERROR_NOMATCH) {                  if(exec_substrings==PCRE_ERROR_NOMATCH) {
                         pcre_free(code);                          pcre_free(code);
                         row_action(result, ArrayStringPtr(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<ArrayString> row(new ArrayString);                  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+=StringPtr(0); // .prematch column value                          *row+=0; // .prematch column value
                         *row+=StringPtr(0); // .match                          *row+=0; // .match
                         *row+=StringPtr(0); // .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, 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, ArrayStringPtr(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 489  TablePtr String::match(Charset& source_c Line 360  TablePtr String::match(Charset& source_c
         }          }
 }  }
   
 StringPtr String::change_case(Pool& pool, Charset& source_charset, Change_case_kind kind) {  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 507  StringPtr String::change_case(Pool& pool Line 378  StringPtr String::change_case(Pool& pool
                 break;                  break;
         default:          default:
                 throw Exception(0,                   throw Exception(0, 
                         StringPtr(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_length=current-prematch_start) {
                 const size_t joined_origin_line=fragment->origin.line;                                  result.body=CORD_cat_char_star(result.body, prematch_start, prematch_length);
 );                                  result.fragments.append(fragments, prematch_start-old_cstr, prematch_length);
                 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  
                                 StringPtr a=row->get(0);  
                                 StringPtr 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->length(); prematch_start=current;
   
                           if(row->count()>1) { // are there any b?
                                   const String* b=row->get(1);
                                   if(size_t b_length=b->length()) {
                                           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_length=current-prematch_start) {
                           result.body=CORD_cat_char_star(result.body, prematch_start, prematch_length);
         char *pooled_buf=new(pool) char[cstr_bufsize()];                  result.fragments.append(fragments, prematch_start-old_cstr, prematch_length);
         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() {   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))
                         StringPtr(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",
                                 StringPtr(this),                                  this,
                                 "invalid number (double)");                                  "invalid number (double)");
   
         return result;          return result;
 }  }
 int String::as_int() {   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))
                         StringPtr(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",
                                 StringPtr(this),                                  this,
                                 "invalid number (int)");                                  "invalid number (int)");
   
         return result;          return result;
Line 701  inline uint uchars2uint(uchar *bytes) { Line 508  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);
                 prolog_size  
                 +fused*(sizeof(uchar)+sizeof(size_t))  
                 +size();  
         buf=new(pool) char[buf_size];  
         char *cur=buf+prolog_size;  
   
         STRING_FOREACH_FRAGMENT(          size_t length=strlen(s);
           memcpy(cur, s, length);  cur+=length;
           return 0;
   };
   String::C String::serialize(size_t prolog_length) const {
           size_t buf_length=
                   prolog_length
                   +fragments.count()*(sizeof(Untaint_lang)+sizeof(size_t))
                   +length();
           C result(new(PointerFreeGC) char[buf_length], buf_length);
   
           // 1: prolog
           char *cur=result.str+prolog_length;
   
   
           // 2: fragments.count
           size_t fragments_count=fragments.count();
           memcpy(cur, &fragments_count, sizeof(fragments_count));  cur+=sizeof(fragments_count);
   
           // 3: lang info
           for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); ) {
                   const Fragment& fragment=i.next();
                 // lang                  // lang
                 memcpy(cur, &fragment->lang, sizeof(fragment->lang));                  memcpy(cur, &fragment.lang, sizeof(fragment.lang));  cur+=sizeof(fragment.lang);
                 cur+=sizeof(fragment->lang);                  // length
                 // size                  memcpy(cur, &fragment.length, sizeof(fragment.length));  cur+=sizeof(fragment.length);
                 // bug on some sparc platform [you can't work with integers on odd pointers]          }
                 // forces us to use byte array instead  
                 uchar bytes[4];          // 4: letters
                 uint2uchars(fragment->size, bytes);          CORD_iter5(body, 0, 0, CORD_batched_iter_fn_append, &cur);
                 memcpy(cur, &bytes, sizeof(bytes)); cur+=sizeof(bytes);  
                 // bytes          return result;
                 memcpy(cur, fragment->ptr, fragment->size);  
                 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_length, void *buf, size_t buf_length, const char* file) {
         if(buf_size<=prolog_size)          if(buf_length<=prolog_length)
                 return false;                  return false;
           buf_length-=prolog_length;
   
         char *cur=(char *)buf+prolog_size;          // 1: prolog
         buf_size-=prolog_size;          const char* cur=(const char* )buf+prolog_length;
   
         while(buf_size) {  
                 if(sizeof(uchar)+sizeof(size_t)>buf_size) // lang+size  
                         return false;  
   
                 String_UL lang=*(String_UL *)(cur);                       // 2: fragments.count
                 size_t size=uchars2uint((uchar *)cur);          if(buf_length<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_length-=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_length=0;
           for(size_t f=0; f<fragments_count; f++) {
                   size_t piece_length=sizeof(Untaint_lang)+sizeof(size_t);
                   if(buf_length<piece_length) // lang+length
                         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_length=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
                   fragments+=Fragment(lang, fragment_length);
                   total_length+=fragment_length;
   
                 cur+=piece_size;                  buf_length-=piece_length;
                 buf_size-=piece_size;  
         }          }
   
           // 4: letters
           if(buf_length!=total_length)
                   return false;
   
           body=CORD_cat_char_star(CORD_EMPTY, cur, buf_length);
   
         return true;          return true;
 }  }

Removed from v.1.172.2.17  
changed lines
  Added in v.1.172.2.21.2.9


E-mail: