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

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.46      paf         5: 
1.37      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.36      paf         7: 
1.83    ! parser      8:        $Id: pa_string.C,v 1.82 2001/05/14 13:18:07 parser Exp $
1.4       paf         9: */
                     10: 
1.48      paf        11: #include "pa_config_includes.h"
1.1       paf        12: 
1.70      paf        13: #include "pcre.h"
1.82      parser     14: #include "internal.h"
1.70      paf        15: 
1.13      paf        16: #include "pa_pool.h"
1.12      paf        17: #include "pa_string.h"
1.5       paf        18: #include "pa_hash.h"
1.22      paf        19: #include "pa_exception.h"
1.53      paf        20: #include "pa_common.h"
1.60      paf        21: #include "pa_array.h"
                     22: #include "pa_globals.h"
1.61      paf        23: #include "pa_table.h"
1.62      paf        24: #include "pa_threads.h"
1.60      paf        25: 
1.83    ! parser     26: #include "pa_sapi.h"
        !            27: #define STRING_STAT_MAX_PIECES 1000
        !            28: int string_stat_pieces[STRING_STAT_MAX_PIECES];
        !            29: void log_string_stats(Pool& pool) {
        !            30:        for(int i=0; i<STRING_STAT_MAX_PIECES; i++)
        !            31:                if(int v=string_stat_pieces[i])
        !            32:                        SAPI::log(pool, "%i: %10d",     
        !            33:                                i, v);
        !            34: }
1.1       paf        35: 
1.18      paf        36: 
1.75      paf        37: String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
1.83    ! parser     38:        Pooled(apool),expand_times(0) {
        !            39:                string_stat_pieces[0]++;
1.28      paf        40:        last_chunk=&head;
                     41:        head.count=CR_PREALLOCATED_COUNT;
1.5       paf        42:        append_here=head.rows;
1.2       paf        43:        head.preallocated_link=0;
1.28      paf        44:        link_row=&head.rows[head.count];
1.8       paf        45:        fused_rows=fsize=0;
1.41      paf        46: 
                     47:        if(src)
1.75      paf        48:                if(tainted)
                     49:                        APPEND_TAINTED(src, src_size, 0, 0);
1.41      paf        50:                else
1.75      paf        51:                        APPEND_CLEAN(src, src_size, 0, 0);
1.1       paf        52: }
                     53: 
                     54: void String::expand() {
1.83    ! parser     55:        {
        !            56:                int index=min(++expand_times, STRING_STAT_MAX_PIECES-1);
        !            57:                if(index)
        !            58:                        string_stat_pieces[index-1]++;
        !            59:                string_stat_pieces[index]++;
        !            60:        }
        !            61: 
1.55      paf        62:        size_t new_chunk_count=last_chunk->count+last_chunk->count*CR_GROW_PERCENT/100;
1.28      paf        63:        last_chunk=static_cast<Chunk *>(
1.55      paf        64:                malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
1.28      paf        65:        last_chunk->count=new_chunk_count;
                     66:        link_row->link=last_chunk;
                     67:        append_here=last_chunk->rows;
                     68:        link_row=&last_chunk->rows[last_chunk->count];
1.8       paf        69:        link_row->link=0;
1.1       paf        70: }
                     71: 
1.40      paf        72: String::String(const String& src) :    Pooled(src.pool()) {
1.83    ! parser     73:        string_stat_pieces[0]++;
1.8       paf        74:        head.count=CR_PREALLOCATED_COUNT;
                     75:        
1.55      paf        76:        size_t src_used_rows=src.fused_rows;
1.8       paf        77:        if(src_used_rows<=head.count) {
1.55      paf        78:                // all new rows fit size_to preallocated area
                     79:                size_t curr_chunk_rows=head.count;
1.8       paf        80:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
                     81:                append_here=&head.rows[src_used_rows];
                     82:                link_row=&head.rows[curr_chunk_rows];
                     83:        } else {
                     84:                // warning: 
1.10      paf        85:                //   heavily relies on the fact 
                     86:                //   "preallocated area is the same for all strings"
1.8       paf        87:                //
                     88:                // info:
                     89:                //   allocating only enough mem to fit src string rows
                     90:                //   next append would allocate a new chunk
                     91:                //
1.55      paf        92:                // new rows don't fit size_to preallocated area: splitting size_to two chunks
1.8       paf        93:                // preallocated chunk src to constructing head
                     94:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
1.55      paf        95:                // remaining rows size_to new_chunk
                     96:                size_t curr_chunk_rows=src_used_rows-head.count;
1.8       paf        97:                Chunk *new_chunk=static_cast<Chunk *>(
1.55      paf        98:                        malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.8       paf        99:                new_chunk->count=curr_chunk_rows;
                    100:                head.preallocated_link=new_chunk;
1.28      paf       101:                append_here=link_row=&new_chunk->rows[new_chunk->count];
1.8       paf       102: 
                    103:                Chunk *old_chunk=src.head.preallocated_link; 
                    104:                Chunk::Row *new_rows=new_chunk->rows;
1.55      paf       105:                size_t rows_left_to_copy=new_chunk->count;
1.8       paf       106:                while(true) {
1.55      paf       107:                        size_t old_count=old_chunk->count;
1.8       paf       108:                        Chunk *next_chunk=old_chunk->rows[old_count].link;
                    109:                        if(next_chunk) {
                    110:                                // not last source chunk
                    111:                                // taking it all
                    112:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
                    113:                                new_rows+=old_count;
                    114:                                rows_left_to_copy-=old_count;
                    115: 
                    116:                                old_chunk=next_chunk;
                    117:                        } else {
                    118:                                // the last source chunk
                    119:                                // taking only those rows of chunk that _left_to_copy
                    120:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
                    121:                                break;
                    122:                        }
                    123:                }
1.5       paf       124:        }
1.8       paf       125:        link_row->link=0;
                    126:        fused_rows=src_used_rows;
                    127:        fsize=src.fsize;
1.5       paf       128: }
1.28      paf       129: 
1.42      paf       130: String& String::append(const String& src, Untaint_lang lang, bool forced) {
1.60      paf       131:        const Chunk *chunk=&src.head; 
1.40      paf       132:        do {
1.60      paf       133:                const Chunk::Row *row=chunk->rows;
                    134:                for(size_t i=0; i<chunk->count; i++, row++) {
                    135:                        if(row==src.append_here)
1.40      paf       136:                                goto break2;
1.60      paf       137:                        
                    138:                        APPEND(row->item.ptr, row->item.size, 
                    139:                                (lang!=UL_PASS_APPENDED && (row->item.lang==UL_TAINTED || forced))?lang:row->item.lang,
                    140:                                row->item.origin.file, row->item.origin.line);
1.40      paf       141:                }
                    142:                chunk=row->link;
                    143:        } while(chunk);
                    144: break2:
1.60      paf       145:        return *this;
1.34      paf       146: }
1.60      paf       147: 
1.13      paf       148: String& String::real_append(STRING_APPEND_PARAMS) {
1.9       paf       149:        if(!src)
                    150:                return *this;
1.26      paf       151:        if(!size)
                    152:                size=strlen(src);
                    153:        if(!size)
1.9       paf       154:                return *this;
                    155: 
1.1       paf       156:        if(chunk_is_full())
                    157:                expand();
                    158: 
                    159:        append_here->item.ptr=src;
1.26      paf       160:        fsize+=append_here->item.size=size;
1.52      paf       161:        append_here->item.lang=lang;
1.13      paf       162: #ifndef NO_STRING_ORIGIN
1.14      paf       163:        append_here->item.origin.file=file;
                    164:        append_here->item.origin.line=line;
1.13      paf       165: #endif
1.8       paf       166:        append_here++; fused_rows++;
1.1       paf       167: 
                    168:        return *this;
                    169: }
                    170: 
1.16      paf       171: uint String::hash_code() const {
1.7       paf       172:        uint result=0;
1.5       paf       173: 
1.16      paf       174:        const Chunk *chunk=&head; 
1.5       paf       175:        do {
1.16      paf       176:                const Chunk::Row *row=chunk->rows;
1.55      paf       177:                for(size_t i=0; i<chunk->count; i++) {
1.5       paf       178:                        if(row==append_here)
                    179:                                goto break2;
                    180: 
1.6       paf       181:                        result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5       paf       182:                        row++;
                    183:                }
                    184:                chunk=row->link;
                    185:        } while(chunk);
                    186: break2:
                    187:        return result;
                    188: }
                    189: 
1.60      paf       190: /// @todo move 'lang' skipping to pos
                    191: int String::cmp(int& partial, const String& src, 
                    192:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       193:        partial=-1;
1.55      paf       194:        this_offset=min(this_offset, size()-1);
                    195: 
1.16      paf       196:        const Chunk *a_chunk=&head;
                    197:        const Chunk *b_chunk=&src.head;
                    198:        const Chunk::Row *a_row=a_chunk->rows;
                    199:        const Chunk::Row *b_row=b_chunk->rows;
1.55      paf       200:        size_t a_offset=this_offset;
                    201:        size_t b_offset=0;
1.9       paf       202:        Chunk::Row *a_end=append_here;
                    203:        Chunk::Row *b_end=src.append_here;
1.55      paf       204:        size_t a_countdown=a_chunk->count;
                    205:        size_t b_countdown=b_chunk->count;
                    206:        size_t result;
1.60      paf       207:        size_t pos=0; 
1.33      paf       208: 
1.83    ! parser    209:        bool a_break=size()==0;
        !           210:        bool b_break=size()==0;
        !           211:        if(!(a_break || b_break)) while(true) {
1.55      paf       212:                if(pos+a_row->item.size > this_offset) {
1.71      paf       213:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
1.60      paf       214:                                return -1; // wrong lang -- bail out
                    215: 
1.55      paf       216:                        int size_diff=
                    217:                                (a_row->item.size-a_offset)-
                    218:                                (b_row->item.size-b_offset);
                    219:                        
                    220:                        if(size_diff==0) { // a has same size as b
1.60      paf       221:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    222:                                        a_row->item.size-a_offset);
1.55      paf       223:                                if(result)
                    224:                                        return result;
1.60      paf       225:                                pos+=a_row->item.size;
1.55      paf       226:                                a_row++; a_countdown--; a_offset=0;
                    227:                                b_row++; b_countdown--; b_offset=0;
                    228:                        } else if (size_diff>0) { // a longer
1.60      paf       229:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    230:                                        b_row->item.size-b_offset);
1.55      paf       231:                                if(result)
                    232:                                        return result;
                    233:                                a_offset+=b_row->item.size-b_offset;
                    234:                                b_row++; b_countdown--; b_offset=0;
                    235:                        } else { // b longer
1.60      paf       236:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    237:                                        a_row->item.size-a_offset);
1.55      paf       238:                                if(result)
                    239:                                        return result;
                    240:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       241:                                pos+=a_row->item.size;
1.55      paf       242:                                a_row++; a_countdown--; a_offset=0;
                    243:                        }
1.83    ! parser    244:                        if(b_break=b_row==b_end) {
        !           245:                                a_break=a_row==a_end;
        !           246:                                break;                  
        !           247:                        }
1.55      paf       248:                        if(!b_countdown) {
                    249:                                b_chunk=b_row->link;
                    250:                                b_row=b_chunk->rows;
                    251:                                b_countdown=b_chunk->count;
                    252:                        }
                    253:                } else {
1.60      paf       254:                        a_offset-=a_row->item.size;
                    255:                        pos+=a_row->item.size;
                    256:                        a_row++; a_countdown--; 
1.9       paf       257:                }
                    258: 
1.83    ! parser    259:                if(a_break=a_row==a_end) {
        !           260:                        b_break=b_row==b_end;
        !           261:                        break;
        !           262:                }
1.11      paf       263:                if(!a_countdown) {
1.9       paf       264:                        a_chunk=a_row->link;
                    265:                        a_row=a_chunk->rows;
1.11      paf       266:                        a_countdown=a_chunk->count;
1.9       paf       267:                }
1.27      paf       268:        }
1.55      paf       269:        if(a_break==b_break) { // ended simultaneously
                    270:                partial=0; return 0;
                    271:        } else if(a_break) { // first bytes equal, but a ended before b
                    272:                partial=1; return -1;
                    273:        } else {
                    274:                partial=2; return +1;
                    275:        }
1.27      paf       276: }
                    277: 
1.60      paf       278: /// @todo move 'lang' skipping to pos
1.59      paf       279: int String::cmp(int& partial, const char* b_ptr, size_t src_size, 
1.60      paf       280:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       281:        partial=-1;
1.50      paf       282:        size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.59      paf       283:        this_offset=min(this_offset, size()-1);
1.27      paf       284: 
                    285:        const Chunk *a_chunk=&head;
                    286:        const Chunk::Row *a_row=a_chunk->rows;
1.59      paf       287:        size_t a_offset=this_offset;
1.55      paf       288:        size_t b_offset=0;
1.27      paf       289:        Chunk::Row *a_end=append_here;
1.55      paf       290:        size_t a_countdown=a_chunk->count;
1.60      paf       291:        size_t pos=0;
1.52      paf       292: 
1.83    ! parser    293:        bool a_break=size()==0;
        !           294:        bool b_break=b_size==0;
        !           295:        if(!(a_break || b_break)) while(true) {
1.59      paf       296:                if(pos+a_row->item.size > this_offset) {
1.71      paf       297:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
1.60      paf       298:                                return -1; // wrong lang -- bail out
                    299: 
1.59      paf       300:                        int size_diff=
                    301:                                (a_row->item.size-a_offset)-
                    302:                                (b_size-b_offset);
                    303:                        
                    304:                        if(size_diff==0) { // a has same size as b
                    305:                                if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
                    306:                                        a_row->item.size-a_offset)!=0)
                    307:                                        return result;
1.60      paf       308:                                pos+=a_row->item.size;
1.59      paf       309:                                a_row++; a_countdown--; a_offset=0;
                    310:                                b_break=true;
                    311:                        } else if (size_diff>0) { // a longer
                    312:                                if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
                    313:                                        b_size-b_offset)!=0)
                    314:                                        return result;
                    315:                                a_offset+=b_size-b_offset;
                    316:                                b_break=true;
                    317:                        } else { // b longer
                    318:                                if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
                    319:                                        a_row->item.size-a_offset)!=0)
                    320:                                        return result;
                    321:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       322:                                pos+=a_row->item.size;
1.59      paf       323:                                a_row++; a_countdown--; a_offset=0;
                    324:                        }
                    325:                } else {
1.60      paf       326:                        a_offset-=a_row->item.size; 
                    327:                        pos+=a_row->item.size;
                    328:                        a_row++; a_countdown--; 
1.27      paf       329:                }
                    330: 
1.83    ! parser    331:                if(a_break=a_row==a_end)
        !           332:                        break;
1.27      paf       333:                if(!a_countdown) {
                    334:                        a_chunk=a_row->link;
                    335:                        a_row=a_chunk->rows;
                    336:                        a_countdown=a_chunk->count;
1.9       paf       337:                }
                    338:        }
1.55      paf       339:        if(a_break==b_break) { // ended simultaneously
                    340:                partial=0; return 0;
                    341:        } else if(a_break) { // first bytes equal, but a ended before b
                    342:                partial=1; return -1;
                    343:        } else {
                    344:                partial=2; return +1;
                    345:        }
1.5       paf       346: }
1.46      paf       347: 
                    348: #ifndef NO_STRING_ORIGIN
                    349: const Origin& String::origin() const { 
                    350:        if(!fused_rows)
                    351:                THROW(0, 0, 
1.50      paf       352:                        0,
                    353:                        "String::origin() of empty string called");
1.46      paf       354:        
1.49      paf       355:        // determining origin by last appended piece
1.50      paf       356:        // because first one frequently constant. 
                    357:        // ex: ^load[/file] "document_root" + "/file"
1.80      paf       358:        // when last peice is constant, 
                    359:        // ex: parser_root_auto_path{dynamic} / auto.p{const}
                    360:        // using first piece
                    361:        Origin& last_origin=append_here[-1].item.origin;
                    362:        return last_origin.file ? last_origin : head.rows[0].item.origin;
1.46      paf       363: }
                    364: #endif
1.53      paf       365: 
1.69      paf       366: String& String::mid(size_t start, size_t finish) const {
1.53      paf       367:        start=max(0, start);
                    368:        finish=min(size(), finish);
1.60      paf       369:        if(start==finish)
                    370:                return *empty_string;
1.53      paf       371: 
                    372:        String& result=*NEW String(pool());
                    373: 
                    374:        size_t pos=0;
                    375:        const Chunk *chunk=&head; 
                    376:        do {
                    377:                const Chunk::Row *row=chunk->rows;
1.55      paf       378:                for(size_t i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53      paf       379:                        if(row==append_here)
                    380:                                goto break2;
                    381: 
1.60      paf       382:                        size_t item_finish=pos+row->item.size;
                    383:                        if(item_finish > start) { // started now or already?
                    384:                                bool started=result.size()==0; // started now?
                    385:                                bool finished=finish <= item_finish; // finished now?
1.53      paf       386:                                size_t offset=started?start-pos:0;
                    387:                                size_t size=finished?finish-pos:row->item.size;
                    388:                                result.APPEND(
                    389:                                        row->item.ptr+offset, size-offset, 
                    390:                                        row->item.lang,
                    391:                                        row->item.origin.file, row->item.origin.line);
                    392:                                if(finished)
                    393:                                        goto break2;
                    394:                        }
                    395:                }
                    396:                chunk=row->link;
                    397:        } while(chunk);
                    398: break2:
1.60      paf       399: //     SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
                    400:                //cstr(), start, finish, result.cstr());
1.53      paf       401:        return result;
1.54      paf       402: }
                    403: 
1.60      paf       404: int String::pos(const String& substr, 
                    405:                                size_t result, Untaint_lang lang) const {
1.58      paf       406:        for(; result<size(); result++) {
1.60      paf       407:                int partial; cmp(partial, substr, result, lang);
1.58      paf       408:                if(
                    409:                        partial==0 || // full match
                    410:                        partial==2) // 'substr' starts 'this'+'result'
                    411:                        return result;
                    412:        }
                    413:        
                    414:        return -1;
                    415: }
                    416: 
1.60      paf       417: int String::pos(const char *substr, size_t substr_size, 
                    418:                                size_t result, Untaint_lang lang) const {
1.57      paf       419:        for(; result<size(); result++) {
1.60      paf       420:                int partial; cmp(partial, substr, substr_size, result, lang);
1.55      paf       421:                if(
                    422:                        partial==0 || // full match
                    423:                        partial==2) // 'substr' starts 'this'+'result'
                    424:                        return result;
                    425:        }
                    426:        
                    427:        return -1;
1.60      paf       428: }
                    429: 
                    430: void String::split(Array& result, 
                    431:                                   size_t* pos_after_ref, 
                    432:                                   const char *delim, size_t delim_size, 
                    433:                                   Untaint_lang lang, int limit) const {
                    434:        if(delim_size) {
                    435:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    436:                int pos_before;
                    437:                // while we have 'delim'...
                    438:                for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       439:                        result+=&mid(pos_after, pos_before);
1.60      paf       440:                        pos_after=pos_before+delim_size;
                    441:                }
                    442:                // last piece
                    443:                if(pos_after<size() && limit) {
1.69      paf       444:                        result+=&mid(pos_after, size());
1.60      paf       445:                        pos_after=size();
                    446:                }
                    447:                if(pos_after_ref)
                    448:                        *pos_after_ref=pos_after;
                    449:        } else { // empty delim
                    450:                result+=this;
                    451:                if(pos_after_ref)
                    452:                        *pos_after_ref+=size();
                    453:        }
                    454: }
                    455: 
                    456: void String::split(Array& result, 
                    457:                                   size_t* pos_after_ref, 
                    458:                                   const String& delim, Untaint_lang lang, 
                    459:                                   int limit) const {
                    460:        if(delim.size()) {
                    461:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    462:                int pos_before;
                    463:                // while we have 'delim'...
                    464:                for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       465:                        result+=&mid(pos_after, pos_before);
1.60      paf       466:                        pos_after=pos_before+delim.size();
                    467:                }
                    468:                // last piece
                    469:                if(pos_after<size() && limit) {
1.69      paf       470:                        result+=&mid(pos_after, size());
1.60      paf       471:                        pos_after=size();
                    472:                }
                    473:                if(pos_after_ref)
                    474:                        *pos_after_ref=pos_after;
                    475:        } else { // empty delim
                    476:                result+=this;
                    477:                if(pos_after_ref)
                    478:                        *pos_after_ref+=size();
                    479:        }
1.61      paf       480: }
                    481: 
1.63      paf       482: static void regex_options(char *options, int *result){
                    483:     struct Regex_option {
                    484:                char key;
                    485:                int clear, set;
                    486:                int *result;
                    487:     } regex_option[]={
                    488:                {'i', 0, PCRE_CASELESS, result}, // a=A
1.79      paf       489:                {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63      paf       490:                {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    491:                {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    492:                {'g', 0, true, result+1}, // many rows
                    493:                {0},
                    494:     };
                    495:        result[0]=PCRE_EXTRA | PCRE_DOTALL;
                    496:        result[1]=0;
                    497: 
                    498:     if(options) 
                    499:                for(Regex_option *o=regex_option; o->key; o++) 
                    500:                        if(
                    501:                                strchr(options, o->key) || 
                    502:                                strchr(options, toupper(o->key))) {
                    503:                                *(o->result)&=~o->clear;
                    504:                                *(o->result)|=o->set;
                    505:                        }
                    506: }
                    507: 
1.77      paf       508: bool String::match(const unsigned char *pcre_tables,
                    509:                                   const String *aorigin,
1.62      paf       510:                                   const String& regexp, 
1.63      paf       511:                                   const String *options,
1.64      paf       512:                                   Table **table,
                    513:                                   Row_action row_action, void *info) const { 
                    514: 
1.73      paf       515:        if(!regexp.size())
                    516:                THROW(0, 0,
                    517:                        aorigin,
                    518:                        "regexp is empty");
1.68      paf       519:        const char *pattern=regexp.cstr(UL_AS_IS);
1.62      paf       520:        const char *errptr;
                    521:        int erroffset;
1.63      paf       522:     int option_bits[2];  regex_options(options?options->cstr():0, option_bits);
                    523:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       524:                &errptr, &erroffset,
1.74      paf       525:                pcre_tables);
1.62      paf       526: 
1.67      paf       527:        if(!code)
1.62      paf       528:                THROW(0, 0,
1.69      paf       529:                        &regexp.mid(erroffset, regexp.size()),
1.74      paf       530:                        "regular expression syntax error - %s", errptr);
1.62      paf       531:        
1.63      paf       532:        int info_substrings=pcre_info(code, 0, 0);
                    533:        if(info_substrings<0) {
                    534:                (*pcre_free)(code);
                    535:                THROW(0, 0,
1.73      paf       536:                        aorigin,
1.76      paf       537:                        "pcre_info error (%d)", 
1.73      paf       538:                                info_substrings);
1.63      paf       539:        }
                    540: 
                    541:        int startoffset=0;
1.68      paf       542:        const char *subject=cstr(UL_AS_IS);
1.62      paf       543:        int length=strlen(subject);
1.63      paf       544:        int ovecsize;
                    545:        int *ovector=(int *)malloc(sizeof(int)*
1.65      paf       546:                (ovecsize=(1/*match*/+info_substrings)*3));
1.62      paf       547: 
1.64      paf       548:        { // create table
                    549:                Array& columns=*NEW Array(pool());
                    550:                columns+=string_pre_match_name;
                    551:                columns+=string_match_name;
                    552:                columns+=string_post_match_name;
                    553:                for(int i=1; i<=info_substrings; i++) {
                    554:                        char *column=(char *)malloc(MAX_NUMBER);
                    555:                        snprintf(column, MAX_NUMBER, "%d", i);
                    556:                        columns+=NEW String(pool(), column); // .i column name
                    557:                }
                    558:                *table=NEW Table(pool(), aorigin, &columns);
1.62      paf       559:        }
1.63      paf       560: 
1.64      paf       561:        int exec_option_bits=0;
1.63      paf       562:        while(true) {
                    563:                int exec_substrings=pcre_exec(code, 0,
                    564:                        subject, length, startoffset,
1.64      paf       565:                        exec_option_bits, ovector, ovecsize);
1.63      paf       566:                
                    567:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
                    568:                        (*pcre_free)(code);
1.67      paf       569:                        (*row_action)(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       570:                        return option_bits[1]!=0; // global=true+table, not global=false
                    571:                }
                    572: 
                    573:                if(exec_substrings<0) {
                    574:                        (*pcre_free)(code);
                    575:                        THROW(0, 0,
                    576:                                aorigin,
1.76      paf       577:                                "regular expression execute error (%d)", 
1.63      paf       578:                                        exec_substrings);
                    579:                }
                    580: 
                    581:                Array& row=*NEW Array(pool());
1.81      paf       582:                row+=&mid(0, ovector[0]); // .prematch column value
1.69      paf       583:                row+=&mid(ovector[0], ovector[1]); // .match
1.81      paf       584:                row+=&mid(ovector[1], size()); // .postmatch
1.63      paf       585:                
                    586:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       587:                        // -1:-1 case handled peacefully by mid() itself
                    588:                        row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       589:                }
                    590:                
1.67      paf       591:                (*row_action)(**table, &row, startoffset, ovector[0], info);
1.63      paf       592: 
1.67      paf       593:                if(!option_bits[1] || !(startoffset=ovector[1])) { // not global | going to hang
1.63      paf       594:                        (*pcre_free)(code);
1.67      paf       595:                        (*row_action)(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       596:                        return true;
                    597:                }
                    598: 
                    599: /*
                    600:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       601:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       602: */
                    603:        }
1.82      parser    604: }
                    605: 
                    606: String& String::change_case(Pool& pool, const unsigned char *tables, 
                    607:                                                        Change_case_kind kind) const {
                    608:        String& result=*new(pool) String(pool);
                    609: 
                    610:        const unsigned char *a;
                    611:        const unsigned char *b;
                    612:        switch(kind) {
                    613:        case CC_UPPER:
                    614:                a=tables+lcc_offset;
                    615:                b=tables+fcc_offset;
                    616:                break;
                    617:        case CC_LOWER:
                    618:                a=tables+lcc_offset;
                    619:                b=0;
                    620:                break;
                    621:        default:
                    622:                PTHROW(0, 0, 
                    623:                        this, 
                    624:                        "unknown change case kind #%d", 
                    625:                                static_cast<int>(kind)); // never
                    626:                a=b=0; // calm, compiler
                    627:                break; // never
                    628:        }       
                    629: 
                    630:        const Chunk *chunk=&head; 
                    631:        do {
                    632:                const Chunk::Row *row=chunk->rows;
                    633:                for(size_t i=0; i<chunk->count; i++, row++) {
                    634:                        if(row==append_here)
                    635:                                goto break2;
                    636: 
                    637:                        char *new_cstr=(char *)pool.malloc(row->item.size);
                    638:                        char *dest=new_cstr;
                    639:                        const char *src=row->item.ptr; 
                    640:                        for(int size=row->item.size; size--; src++) {
                    641:                                unsigned char c=a[(unsigned char)*src];
                    642:                                if(b)
                    643:                                        c=b[c];
                    644: 
                    645:                                *dest++=(char)c;
                    646:                        }
                    647:                        
                    648:                        result.APPEND(new_cstr, row->item.size, 
                    649:                                row->item.lang,
                    650:                                row->item.origin.file, row->item.origin.line);
                    651:                }
                    652:                chunk=row->link;
                    653:        } while(chunk);
                    654: break2:
                    655: 
                    656:        return result;
1.61      paf       657: }

E-mail: