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

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

E-mail: