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

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

E-mail: