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

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

E-mail: