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

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

E-mail: