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

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

E-mail: