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

1.45      paf         1: /** @file
1.55      paf         2:        Parser: string class. @see untasize_t.C.
1.46      paf         3: 
1.36      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.119     paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.46      paf         6: 
1.120   ! paf         7:        $Id: pa_string.C,v 1.119 2001/11/05 11:46:28 paf Exp $
1.4       paf         8: */
                      9: 
1.48      paf        10: #include "pa_config_includes.h"
1.1       paf        11: 
1.70      paf        12: #include "pcre.h"
1.82      parser     13: #include "internal.h"
1.70      paf        14: 
1.13      paf        15: #include "pa_pool.h"
1.12      paf        16: #include "pa_string.h"
1.5       paf        17: #include "pa_hash.h"
1.22      paf        18: #include "pa_exception.h"
1.53      paf        19: #include "pa_common.h"
1.60      paf        20: #include "pa_array.h"
                     21: #include "pa_globals.h"
1.61      paf        22: #include "pa_table.h"
1.101     parser     23: #include "pa_dictionary.h"
1.60      paf        24: 
1.75      paf        25: String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
1.120   ! paf        26:        Pooled(apool) {
1.28      paf        27:        last_chunk=&head;
                     28:        head.count=CR_PREALLOCATED_COUNT;
1.5       paf        29:        append_here=head.rows;
1.2       paf        30:        head.preallocated_link=0;
1.28      paf        31:        link_row=&head.rows[head.count];
1.41      paf        32: 
                     33:        if(src)
1.75      paf        34:                if(tainted)
                     35:                        APPEND_TAINTED(src, src_size, 0, 0);
1.41      paf        36:                else
1.75      paf        37:                        APPEND_CLEAN(src, src_size, 0, 0);
1.1       paf        38: }
                     39: 
1.94      parser     40: String::String(const String& src) :    
1.120   ! paf        41:        Pooled(src.pool()) {
1.8       paf        42:        head.count=CR_PREALLOCATED_COUNT;
                     43:        
1.116     paf        44:        uint src_used_rows=src.used_rows();
1.8       paf        45:        if(src_used_rows<=head.count) {
1.55      paf        46:                // all new rows fit size_to preallocated area
1.98      parser     47:                last_chunk=&head;
1.116     paf        48:                uint curr_chunk_rows=head.count;
1.8       paf        49:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
                     50:                append_here=&head.rows[src_used_rows];
                     51:                link_row=&head.rows[curr_chunk_rows];
                     52:        } else {
                     53:                // warning: 
1.10      paf        54:                //   heavily relies on the fact 
                     55:                //   "preallocated area is the same for all strings"
1.8       paf        56:                //
                     57:                // info:
                     58:                //   allocating only enough mem to fit src string rows
                     59:                //   next append would allocate a new chunk
                     60:                //
1.55      paf        61:                // new rows don't fit size_to preallocated area: splitting size_to two chunks
1.8       paf        62:                // preallocated chunk src to constructing head
                     63:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
1.55      paf        64:                // remaining rows size_to new_chunk
1.116     paf        65:                uint curr_chunk_rows=src_used_rows-head.count;
1.98      parser     66:                last_chunk=static_cast<Chunk *>(
1.116     paf        67:                        malloc(sizeof(uint)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *), 9));
1.98      parser     68:                last_chunk->count=curr_chunk_rows;
                     69:                head.preallocated_link=last_chunk;
                     70:                append_here=link_row=&last_chunk->rows[last_chunk->count];
1.8       paf        71: 
                     72:                Chunk *old_chunk=src.head.preallocated_link; 
1.98      parser     73:                Chunk::Row *new_rows=last_chunk->rows;
1.116     paf        74:                uint rows_left_to_copy=last_chunk->count;
1.8       paf        75:                while(true) {
1.116     paf        76:                        uint old_count=old_chunk->count;
1.8       paf        77:                        Chunk *next_chunk=old_chunk->rows[old_count].link;
                     78:                        if(next_chunk) {
                     79:                                // not last source chunk
                     80:                                // taking it all
                     81:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
                     82:                                new_rows+=old_count;
                     83:                                rows_left_to_copy-=old_count;
                     84: 
                     85:                                old_chunk=next_chunk;
                     86:                        } else {
                     87:                                // the last source chunk
                     88:                                // taking only those rows of chunk that _left_to_copy
                     89:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
                     90:                                break;
                     91:                        }
                     92:                }
1.5       paf        93:        }
1.8       paf        94:        link_row->link=0;
1.115     paf        95:        src_used_rows;
1.120   ! paf        96: }
        !            97: 
        !            98: size_t  String::size() const {
        !            99:        size_t result=0;
        !           100:        const Chunk *chunk=&head; 
        !           101:        do {
        !           102:                const Chunk::Row *row=chunk->rows;
        !           103:                for(uint i=0; i<chunk->count; i++, row++) {
        !           104:                        if(row==append_here)
        !           105:                                goto break2;
        !           106: 
        !           107:                        result+=row->item.size;
        !           108:                }
        !           109:                chunk=row->link;
        !           110:        } while(chunk);
        !           111: 
        !           112: break2:
        !           113:        return result;
1.94      parser    114: }
                    115: 
1.115     paf       116: /// @todo not very optimal
                    117: uint String::used_rows() const {
                    118:        uint result=0;
                    119:        const Chunk *chunk=&head; 
                    120:        do {
                    121:                const Chunk::Row *row=chunk->rows;
1.116     paf       122:                for(uint i=0; i<chunk->count; i++, row++) {
1.115     paf       123:                        if(row==append_here)
                    124:                                goto break2;
                    125: 
                    126:                        result++;
                    127:                }
                    128:                chunk=row->link;
                    129:        } while(chunk);
                    130: 
                    131: break2:
                    132:        return result;
                    133: }
1.94      parser    134: void String::expand() {
1.116     paf       135:        uint new_chunk_count=last_chunk->count+CR_GROW_COUNT;
1.94      parser    136:        last_chunk=static_cast<Chunk *>(
1.116     paf       137:                malloc(sizeof(uint)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *), 10));
1.94      parser    138:        last_chunk->count=new_chunk_count;
                    139:        link_row->link=last_chunk;
                    140:        append_here=last_chunk->rows;
                    141:        link_row=&last_chunk->rows[last_chunk->count];
                    142:        link_row->link=0;
1.5       paf       143: }
1.28      paf       144: 
1.42      paf       145: String& String::append(const String& src, Untaint_lang lang, bool forced) {
1.60      paf       146:        const Chunk *chunk=&src.head; 
1.40      paf       147:        do {
1.60      paf       148:                const Chunk::Row *row=chunk->rows;
1.116     paf       149:                for(uint i=0; i<chunk->count; i++, row++) {
1.60      paf       150:                        if(row==src.append_here)
1.40      paf       151:                                goto break2;
1.60      paf       152:                        
                    153:                        APPEND(row->item.ptr, row->item.size, 
1.117     paf       154:                                (lang!=UL_PASS_APPENDED && (row->item.lang==UL_TAINTED || forced))?lang:(Untaint_lang)row->item.lang,
1.60      paf       155:                                row->item.origin.file, row->item.origin.line);
1.40      paf       156:                }
                    157:                chunk=row->link;
                    158:        } while(chunk);
                    159: break2:
1.60      paf       160:        return *this;
1.34      paf       161: }
1.60      paf       162: 
1.13      paf       163: String& String::real_append(STRING_APPEND_PARAMS) {
1.9       paf       164:        if(!src)
                    165:                return *this;
1.26      paf       166:        if(!size)
                    167:                size=strlen(src);
                    168:        if(!size)
1.9       paf       169:                return *this;
                    170: 
1.1       paf       171:        if(chunk_is_full())
                    172:                expand();
                    173: 
                    174:        append_here->item.ptr=src;
1.52      paf       175:        append_here->item.lang=lang;
1.13      paf       176: #ifndef NO_STRING_ORIGIN
1.14      paf       177:        append_here->item.origin.file=file;
                    178:        append_here->item.origin.line=line;
1.13      paf       179: #endif
1.115     paf       180:        append_here++;
1.1       paf       181: 
                    182:        return *this;
1.97      parser    183: }
                    184: 
                    185: char String::first_char() const {
1.120   ! paf       186:        if(!used_rows())
1.112     parser    187:                throw Exception(0, 0,
1.97      parser    188:                        this,
                    189:                        "getting first char of empty string");
                    190: 
                    191:        return *head.rows[0].item.ptr;
1.1       paf       192: }
                    193: 
1.16      paf       194: uint String::hash_code() const {
1.7       paf       195:        uint result=0;
1.5       paf       196: 
1.16      paf       197:        const Chunk *chunk=&head; 
1.5       paf       198:        do {
1.16      paf       199:                const Chunk::Row *row=chunk->rows;
1.116     paf       200:                for(uint i=0; i<chunk->count; i++) {
1.5       paf       201:                        if(row==append_here)
                    202:                                goto break2;
                    203: 
1.6       paf       204:                        result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5       paf       205:                        row++;
                    206:                }
                    207:                chunk=row->link;
                    208:        } while(chunk);
                    209: break2:
                    210:        return result;
                    211: }
                    212: 
1.60      paf       213: /// @todo move 'lang' skipping to pos
                    214: int String::cmp(int& partial, const String& src, 
                    215:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       216:        partial=-1;
1.55      paf       217:        this_offset=min(this_offset, size()-1);
                    218: 
1.16      paf       219:        const Chunk *a_chunk=&head;
                    220:        const Chunk *b_chunk=&src.head;
                    221:        const Chunk::Row *a_row=a_chunk->rows;
                    222:        const Chunk::Row *b_row=b_chunk->rows;
1.55      paf       223:        size_t a_offset=this_offset;
                    224:        size_t b_offset=0;
1.9       paf       225:        Chunk::Row *a_end=append_here;
                    226:        Chunk::Row *b_end=src.append_here;
1.116     paf       227:        uint a_countdown=a_chunk->count;
                    228:        uint b_countdown=b_chunk->count;
                    229:        int result;
1.60      paf       230:        size_t pos=0; 
1.33      paf       231: 
1.83      parser    232:        bool a_break=size()==0;
1.91      parser    233:        bool b_break=src.size()==0;
1.83      parser    234:        if(!(a_break || b_break)) while(true) {
1.55      paf       235:                if(pos+a_row->item.size > this_offset) {
1.71      paf       236:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
1.60      paf       237:                                return -1; // wrong lang -- bail out
                    238: 
1.55      paf       239:                        int size_diff=
                    240:                                (a_row->item.size-a_offset)-
                    241:                                (b_row->item.size-b_offset);
                    242:                        
                    243:                        if(size_diff==0) { // a has same size as b
1.60      paf       244:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    245:                                        a_row->item.size-a_offset);
1.55      paf       246:                                if(result)
                    247:                                        return result;
1.60      paf       248:                                pos+=a_row->item.size;
1.55      paf       249:                                a_row++; a_countdown--; a_offset=0;
                    250:                                b_row++; b_countdown--; b_offset=0;
                    251:                        } else if (size_diff>0) { // a longer
1.60      paf       252:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    253:                                        b_row->item.size-b_offset);
1.55      paf       254:                                if(result)
                    255:                                        return result;
                    256:                                a_offset+=b_row->item.size-b_offset;
                    257:                                b_row++; b_countdown--; b_offset=0;
                    258:                        } else { // b longer
1.60      paf       259:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    260:                                        a_row->item.size-a_offset);
1.55      paf       261:                                if(result)
                    262:                                        return result;
                    263:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       264:                                pos+=a_row->item.size;
1.55      paf       265:                                a_row++; a_countdown--; a_offset=0;
                    266:                        }
1.83      parser    267:                        if(b_break=b_row==b_end) {
                    268:                                a_break=a_row==a_end;
                    269:                                break;                  
                    270:                        }
1.55      paf       271:                        if(!b_countdown) {
                    272:                                b_chunk=b_row->link;
                    273:                                b_row=b_chunk->rows;
                    274:                                b_countdown=b_chunk->count;
                    275:                        }
                    276:                } else {
1.60      paf       277:                        a_offset-=a_row->item.size;
                    278:                        pos+=a_row->item.size;
                    279:                        a_row++; a_countdown--; 
1.9       paf       280:                }
                    281: 
1.83      parser    282:                if(a_break=a_row==a_end) {
                    283:                        b_break=b_row==b_end;
                    284:                        break;
                    285:                }
1.11      paf       286:                if(!a_countdown) {
1.9       paf       287:                        a_chunk=a_row->link;
                    288:                        a_row=a_chunk->rows;
1.11      paf       289:                        a_countdown=a_chunk->count;
1.9       paf       290:                }
1.27      paf       291:        }
1.55      paf       292:        if(a_break==b_break) { // ended simultaneously
                    293:                partial=0; return 0;
                    294:        } else if(a_break) { // first bytes equal, but a ended before b
                    295:                partial=1; return -1;
                    296:        } else {
                    297:                partial=2; return +1;
                    298:        }
1.27      paf       299: }
                    300: 
1.60      paf       301: /// @todo move 'lang' skipping to pos
1.59      paf       302: int String::cmp(int& partial, const char* b_ptr, size_t src_size, 
1.60      paf       303:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       304:        partial=-1;
1.50      paf       305:        size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.59      paf       306:        this_offset=min(this_offset, size()-1);
1.27      paf       307: 
                    308:        const Chunk *a_chunk=&head;
                    309:        const Chunk::Row *a_row=a_chunk->rows;
1.59      paf       310:        size_t a_offset=this_offset;
1.55      paf       311:        size_t b_offset=0;
1.27      paf       312:        Chunk::Row *a_end=append_here;
1.116     paf       313:        uint a_countdown=a_chunk->count;
1.60      paf       314:        size_t pos=0;
1.52      paf       315: 
1.83      parser    316:        bool a_break=size()==0;
                    317:        bool b_break=b_size==0;
                    318:        if(!(a_break || b_break)) while(true) {
1.59      paf       319:                if(pos+a_row->item.size > this_offset) {
1.71      paf       320:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
1.60      paf       321:                                return -1; // wrong lang -- bail out
                    322: 
1.59      paf       323:                        int size_diff=
                    324:                                (a_row->item.size-a_offset)-
                    325:                                (b_size-b_offset);
                    326:                        
                    327:                        if(size_diff==0) { // a has same size as b
1.116     paf       328:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       329:                                        a_row->item.size-a_offset)!=0)
                    330:                                        return result;
1.60      paf       331:                                pos+=a_row->item.size;
1.59      paf       332:                                a_row++; a_countdown--; a_offset=0;
                    333:                                b_break=true;
                    334:                        } else if (size_diff>0) { // a longer
1.116     paf       335:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       336:                                        b_size-b_offset)!=0)
                    337:                                        return result;
                    338:                                a_offset+=b_size-b_offset;
                    339:                                b_break=true;
                    340:                        } else { // b longer
1.116     paf       341:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       342:                                        a_row->item.size-a_offset)!=0)
                    343:                                        return result;
                    344:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       345:                                pos+=a_row->item.size;
1.59      paf       346:                                a_row++; a_countdown--; a_offset=0;
                    347:                        }
                    348:                } else {
1.60      paf       349:                        a_offset-=a_row->item.size; 
                    350:                        pos+=a_row->item.size;
                    351:                        a_row++; a_countdown--; 
1.27      paf       352:                }
                    353: 
1.86      parser    354:                a_break=a_row==a_end;
                    355:                if(a_break || b_break)
1.83      parser    356:                        break;
1.27      paf       357:                if(!a_countdown) {
                    358:                        a_chunk=a_row->link;
                    359:                        a_row=a_chunk->rows;
                    360:                        a_countdown=a_chunk->count;
1.9       paf       361:                }
                    362:        }
1.55      paf       363:        if(a_break==b_break) { // ended simultaneously
                    364:                partial=0; return 0;
                    365:        } else if(a_break) { // first bytes equal, but a ended before b
                    366:                partial=1; return -1;
                    367:        } else {
                    368:                partial=2; return +1;
                    369:        }
1.5       paf       370: }
1.46      paf       371: 
                    372: #ifndef NO_STRING_ORIGIN
                    373: const Origin& String::origin() const { 
1.120   ! paf       374:        if(!used_rows()) {
1.96      parser    375:                static const Origin empty_origin={"empty string"};
                    376:                return empty_origin;
                    377:        }
1.46      paf       378:        
1.49      paf       379:        // determining origin by last appended piece
1.50      paf       380:        // because first one frequently constant. 
                    381:        // ex: ^load[/file] "document_root" + "/file"
1.80      paf       382:        // when last peice is constant, 
                    383:        // ex: parser_root_auto_path{dynamic} / auto.p{const}
                    384:        // using first piece
                    385:        Origin& last_origin=append_here[-1].item.origin;
                    386:        return last_origin.file ? last_origin : head.rows[0].item.origin;
1.46      paf       387: }
                    388: #endif
1.53      paf       389: 
1.69      paf       390: String& String::mid(size_t start, size_t finish) const {
1.107     parser    391:        String& result=*NEW String(pool());
                    392: 
1.53      paf       393:        start=max(0, start);
1.111     parser    394:        finish=min(size(), finish);
1.60      paf       395:        if(start==finish)
1.107     parser    396:                return result;
1.53      paf       397: 
                    398:        size_t pos=0;
                    399:        const Chunk *chunk=&head; 
                    400:        do {
                    401:                const Chunk::Row *row=chunk->rows;
1.116     paf       402:                for(uint i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53      paf       403:                        if(row==append_here)
                    404:                                goto break2;
                    405: 
1.60      paf       406:                        size_t item_finish=pos+row->item.size;
                    407:                        if(item_finish > start) { // started now or already?
                    408:                                bool started=result.size()==0; // started now?
                    409:                                bool finished=finish <= item_finish; // finished now?
1.53      paf       410:                                size_t offset=started?start-pos:0;
                    411:                                size_t size=finished?finish-pos:row->item.size;
                    412:                                result.APPEND(
                    413:                                        row->item.ptr+offset, size-offset, 
1.117     paf       414:                                        (Untaint_lang)row->item.lang,
1.53      paf       415:                                        row->item.origin.file, row->item.origin.line);
                    416:                                if(finished)
                    417:                                        goto break2;
                    418:                        }
                    419:                }
                    420:                chunk=row->link;
                    421:        } while(chunk);
                    422: break2:
1.60      paf       423: //     SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
                    424:                //cstr(), start, finish, result.cstr());
1.53      paf       425:        return result;
1.54      paf       426: }
                    427: 
1.60      paf       428: int String::pos(const String& substr, 
1.116     paf       429:                                int result, Untaint_lang lang) const {
1.58      paf       430:        for(; result<size(); result++) {
1.60      paf       431:                int partial; cmp(partial, substr, result, lang);
1.58      paf       432:                if(
                    433:                        partial==0 || // full match
                    434:                        partial==2) // 'substr' starts 'this'+'result'
                    435:                        return result;
                    436:        }
                    437:        
                    438:        return -1;
                    439: }
                    440: 
1.60      paf       441: int String::pos(const char *substr, size_t substr_size, 
1.116     paf       442:                                int result, Untaint_lang lang) const {
1.57      paf       443:        for(; result<size(); result++) {
1.60      paf       444:                int partial; cmp(partial, substr, substr_size, result, lang);
1.55      paf       445:                if(
                    446:                        partial==0 || // full match
                    447:                        partial==2) // 'substr' starts 'this'+'result'
                    448:                        return result;
                    449:        }
                    450:        
                    451:        return -1;
1.60      paf       452: }
                    453: 
                    454: void String::split(Array& result, 
                    455:                                   size_t* pos_after_ref, 
                    456:                                   const char *delim, size_t delim_size, 
                    457:                                   Untaint_lang lang, int limit) const {
                    458:        if(delim_size) {
                    459:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    460:                int pos_before;
                    461:                // while we have 'delim'...
                    462:                for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       463:                        result+=&mid(pos_after, pos_before);
1.60      paf       464:                        pos_after=pos_before+delim_size;
                    465:                }
                    466:                // last piece
                    467:                if(pos_after<size() && limit) {
1.69      paf       468:                        result+=&mid(pos_after, size());
1.60      paf       469:                        pos_after=size();
                    470:                }
                    471:                if(pos_after_ref)
                    472:                        *pos_after_ref=pos_after;
                    473:        } else { // empty delim
                    474:                result+=this;
                    475:                if(pos_after_ref)
                    476:                        *pos_after_ref+=size();
                    477:        }
                    478: }
                    479: 
                    480: void String::split(Array& result, 
                    481:                                   size_t* pos_after_ref, 
                    482:                                   const String& delim, Untaint_lang lang, 
                    483:                                   int limit) const {
                    484:        if(delim.size()) {
                    485:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    486:                int pos_before;
                    487:                // while we have 'delim'...
                    488:                for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       489:                        result+=&mid(pos_after, pos_before);
1.60      paf       490:                        pos_after=pos_before+delim.size();
                    491:                }
                    492:                // last piece
                    493:                if(pos_after<size() && limit) {
1.69      paf       494:                        result+=&mid(pos_after, size());
1.60      paf       495:                        pos_after=size();
                    496:                }
                    497:                if(pos_after_ref)
                    498:                        *pos_after_ref=pos_after;
                    499:        } else { // empty delim
                    500:                result+=this;
                    501:                if(pos_after_ref)
                    502:                        *pos_after_ref+=size();
                    503:        }
1.61      paf       504: }
                    505: 
1.63      paf       506: static void regex_options(char *options, int *result){
                    507:     struct Regex_option {
                    508:                char key;
                    509:                int clear, set;
                    510:                int *result;
                    511:     } regex_option[]={
                    512:                {'i', 0, PCRE_CASELESS, result}, // a=A
1.79      paf       513:                {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63      paf       514:                {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    515:                {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    516:                {'g', 0, true, result+1}, // many rows
                    517:                {0},
                    518:     };
                    519:        result[0]=PCRE_EXTRA | PCRE_DOTALL;
                    520:        result[1]=0;
                    521: 
                    522:     if(options) 
                    523:                for(Regex_option *o=regex_option; o->key; o++) 
                    524:                        if(
                    525:                                strchr(options, o->key) || 
                    526:                                strchr(options, toupper(o->key))) {
                    527:                                *(o->result)&=~o->clear;
                    528:                                *(o->result)|=o->set;
                    529:                        }
                    530: }
                    531: 
1.88      parser    532: /// @todo maybe need speedup: some option to remove pre/match/post string generation
1.77      paf       533: bool String::match(const unsigned char *pcre_tables,
                    534:                                   const String *aorigin,
1.62      paf       535:                                   const String& regexp, 
1.63      paf       536:                                   const String *options,
1.64      paf       537:                                   Table **table,
1.95      parser    538:                                   Row_action row_action, void *info,
                    539:                                   bool *was_global) const { 
1.64      paf       540: 
1.73      paf       541:        if(!regexp.size())
1.112     parser    542:                throw Exception(0, 0,
1.73      paf       543:                        aorigin,
                    544:                        "regexp is empty");
1.118     paf       545:        const char *pattern=regexp.cstr();
1.62      paf       546:        const char *errptr;
                    547:        int erroffset;
1.63      paf       548:     int option_bits[2];  regex_options(options?options->cstr():0, option_bits);
1.95      parser    549:        if(was_global)
                    550:                *was_global=option_bits[1]!=0;
1.63      paf       551:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       552:                &errptr, &erroffset,
1.74      paf       553:                pcre_tables);
1.62      paf       554: 
1.67      paf       555:        if(!code)
1.112     parser    556:                throw Exception(0, 0,
1.69      paf       557:                        &regexp.mid(erroffset, regexp.size()),
1.74      paf       558:                        "regular expression syntax error - %s", errptr);
1.62      paf       559:        
1.63      paf       560:        int info_substrings=pcre_info(code, 0, 0);
                    561:        if(info_substrings<0) {
1.100     parser    562:                pcre_free(code);
1.112     parser    563:                throw Exception(0, 0,
1.73      paf       564:                        aorigin,
1.76      paf       565:                        "pcre_info error (%d)", 
1.73      paf       566:                                info_substrings);
1.63      paf       567:        }
                    568: 
                    569:        int startoffset=0;
1.118     paf       570:        const char *subject=cstr();
1.62      paf       571:        int length=strlen(subject);
1.63      paf       572:        int ovecsize;
                    573:        int *ovector=(int *)malloc(sizeof(int)*
1.114     paf       574:                (ovecsize=(1/*match*/+info_substrings)*3), 11);
1.62      paf       575: 
1.64      paf       576:        { // create table
                    577:                Array& columns=*NEW Array(pool());
                    578:                columns+=string_pre_match_name;
                    579:                columns+=string_match_name;
                    580:                columns+=string_post_match_name;
                    581:                for(int i=1; i<=info_substrings; i++) {
                    582:                        char *column=(char *)malloc(MAX_NUMBER);
                    583:                        snprintf(column, MAX_NUMBER, "%d", i);
                    584:                        columns+=NEW String(pool(), column); // .i column name
                    585:                }
                    586:                *table=NEW Table(pool(), aorigin, &columns);
1.62      paf       587:        }
1.63      paf       588: 
1.64      paf       589:        int exec_option_bits=0;
1.63      paf       590:        while(true) {
                    591:                int exec_substrings=pcre_exec(code, 0,
                    592:                        subject, length, startoffset,
1.64      paf       593:                        exec_option_bits, ovector, ovecsize);
1.63      paf       594:                
                    595:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    596:                        pcre_free(code);
                    597:                        row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       598:                        return option_bits[1]!=0; // global=true+table, not global=false
                    599:                }
                    600: 
                    601:                if(exec_substrings<0) {
1.100     parser    602:                        pcre_free(code);
1.112     parser    603:                        throw Exception(0, 0,
1.63      paf       604:                                aorigin,
1.76      paf       605:                                "regular expression execute error (%d)", 
1.63      paf       606:                                        exec_substrings);
                    607:                }
                    608: 
                    609:                Array& row=*NEW Array(pool());
1.81      paf       610:                row+=&mid(0, ovector[0]); // .prematch column value
1.69      paf       611:                row+=&mid(ovector[0], ovector[1]); // .match
1.81      paf       612:                row+=&mid(ovector[1], size()); // .postmatch
1.63      paf       613:                
                    614:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       615:                        // -1:-1 case handled peacefully by mid() itself
                    616:                        row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       617:                }
                    618:                
1.100     parser    619:                row_action(**table, &row, startoffset, ovector[0], info);
1.63      paf       620: 
1.100     parser    621:                if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang
                    622:                        pcre_free(code);
                    623:                        row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       624:                        return true;
                    625:                }
1.100     parser    626:                startoffset=ovector[1];
1.63      paf       627: 
                    628: /*
                    629:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       630:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       631: */
                    632:        }
1.82      parser    633: }
                    634: 
                    635: String& String::change_case(Pool& pool, const unsigned char *tables, 
                    636:                                                        Change_case_kind kind) const {
                    637:        String& result=*new(pool) String(pool);
                    638: 
                    639:        const unsigned char *a;
                    640:        const unsigned char *b;
                    641:        switch(kind) {
                    642:        case CC_UPPER:
                    643:                a=tables+lcc_offset;
                    644:                b=tables+fcc_offset;
                    645:                break;
                    646:        case CC_LOWER:
                    647:                a=tables+lcc_offset;
                    648:                b=0;
                    649:                break;
                    650:        default:
1.112     parser    651:                throw Exception(0, 0, 
1.82      parser    652:                        this, 
                    653:                        "unknown change case kind #%d", 
                    654:                                static_cast<int>(kind)); // never
                    655:                a=b=0; // calm, compiler
                    656:                break; // never
                    657:        }       
                    658: 
                    659:        const Chunk *chunk=&head; 
                    660:        do {
                    661:                const Chunk::Row *row=chunk->rows;
1.116     paf       662:                for(uint i=0; i<chunk->count; i++, row++) {
1.82      parser    663:                        if(row==append_here)
                    664:                                goto break2;
                    665: 
1.114     paf       666:                        char *new_cstr=(char *)pool.malloc(row->item.size, 12);
1.82      parser    667:                        char *dest=new_cstr;
                    668:                        const char *src=row->item.ptr; 
                    669:                        for(int size=row->item.size; size--; src++) {
                    670:                                unsigned char c=a[(unsigned char)*src];
                    671:                                if(b)
                    672:                                        c=b[c];
                    673: 
                    674:                                *dest++=(char)c;
                    675:                        }
                    676:                        
                    677:                        result.APPEND(new_cstr, row->item.size, 
1.117     paf       678:                                (Untaint_lang)row->item.lang,
1.82      parser    679:                                row->item.origin.file, row->item.origin.line);
                    680:                }
                    681:                chunk=row->link;
                    682:        } while(chunk);
                    683: break2:
1.89      parser    684: 
1.101     parser    685:        return result;
                    686: }
                    687: 
1.108     parser    688: void String::join_chain(Pool& pool, 
1.116     paf       689:                                           uint& ai, const Chunk*& achunk, const Chunk::Row*& arow,
1.108     parser    690:                                           Untaint_lang& joined_lang, const char *& joined_ptr, size_t& joined_size) const {
1.117     paf       691:        joined_lang=(Untaint_lang)arow->item.lang;
1.108     parser    692:        
                    693:        // calc size
                    694:        joined_size=0;
                    695:        {
1.116     paf       696:                uint start_i=ai;
1.108     parser    697:                const Chunk::Row *start_row=arow;
                    698:                const Chunk *chunk=achunk;
                    699:                do {
                    700:                        const Chunk::Row *row=start_row;
1.116     paf       701:                        for(uint i=start_i; i<chunk->count; i++, row++) {
1.108     parser    702:                                if(row==append_here)
                    703:                                        goto break21;
                    704:                                
                    705:                                if(row->item.lang==joined_lang)
                    706:                                        joined_size+=row->item.size;
                    707:                                else
                    708:                                        break;
                    709:                        }
                    710:                        if(chunk=row->link) {
                    711:                                start_i=0;
                    712:                                start_row=chunk->rows;
                    713:                        } else
                    714:                                break;
                    715:                } while(true);
                    716: break21:;
                    717:        }
                    718: 
                    719:        // if one row, return simply itself
                    720:        if(joined_size==arow->item.size) {
                    721:                joined_ptr=arow->item.ptr;
                    722:                ai++; arow++;
                    723:                if(ai==achunk->count)
                    724:                        achunk=arow->link;              
                    725:        } else {
                    726:                // join adjacent rows
1.114     paf       727:                char *ptr=(char *)pool.malloc(joined_size,13);
1.108     parser    728:                joined_ptr=ptr;
1.116     paf       729:                uint start_i=ai;
1.108     parser    730:                const Chunk::Row *start_row=arow;
                    731:                const Chunk *chunk=achunk;
1.116     paf       732:                uint i;
1.108     parser    733:                const Chunk::Row *row;
                    734:                do {
                    735:                        row=start_row;
                    736:                        for(i=start_i; i<chunk->count; i++, row++) {
                    737:                                if(row==append_here)
                    738:                                        goto break22;
                    739:                                
                    740:                                if(row->item.lang==joined_lang) {
                    741:                                        memcpy(ptr, row->item.ptr, row->item.size);
                    742:                                        ptr+=row->item.size;
                    743:                                } else
                    744:                                        break;
                    745:                        }
                    746:                        if(chunk=row->link) {
                    747:                                start_i=0;
                    748:                                start_row=chunk->rows;
                    749:                        } else
                    750:                                break;
                    751:                } while(true);
                    752: break22:;
                    753:                
                    754:                // return joined rows
                    755:                ai=i;
                    756:                arow=row;
                    757:                achunk=chunk;
                    758:        }
                    759: }
                    760: 
                    761: String& String::reconstruct(Pool& pool) const {
                    762:        //_asm int 3;
                    763:        String& result=*new(pool) String(pool);
                    764:        const Chunk *chunk=&head; 
                    765:        do {
                    766:                const Chunk::Row *row=chunk->rows;
1.116     paf       767:                for(uint i=0; i<chunk->count; ) {
1.108     parser    768:                        if(row==append_here)
                    769:                                goto break2;
                    770: 
                    771:                        Untaint_lang joined_lang;
1.109     parser    772:                        const char *joined_ptr;
1.108     parser    773:                        size_t joined_size;
                    774:                        join_chain(pool, i, chunk, row,
                    775:                                joined_lang, joined_ptr, joined_size);
                    776: 
                    777:                        result.APPEND(joined_ptr, joined_size, 
                    778:                                joined_lang,
                    779:                                row->item.origin.file, row->item.origin.line);
                    780:                        if(!chunk)
                    781:                                goto break2;
                    782:                }
                    783:        } while(true);
                    784: break2:
                    785: 
                    786:        return result;
                    787: };
                    788: 
                    789: String& String::replace_in_reconstructed(Pool& pool, Dictionary& dict) const {
1.106     parser    790:        //_asm int 3;
1.101     parser    791:        String& result=*new(pool) String(pool);
                    792:        const Chunk *chunk=&head; 
                    793:        do {
                    794:                const Chunk::Row *row=chunk->rows;
1.116     paf       795:                for(uint i=0; i<chunk->count; i++, row++) {
1.101     parser    796:                        if(row==append_here)
                    797:                                goto break2;
                    798: 
                    799:                        const char *src=row->item.ptr; 
                    800:                        size_t src_size=row->item.size;
1.114     paf       801:                        char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);
1.101     parser    802:                        char *dest=new_cstr;
                    803:                        while(src_size) {
                    804:                                // there is a row where first column starts 'src'
1.106     parser    805:                                if(Table::Item *item=dict.first_that_starts(src, src_size)) {
1.101     parser    806:                                        // get a=>b values
                    807:                                        const String& a=*static_cast<Array *>(item)->get_string(0);
                    808:                                        const String& b=*static_cast<Array *>(item)->get_string(1);
1.105     parser    809:                                        // skip 'a' in 'src' && reduce work size
                    810:                                        src+=a.size();  src_size-=a.size();
                    811:                                        // write 'b' to 'dest' && skip 'b' in 'dest'
                    812:                                        b.store_to(dest);  dest+=b.size();
1.101     parser    813:                                } else {
1.105     parser    814:                                        // write a char to b && reduce work size
                    815:                                        *dest++=*src++;  src_size--;
1.101     parser    816:                                }
                    817:                        }
                    818: 
                    819:                        result.APPEND(new_cstr, dest-new_cstr, 
1.117     paf       820:                                (Untaint_lang)row->item.lang,
1.101     parser    821:                                row->item.origin.file, row->item.origin.line);
                    822:                }
                    823:                chunk=row->link;
                    824:        } while(chunk);
                    825: 
                    826: break2:
1.89      parser    827:        return result;
1.108     parser    828: }
                    829: 
                    830: String& String::replace(Pool& pool, Dictionary& dict) const {
                    831:        return reconstruct(pool).replace_in_reconstructed(pool, dict);
1.89      parser    832: }
                    833: 
1.90      parser    834: double String::as_double() const { 
1.89      parser    835:        double result;
1.114     paf       836:        const char *cstr;
                    837:        char buf[MAX_NUMBER];
1.115     paf       838:        if(head.rows+1==append_here) {
1.114     paf       839:                int size=min(head.rows[0].item.size, MAX_NUMBER-1);
                    840:                memcpy(buf, head.rows[0].item.ptr, size);
                    841:                buf[size]=0;
                    842:                cstr=buf;
                    843:        } else
                    844:                cstr=this->cstr();
1.102     parser    845:        char *error_pos;
1.89      parser    846:        // 0xABC
1.99      parser    847:        if(cstr[0]=='0')
                    848:                if(cstr[1]=='x' || cstr[1]=='X')
                    849:                        result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
                    850:                else
1.102     parser    851:                        result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89      parser    852:        else
1.99      parser    853:                result=(double)strtod(cstr, &error_pos);
1.89      parser    854: 
1.103     parser    855:        if(*error_pos/*not EOS*/)
1.112     parser    856:                throw Exception(0, 0,
1.89      parser    857:                        this,
                    858:                        "invalid number (double)");
                    859: 
                    860:        return result;
                    861: }
1.90      parser    862: int String::as_int() const { 
1.89      parser    863:        int result;
1.114     paf       864:        const char *cstr;
                    865:        char buf[MAX_NUMBER];
1.115     paf       866:        if(head.rows+1==append_here) {
1.114     paf       867:                int size=min(head.rows[0].item.size, MAX_NUMBER-1);
                    868:                memcpy(buf, head.rows[0].item.ptr, size);
                    869:                buf[size]=0;
                    870:                cstr=buf;
                    871:        } else
                    872:                cstr=this->cstr();
1.102     parser    873:        char *error_pos;
1.89      parser    874:        // 0xABC
1.99      parser    875:        if(cstr[0]=='0')
                    876:                if(cstr[1]=='x' || cstr[1]=='X')
                    877:                        result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
                    878:                else
1.102     parser    879:                        result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89      parser    880:        else
                    881:                result=(int)strtol(cstr, &error_pos, 0);
                    882: 
1.103     parser    883:        if(*error_pos/*not EOS*/)
1.112     parser    884:                throw Exception(0, 0,
1.89      parser    885:                        this,
                    886:                        "invalid number (int)");
1.82      parser    887: 
                    888:        return result;
1.61      paf       889: }
1.113     parser    890: 
                    891: /* @todo maybe network order worth spending some effort?
                    892:        don't bothering myself with network byte order,
                    893:        am not planning to be able to move resulting file across platforms
                    894:        for now
                    895: */
                    896: void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
                    897:        buf_size=
                    898:                prolog_size
1.115     paf       899:                +used_rows()*(sizeof(Untaint_lang)+sizeof(size_t))
1.113     parser    900:                +size();
1.114     paf       901:        buf=malloc(buf_size,15);
1.113     parser    902:        char *cur=(char *)buf+prolog_size;
                    903: 
                    904:        const Chunk *chunk=&head; 
                    905:        do {
                    906:                const Chunk::Row *row=chunk->rows;
1.116     paf       907:                for(uint i=0; i<chunk->count; i++) {
1.113     parser    908:                        if(row==append_here)
                    909:                                goto break2;
                    910: 
                    911:                        // lang
                    912:                        memcpy(cur, &row->item.lang, sizeof(Untaint_lang));
                    913:                        cur+=sizeof(Untaint_lang);
                    914:                        // size
                    915:                        memcpy(cur, &row->item.size, sizeof(size_t));
                    916:                        cur+=sizeof(size_t);
                    917:                        // bytes
                    918:                        memcpy(cur, row->item.ptr, row->item.size);
                    919:                        cur+=row->item.size;
                    920: 
                    921:                        row++;
                    922:                }
                    923:                chunk=row->link;
                    924:        } while(chunk);
                    925: break2:
                    926:        ;
                    927: }
                    928: 
                    929: /* @todo maybe network order worth spending some effort?
                    930:        don't bothering myself with network byte order,
                    931:        am not planning to be able to move resulting file across platforms
                    932:        for now
                    933: */
                    934: #ifndef DOXYGEN
                    935: struct Serialized_piece {
                    936:        String::Untaint_lang lang;
                    937:        size_t size;
                    938:        char ptr[1];
                    939: };
                    940: #endif
                    941: 
                    942: void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
                    943:        char *cur=((char *)buf)+prolog_size;
                    944:        buf_size-=prolog_size;
                    945: 
                    946:        while(buf_size) {
                    947:                Serialized_piece& p=*(Serialized_piece *)cur;
                    948:                APPEND(p.ptr, p.size, p.lang, file, 0);
                    949: 
                    950:                size_t piece_size=sizeof(p.lang)+sizeof(p.size)+p.size;
                    951:                cur+=piece_size;
                    952:                buf_size-=piece_size;
                    953:        }
                    954: }
                    955: 

E-mail: