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

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

E-mail: