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

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

E-mail: