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

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

E-mail: