Annotation of parser3/src/main/pa_string.C, revision 1.172.2.21.2.14

1.45      paf         1: /** @file
1.172.2.21.2.  (paf        2:):      Parser: string class. @see untalength_t.C.
1.46      paf         3: 
1.172.2.11  paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.138     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.164     paf         6: */
1.46      paf         7: 
1.172.2.21.2.  4(paf       8:3): static const char* IDENT_STRING_C="$Date: 2003/03/20 16:57:43 $";
1.4       paf         9: 
1.70      paf        10: #include "pcre.h"
                     11: 
1.12      paf        12: #include "pa_string.h"
1.22      paf        13: #include "pa_exception.h"
1.61      paf        14: #include "pa_table.h"
1.101     parser     15: #include "pa_dictionary.h"
1.132     paf        16: #include "pa_charset.h"
1.60      paf        17: 
1.172.2.2  paf        18: // helpers
1.139     paf        19: 
1.172.2.2  paf        20: /// String::match uses this as replace & global search table columns
1.139     paf        21: 
1.172.2.4  paf        22: const int MAX_MATCH_GROUPS=100;
                     23: 
1.172.2.14  paf        24: class String_match_table_template_columns: public ArrayString {
1.172.2.2  paf        25: public:
1.172.2.4  paf        26:        String_match_table_template_columns() {
1.172.2.21.2.  (paf       27:):              *this+=new String("prematch");
                     28:):              *this+=new String("match");
                     29:):              *this+=new String("postmatch");
1.172.2.4  paf        30:                for(int i=0; i<MAX_MATCH_GROUPS; i++) {
1.172.2.21.2.  (paf       31:):                      char *cname=new(PointerFreeGC) char[3/*strlen("100")*/+1/*terminating 0*/];
                     32:):                      *this+=new String(cname, sprintf(cname, "%d", 1+i));
1.172.2.2  paf        33:                }
1.172.2.21  paf        34:        }
1.172.2.4  paf        35: };
                     36: 
1.172.2.21.2.  (paf       37:): Table string_match_table_template(new String_match_table_template_columns);
1.172.2.2  paf        38: 
1.172.2.21.2.  (paf       39:): // String::ArrayFragment methods
                     40:): 
          3(paf      41:3): void String::ArrayFragment::append_positions(const ArrayFragment& src, 
                     42:3):                                          size_t substr_begin, size_t substr_end) {
          1(paf      43:3):     if(substr_begin==substr_end)
          3(paf      44:3):             return;
          (paf       45:): 
          1(paf      46:3):     size_t fragment_begin=0;
          (paf       47:):      size_t fragment_end;
          1(paf      48:3):     for(Array_iterator<element_type> i(src); ; fragment_begin=fragment_end) {
          (paf       49:):              const element_type& fragment=i.next();
          1(paf      50:3):             fragment_end=fragment_begin+fragment.length;            
          (paf       51:): 
          1(paf      52:3):             if(fragment_begin<substr_begin) // not reached fragments which may include 'substr'?
          (paf       53:):                      continue;
                     54:): 
                     55:):              // found first fragment including piece of 'substr'
                     56:):              size_t piece_end=min(substr_end, fragment_end);
          1(paf      57:3):             *this+=Fragment(fragment.lang, piece_end-substr_begin);
          (paf       58:):              
                     59:):              while(substr_end>fragment_end) { // are there more fragments including pieces of 'substr'?
          1(paf      60:3):                     fragment_begin=fragment_end;
                     61:3): 
          (paf       62:):                      const element_type& fragment=i.next();
          1(paf      63:3):                     fragment_end=fragment_begin+fragment.length;
          (paf       64:): 
                     65:):                      if(substr_end>fragment_end) // are there still more?
          3(paf      66:3):                             append(Fragment(fragment.lang, fragment.length)); // appending whole fragment
          (paf       67:):                      else { // no, it was last
          3(paf      68:3):                             append(Fragment(fragment.lang, substr_end-fragment_begin));
                     69:3):                             return;
          (paf       70:):                      }
                     71:):              }
                     72:): 
                     73:):              break;
                     74:):      }       
                     75:): }
                     76:): /*
          1(paf      77:3): void String::ArrayFragment::mid(ArrayFragment& result, size_t substr_begin, size_t substr_end) {
          (paf       78:): }
                     79:): */
                     80:): 
                     81:): // String methods
1.172.2.2  paf        82: 
1.172.2.21.2.  (paf       83:): String::String(const char* cstr, size_t helper_length, bool tainted): body(CORD_EMPTY) {
          3(paf      84:3):     append(cstr, helper_length, tainted?UL_TAINTED:UL_CLEAN);
1.1       paf        85: }
1.140     paf        86: 
1.172.2.21.2.  (paf       87:): String::String(const String& src): body(src.body), fragments(src.fragments) {}
1.28      paf        88: 
1.172.2.21.2.  3(paf      89:3): String& String::append(const char* str, size_t helper_length, Untaint_lang lang) {
                     90:3):     if(!str)
1.9       paf        91:                return *this;
1.172.2.21.2.  3(paf      92:3):     size_t use_length=helper_length?helper_length:strlen(str);
          0(paf      93:3):     if(!use_length)
1.9       paf        94:                return *this;
1.122     paf        95: 
1.172.2.21.2.  3(paf      96:3):     body=CORD_cat_char_star(body, str, use_length);
          0(paf      97:3):     fragments+=Fragment(lang, use_length);
1.1       paf        98: 
                     99:        return *this;
                    100: }
                    101: 
1.172.2.21.2.  (paf      102:): static int CORD_batched_iter_fn_generic_hash_code(const char*  s, void * client_data) {
                    103:):      uint& result=*static_cast<uint*>(client_data);
                    104:):      generic_hash_code(result, s);
                    105:):      return 0;
                    106:): };
1.16      paf       107: uint String::hash_code() const {
1.7       paf       108:        uint result=0;
1.172.2.21.2.  (paf      109:):      CORD_iter5(body, 0, 0, CORD_batched_iter_fn_generic_hash_code, &result);
1.5       paf       110:        return result;
                    111: }
                    112: 
1.172.2.21.2.  1(paf     113:3): /// @todo check in doc: whether it documents NOW bad situation "abc".mid(-1, 3) =were?="ab"
                    114:3): const String& String::mid(size_t substr_begin, size_t substr_end) const {
          (paf      115:):      String& result=*new String;
1.33      paf       116: 
1.172.2.21.2.  1(paf     117:3):     size_t self_length=length();
                    118:3):     substr_begin=max(min(substr_begin, self_length), (size_t)0);
                    119:3):     substr_end=min(max(substr_end, substr_begin), self_length);
                    120:3):     if(substr_begin==substr_end)
          (paf      121:):              return result;
1.52      paf       122: 
1.172.2.21.2.  (paf      123:):      // first: letters themselves
          1(paf     124:3):     result.body=CORD_substr(body, substr_begin, substr_end-substr_begin);
1.46      paf       125: 
1.172.2.21.2.  (paf      126:):      // next: their langs
          3(paf     127:3):     result.fragments.append_positions(fragments, substr_begin, substr_end);
1.53      paf       128: 
1.60      paf       129: //     SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
1.172.2.21.2.  1(paf     130:3):             //cstr(), substr_begin, substr_end, result.cstr());
1.53      paf       131:        return result;
1.54      paf       132: }
                    133: 
1.172.2.21.2.  (paf      134:): size_t String::pos(CORD substr, 
                    135:):                 size_t this_offset, Untaint_lang lang) const {
                    136:):      // first: letters themselves
          1(paf     137:3):     size_t substr_begin=CORD_str(body, this_offset, substr);
                    138:3):     if(substr_begin==CORD_NOT_FOUND)
          (paf      139:):              return STRING_NOT_FOUND;
                    140:): 
                    141:):      // next: check the lang when specified
                    142:): 
                    143:):      if(lang==UL_UNSPECIFIED) // ignore lang?
          1(paf     144:3):             return substr_begin;
          (paf      145:): 
                    146:):      // substr must be in one fragment, and fragments' lang must = lang
          1(paf     147:3):     size_t substr_end=substr_begin+CORD_len(substr);
                    148:3):     size_t fragment_begin=0;
          (paf      149:):      size_t fragment_end;
          1(paf     150:3):     for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); fragment_begin=fragment_end) {
          (paf      151:):              const Fragment& fragment=i.next();
          1(paf     152:3):             fragment_end=fragment_begin+fragment.length;
          (paf      153:): 
          1(paf     154:3):             if(substr_begin<fragment_begin) // not reached fragments which may include 'result'?
          (paf      155:):                      continue;
          2(paf     156:3):             if(substr_begin>=fragment_end) // begin of substr OUT of current fragment?
                    157:3):                     continue;
          (paf      158:):              
                    159:):              if(substr_end>fragment_end) // end of substr OUT of current fragment?
          2(paf     160:3):                     throw Exception(0, // (*) see below
                    161:3):                             this,
                    162:3):                             "searching for '%s' starting from %ud problem: found begin in one fragment, but end in another",
                    163:3):                                     CORD_to_const_char_star(substr), this_offset);
          (paf      164:): 
          2(paf     165:3):             if(fragment.lang==lang)
                    166:3):                     return substr_begin;
                    167:3):             else { // bad lang...
                    168:3):                     /// WARNING: this possibly skips assert (*), but it's fast
                    169:3):                     substr_begin=CORD_str(body, fragment_end/*...search AFTER for more*/, substr);
                    170:3):                     if(substr_begin==CORD_NOT_FOUND)
                    171:3):                             return STRING_NOT_FOUND;
                    172:3): 
                    173:3):                     size_t substr_end=substr_begin+CORD_len(substr);
                    174:3):                     // and continuing with next fragment
                    175:3):             }
          (paf      176:):      }       
                    177:): 
                    178:):      return STRING_NOT_FOUND;
1.58      paf       179: }
                    180: 
1.172.2.21.2.  (paf      181:): size_t String::pos(const String& substr, 
                    182:):                              size_t this_offset, Untaint_lang lang) const {
                    183:):      return pos(substr.body, this_offset, lang);
1.60      paf       184: }
                    185: 
1.172.2.14  paf       186: void String::split(ArrayString& result, 
1.172.2.21.2.  (paf      187:):                                 size_t& pos_after, 
                    188:):                                 const char* delim, 
1.172.2.10  paf       189:                                   Untaint_lang lang, int limit) {
1.172.2.21.2.  (paf      190:):      size_t self_length=length();
                    191:):      if(size_t delim_length=strlen(delim)) {
1.60      paf       192:                int pos_before;
                    193:                // while we have 'delim'...
1.172.2.21.2.  (paf      194:):              for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
                    195:):                      result+=&mid(pos_after, pos_before);
                    196:):                      pos_after=pos_before+delim_length;
1.60      paf       197:                }
                    198:                // last piece
1.172.2.21.2.  (paf      199:):              if(pos_after<self_length && limit) {
                    200:):                      result+=&mid(pos_after, self_length);
                    201:):                      pos_after=self_length;
1.60      paf       202:                }
                    203:        } else { // empty delim
1.172.2.21.2.  (paf      204:):              result+=this;
                    205:):              pos_after+=self_length;
1.60      paf       206:        }
                    207: }
                    208: 
1.172.2.14  paf       209: void String::split(ArrayString& result, 
1.172.2.21.2.  (paf      210:):                                 size_t& pos_after, 
1.60      paf       211:                                   const String& delim, Untaint_lang lang, 
1.172.2.21.2.  (paf      212:):                                 int limit) const {
                    213:):      if(delim) {
1.60      paf       214:                int pos_before;
                    215:                // while we have 'delim'...
                    216:                for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.172.2.21.2.  (paf      217:):                      result+=&mid(pos_after, pos_before);
                    218:):                      pos_after=pos_before+delim.length();
1.60      paf       219:                }
                    220:                // last piece
1.172.2.21.2.  (paf      221:):              if(pos_after<length() && limit) {
                    222:):                      result+=&mid(pos_after, length());
                    223:):                      pos_after=length();
1.60      paf       224:                }
                    225:        } else { // empty delim
1.172.2.21.2.  (paf      226:):              result+=this;
                    227:):              pos_after+=length();
1.60      paf       228:        }
1.61      paf       229: }
                    230: 
1.172.2.21.2.  (paf      231:): static void regex_options(const String& options, int *result, bool& need_pre_post_match){
1.63      paf       232:     struct Regex_option {
1.172.2.11  paf       233:                const char* keyL;
                    234:                const char* keyU;
1.63      paf       235:                int clear, set;
                    236:                int *result;
1.154     paf       237:                bool *flag;
1.63      paf       238:     } regex_option[]={
1.153     paf       239:                {"i", "I", 0, PCRE_CASELESS, result}, // a=A
                    240:                {"s", "S", 0, PCRE_DOTALL, result}, // \n\n$ [default]
                    241:                {"x", "U", 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    242:                {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    243:                {"g", "G", 0, true, result+1}, // many rows
1.154     paf       244:                {"'", 0, 0, 0, 0, &need_pre_post_match},
                    245:                {0}
1.63      paf       246:     };
1.171     paf       247:        result[0]=PCRE_EXTRA | PCRE_DOTALL | PCRE_DOLLAR_ENDONLY;
1.63      paf       248:        result[1]=0;
                    249: 
                    250:     if(options) 
1.153     paf       251:                for(Regex_option *o=regex_option; o->keyL; o++) 
1.172.2.21.2.  (paf      252:):                      if(options.pos(o->keyL)>=0
                    253:):                              || (o->keyU && options.pos(o->keyU)>=0)) {
1.154     paf       254:                                if(o->flag)
                    255:                                        *o->flag=true;
                    256:                                else { // result
                    257:                                        *o->result &= ~o->clear;
                    258:                                        *o->result |= o->set;
                    259:                                }
1.63      paf       260:                        }
                    261: }
                    262: 
1.172.2.21.2.  (paf      263:): Table* String::match(Charset& source_charset,
1.172.2.4  paf       264:                                           const String& regexp, 
1.172.2.21.2.  (paf      265:):                                         const String& options,
1.172.2.4  paf       266:                                           Row_action row_action, void *info,
1.172.2.20  paf       267:                                           bool& just_matched) const { 
1.172.2.21.2.  (paf      268:):      if(!regexp)
                    269:):              throw Exception(0,
                    270:):                      0,
1.73      paf       271:                        "regexp is empty");
1.154     paf       272: 
1.172.2.21.2.  (paf      273:):      const char* pattern=regexp.cstr();
1.172.2.11  paf       274:        const char* errptr;
1.62      paf       275:        int erroffset;
1.172.2.21.2.  (paf      276:):      bool need_pre_post_match=false;
1.154     paf       277:        int option_bits[2];  regex_options(options, option_bits, need_pre_post_match);
1.172.2.20  paf       278:        bool global=option_bits[1]!=0;
1.172.2.21.2.  (paf      279:):      pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       280:                &errptr, &erroffset,
1.172.2.1  paf       281:                source_charset.pcre_tables);
1.62      paf       282: 
1.67      paf       283:        if(!code)
1.172.2.21.2.  (paf      284:):              throw Exception(0,
                    285:):                      &regexp.mid(erroffset, regexp.length()),
1.74      paf       286:                        "regular expression syntax error - %s", errptr);
1.62      paf       287:        
1.172.2.20  paf       288:        int subpatterns=pcre_info(code, 0, 0);
                    289:        if(subpatterns<0) {
1.100     parser    290:                pcre_free(code);
1.149     paf       291:                throw Exception(0,
1.172.2.21.2.  (paf      292:):                      &regexp,
1.76      paf       293:                        "pcre_info error (%d)", 
1.172.2.20  paf       294:                                subpatterns);
1.63      paf       295:        }
                    296: 
1.172.2.21.2.  (paf      297:):      const char* subject=cstr();
                    298:):      size_t subject_length=strlen(subject);
                    299:):      const int oveclength=(1/*match*/+MAX_MATCH_GROUPS)*3;
                    300:):      int ovector[oveclength];
1.155     paf       301: 
                    302:        // create table
1.172.2.21.2.  (paf      303:):      Table* table=new Table(string_match_table_template);
1.63      paf       304: 
1.64      paf       305:        int exec_option_bits=0;
1.154     paf       306:        int prestart=0;
                    307:        int poststart=0;
1.172.2.21.2.  (paf      308:):      int postfinish=length();
1.63      paf       309:        while(true) {
                    310:                int exec_substrings=pcre_exec(code, 0,
1.172.2.21.2.  (paf      311:):                      subject, subject_length, prestart,
                    312:):                      exec_option_bits, ovector, oveclength);
1.63      paf       313:                
                    314:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    315:                        pcre_free(code);
1.172.2.21.2.  (paf      316:):                      row_action(table, 0/*last time, no raw*/, 0, 0, poststart, postfinish, info);
1.172.2.20  paf       317:                        if(global || subpatterns)
                    318:                                return table; // global or with subpatterns=true+result
                    319:                        else {
1.172.2.21.2.  (paf      320:):                              just_matched=false; return 0; // not global=no result
1.172.2.20  paf       321:                        }
1.63      paf       322:                }
                    323: 
                    324:                if(exec_substrings<0) {
1.100     parser    325:                        pcre_free(code);
1.172.2.21.2.  (paf      326:):                      throw Exception(0,
                    327:):                              &regexp,
1.76      paf       328:                                "regular expression execute error (%d)", 
1.63      paf       329:                                        exec_substrings);
                    330:                }
                    331: 
1.154     paf       332:                int prefinish=ovector[0];
                    333:                poststart=ovector[1];
1.172.2.21.2.  (paf      334:):              ArrayString* row=new ArrayString;
1.172.2.4  paf       335:                if(need_pre_post_match) {
1.172.2.21.2.  (paf      336:):                      *row+=&mid(0, prefinish); // .prematch column value
                    337:):                      *row+=&mid(prefinish, poststart); // .match
                    338:):                      *row+=&mid(poststart, postfinish); // .postmatch
1.172.2.4  paf       339:                } else {
1.172.2.21.2.  (paf      340:):                      *row+=0; // .prematch column value
                    341:):                      *row+=0; // .match
                    342:):                      *row+=0; // .postmatch
1.172.2.4  paf       343:                }
1.63      paf       344:                
                    345:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       346:                        // -1:-1 case handled peacefully by mid() itself
1.172.2.21.2.  (paf      347:):                      *row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       348:                }
                    349:                
1.172.2.20  paf       350:                row_action(table, row, prestart, prefinish, poststart, postfinish, info);
1.63      paf       351: 
1.172.2.20  paf       352:                if(!global || prestart==poststart) { // not global | going to hang
1.100     parser    353:                        pcre_free(code);
1.172.2.21.2.  (paf      354:):                      row_action(table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
1.172.2.20  paf       355:                        return table;
1.63      paf       356:                }
1.154     paf       357:                prestart=poststart;
1.63      paf       358: 
                    359: /*
                    360:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       361:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       362: */
                    363:        }
1.82      parser    364: }
                    365: 
1.172.2.21.2.  (paf      366:): String& String::change_case(Charset& source_charset, Change_case_kind kind) {
                    367:):      String& result=*new String();
1.172.2.4  paf       368: 
1.172.2.1  paf       369:        const unsigned char *tables=source_charset.pcre_tables;
1.82      parser    370: 
                    371:        const unsigned char *a;
                    372:        const unsigned char *b;
                    373:        switch(kind) {
                    374:        case CC_UPPER:
                    375:                a=tables+lcc_offset;
                    376:                b=tables+fcc_offset;
                    377:                break;
                    378:        case CC_LOWER:
                    379:                a=tables+lcc_offset;
                    380:                b=0;
                    381:                break;
                    382:        default:
1.172.2.21.2.  (paf      383:):              throw Exception(0, 
                    384:):                      this, 
1.82      parser    385:                        "unknown change case kind #%d", 
                    386:                                static_cast<int>(kind)); // never
                    387:                a=b=0; // calm, compiler
                    388:                break; // never
                    389:        }       
                    390: 
1.172.2.21.2.  (paf      391:):      char* new_cstr=cstrm();
                    392:):      char *dest=new_cstr;
                    393:):      unsigned char index;
                    394:):      for(const char* current=new_cstr; index=(unsigned char)*current; current++) {
                    395:):              unsigned char c=a[index];
                    396:):              if(b)
                    397:):                      c=b[c];
1.82      parser    398: 
1.172.2.21.2.  (paf      399:):              *dest++=(char)c;
                    400:):      }
                    401:):      result.body=new_cstr;
          3(paf     402:3):     result.fragments.append(fragments);
1.89      parser    403: 
1.101     parser    404:        return result;
                    405: }
                    406: 
1.172.2.21.2.  (paf      407:): const String& String::replace(const Dictionary& dict) const {
                    408:):      String& result=*new String();
                    409:):      const char* old_cstr=cstr();
          1(paf     410:3):     const char* prematch_begin=old_cstr;
          (paf      411:): 
                    412:):      for(const char* current=old_cstr; *current; ) {
          1(paf     413:3):             if(Table::element_type row=dict.first_that_begins(current)) {
          (paf      414:):                      // prematch
          1(paf     415:3):                     if(size_t prematch_length=current-prematch_begin) {
                    416:3):                             result.body=CORD_cat_char_star(result.body, prematch_begin, prematch_length);
          4(paf     417:3):                             result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr);
1.101     parser    418:                        }
                    419: 
1.172.2.21.2.  (paf      420:):                      // match
                    421:): 
                    422:):                      const String* a=row->get(0);
          1(paf     423:3):                     // skip 'a' in 'current'; move prematch_begin
                    424:3):                     current+=a->length(); prematch_begin=current;
1.170     paf       425: 
1.172.2.21.2.  (paf      426:):                      if(row->count()>1) { // are there any b?
                    427:):                              const String* b=row->get(1);
                    428:):                              if(size_t b_length=b->length()) {
                    429:):                                      result.body=CORD_cat(result.body, b->cstr_to_cord());
          3(paf     430:3):                                     result.fragments.append(b->fragments);
          (paf      431:):                              }
                    432:):                      }
                    433:):              } else // simply advance
                    434:):                      current++; 
                    435:):      }
1.156     paf       436: 
1.172.2.21.2.  (paf      437:):      // postmatch
          3(paf     438:3):     if(size_t postmatch_length=current-prematch_begin) {
                    439:3):             result.body=CORD_cat_char_star(result.body, prematch_begin, postmatch_length);
          4(paf     440:3):             result.fragments.append_positions(fragments, prematch_begin-old_cstr, current-old_cstr);
          (paf      441:):      }
1.156     paf       442: 
1.89      parser    443:        return result;
                    444: }
                    445: 
1.172.2.21.2.  (paf      446:): double String::as_double() const { 
1.89      parser    447:        double result;
1.172.2.21.2.  (paf      448:):      const char *str=cstr();
                    449:): 
                    450:):      while(*str && isspace(*str))
                    451:):              str++;
                    452:):      if(!*str)
1.162     paf       453:                return 0;
1.161     paf       454: 
1.102     parser    455:        char *error_pos;
1.89      parser    456:        // 0xABC
1.172.2.21.2.  (paf      457:):      if(str[0]=='0')
                    458:):              if(str[1]=='x' || str[1]=='X')
                    459:):                      result=(double)(unsigned long)strtol(str, &error_pos, 0);
1.99      parser    460:                else
1.172.2.21.2.  (paf      461:):                      result=(double)strtod(str+1/*skip leading 0*/, &error_pos);
1.89      parser    462:        else
1.172.2.21.2.  (paf      463:):              result=(double)strtod(str, &error_pos);
1.89      parser    464: 
1.159     paf       465:        while(char c=*error_pos++)
                    466:                if(!isspace(c))
                    467:                        throw Exception("number.format",
1.172.2.21.2.  (paf      468:):                              this,
1.159     paf       469:                                "invalid number (double)");
1.89      parser    470: 
                    471:        return result;
                    472: }
1.172.2.21.2.  (paf      473:): int String::as_int() const { 
1.89      parser    474:        int result;
1.172.2.21.2.  (paf      475:):      const char *str=cstr();
                    476:): 
                    477:):      while(*str && isspace(*str))
                    478:):              str++;
                    479:):      if(!*str)
1.162     paf       480:                return 0;
1.161     paf       481: 
1.102     parser    482:        char *error_pos;
1.89      parser    483:        // 0xABC
1.172.2.21.2.  (paf      484:):      if(str[0]=='0')
                    485:):              if(str[1]=='x' || str[1]=='X')
                    486:):                      result=(int)(unsigned long)strtol(str, &error_pos, 0);
1.99      parser    487:                else
1.172.2.21.2.  (paf      488:):                      result=(int)strtol(str+1/*skip leading 0*/, &error_pos, 0);
1.89      parser    489:        else
1.172.2.21.2.  (paf      490:):              result=(int)strtol(str, &error_pos, 0);
1.89      parser    491: 
1.159     paf       492:        while(char c=*error_pos++)
                    493:                if(!isspace(c))
                    494:                        throw Exception("number.format",
1.172.2.21.2.  (paf      495:):                              this,
1.159     paf       496:                                "invalid number (int)");
1.82      parser    497: 
                    498:        return result;
1.61      paf       499: }
1.113     parser    500: 
1.172.2.4  paf       501: inline void uint2uchars(uint word, uchar *bytes) {
                    502:        bytes[0]=word&0xFF;
                    503:        bytes[1]=(word>>8)&0xFF;
                    504:        bytes[2]=(word>>16)&0xFF;
                    505:        bytes[3]=(word>>24)&0xFF;
                    506: }
                    507: inline uint uchars2uint(uchar *bytes) {
                    508:        return bytes[3]<<24
                    509:                | bytes[2]<<16
                    510:                | bytes[1]<<8
                    511:                | bytes[0];
                    512: }
                    513: 
1.172.2.21.2.  (paf      514:): static int CORD_batched_iter_fn_append(const char* s, void* client_data) {
                    515:):      char*& cur=*static_cast<char**>(client_data);
                    516:): 
                    517:):      size_t length=strlen(s);
                    518:):      memcpy(cur, s, length);  cur+=length;
                    519:):      return 0;
                    520:): };
                    521:): String::C String::serialize(size_t prolog_length) const {
                    522:):      size_t buf_length=
                    523:):              prolog_length
                    524:):              +fragments.count()*(sizeof(Untaint_lang)+sizeof(size_t))
                    525:):              +length();
                    526:):      C result(new(PointerFreeGC) char[buf_length], buf_length);
                    527:): 
                    528:):      // 1: prolog
                    529:):      char *cur=result.str+prolog_length;
                    530:): 
                    531:): 
                    532:):      // 2: fragments.count
                    533:):      size_t fragments_count=fragments.count();
                    534:):      memcpy(cur, &fragments_count, sizeof(fragments_count));  cur+=sizeof(fragments_count);
1.113     parser    535: 
1.172.2.21.2.  (paf      536:):      // 3: lang info
                    537:):      for(Array_iterator<ArrayFragment::element_type> i(fragments); i.has_next(); ) {
                    538:):              const Fragment& fragment=i.next();
1.123     paf       539:                // lang
1.172.2.21.2.  (paf      540:):              memcpy(cur, &fragment.lang, sizeof(fragment.lang));  cur+=sizeof(fragment.lang);
                    541:):              // length
                    542:):              memcpy(cur, &fragment.length, sizeof(fragment.length));  cur+=sizeof(fragment.length);
                    543:):      }
                    544:): 
                    545:):      // 4: letters
                    546:):      CORD_iter5(body, 0, 0, CORD_batched_iter_fn_append, &cur);
                    547:): 
                    548:):      return result;
1.113     parser    549: }
1.172.2.21.2.  (paf      550:): bool String::deserialize(size_t prolog_length, void *buf, size_t buf_length, const char* file) {
                    551:):      if(buf_length<=prolog_length)
1.148     paf       552:                return false;
1.172.2.21.2.  (paf      553:):      buf_length-=prolog_length;
1.113     parser    554: 
1.172.2.21.2.  (paf      555:):      // 1: prolog
                    556:):      const char* cur=(const char* )buf+prolog_length;
1.148     paf       557: 
1.172.2.21.2.  (paf      558:):      // 2: fragments.count
                    559:):      if(buf_length<sizeof(size_t)) // fragments.count don't fit?
                    560:):              return false;
                    561:):      size_t fragments_count=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
                    562:):      buf_length-=sizeof(size_t);
1.128     paf       563: 
1.172.2.21.2.  (paf      564:):      // 3: lang info
                    565:):      size_t total_length=0;
                    566:):      for(size_t f=0; f<fragments_count; f++) {
                    567:):              size_t piece_length=sizeof(Untaint_lang)+sizeof(size_t);
                    568:):              if(buf_length<piece_length) // lang+length
1.148     paf       569:                        return false;
                    570: 
1.172.2.21.2.  (paf      571:):              Untaint_lang lang=*reinterpret_cast<const Untaint_lang *>(cur);  cur+=sizeof(Untaint_lang);
                    572:):              size_t fragment_length=*reinterpret_cast<const size_t*>(cur);  cur+=sizeof(size_t);
                    573:):              fragments+=Fragment(lang, fragment_length);
                    574:):              total_length+=fragment_length;
1.113     parser    575: 
1.172.2.21.2.  (paf      576:):              buf_length-=piece_length;
1.113     parser    577:        }
1.172.2.21.2.  (paf      578:): 
                    579:):      // 4: letters
                    580:):      if(buf_length!=total_length)
                    581:):              return false;
                    582:): 
                    583:):      body=CORD_cat_char_star(CORD_EMPTY, cur, buf_length);
                    584:): 
1.148     paf       585:        return true;
1.113     parser    586: }

E-mail: