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

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.119     paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.46      paf         6: 
1.128   ! paf         7:        $Id: pa_string.C,v 1.125.2.1.2.2 2001/12/07 13:10:18 paf Exp $
1.4       paf         8: */
                      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.101     parser     23: #include "pa_dictionary.h"
1.60      paf        24: 
1.75      paf        25: String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
1.120     paf        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.123     paf        30:        head_link=0;
1.28      paf        31:        link_row=&head.rows[head.count];
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: 
1.94      parser     40: String::String(const String& src) :    
1.120     paf        41:        Pooled(src.pool()) {
1.8       paf        42:        head.count=CR_PREALLOCATED_COUNT;
                     43:        
1.116     paf        44:        uint src_used_rows=src.used_rows();
1.8       paf        45:        if(src_used_rows<=head.count) {
1.55      paf        46:                // all new rows fit size_to preallocated area
1.98      parser     47:                last_chunk=&head;
1.116     paf        48:                uint curr_chunk_rows=head.count;
1.8       paf        49:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
                     50:                append_here=&head.rows[src_used_rows];
                     51:                link_row=&head.rows[curr_chunk_rows];
                     52:        } else {
                     53:                // warning: 
1.10      paf        54:                //   heavily relies on the fact 
                     55:                //   "preallocated area is the same for all strings"
1.8       paf        56:                //
                     57:                // info:
                     58:                //   allocating only enough mem to fit src string rows
                     59:                //   next append would allocate a new chunk
                     60:                //
1.55      paf        61:                // new rows don't fit size_to preallocated area: splitting size_to two chunks
1.8       paf        62:                // preallocated chunk src to constructing head
                     63:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
1.55      paf        64:                // remaining rows size_to new_chunk
1.116     paf        65:                uint curr_chunk_rows=src_used_rows-head.count;
1.98      parser     66:                last_chunk=static_cast<Chunk *>(
1.116     paf        67:                        malloc(sizeof(uint)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *), 9));
1.98      parser     68:                last_chunk->count=curr_chunk_rows;
1.123     paf        69:                head_link=last_chunk;
1.98      parser     70:                append_here=link_row=&last_chunk->rows[last_chunk->count];
1.8       paf        71: 
1.123     paf        72:                Chunk *old_chunk=src.head_link; 
1.98      parser     73:                Chunk::Row *new_rows=last_chunk->rows;
1.116     paf        74:                uint rows_left_to_copy=last_chunk->count;
1.8       paf        75:                while(true) {
1.116     paf        76:                        uint old_count=old_chunk->count;
1.8       paf        77:                        Chunk *next_chunk=old_chunk->rows[old_count].link;
                     78:                        if(next_chunk) {
                     79:                                // not last source chunk
                     80:                                // taking it all
                     81:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
                     82:                                new_rows+=old_count;
                     83:                                rows_left_to_copy-=old_count;
                     84: 
                     85:                                old_chunk=next_chunk;
                     86:                        } else {
                     87:                                // the last source chunk
                     88:                                // taking only those rows of chunk that _left_to_copy
                     89:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
                     90:                                break;
                     91:                        }
                     92:                }
1.5       paf        93:        }
1.8       paf        94:        link_row->link=0;
1.115     paf        95:        src_used_rows;
1.120     paf        96: }
                     97: 
                     98: size_t  String::size() const {
                     99:        size_t result=0;
1.123     paf       100:        STRING_FOREACH_ROW(
1.120     paf       101:                        result+=row->item.size;
1.123     paf       102:        );
1.120     paf       103: break2:
                    104:        return result;
1.94      parser    105: }
                    106: 
1.115     paf       107: /// @todo not very optimal
                    108: uint String::used_rows() const {
                    109:        uint result=0;
1.123     paf       110:        STRING_FOREACH_ROW(
                    111:                result++;
                    112:        );
1.115     paf       113: break2:
                    114:        return result;
                    115: }
1.94      parser    116: void String::expand() {
1.116     paf       117:        uint new_chunk_count=last_chunk->count+CR_GROW_COUNT;
1.122     paf       118:        if(new_chunk_count>MAX_USHORT)
                    119:                new_chunk_count=MAX_USHORT;
                    120: 
1.94      parser    121:        last_chunk=static_cast<Chunk *>(
1.116     paf       122:                malloc(sizeof(uint)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *), 10));
1.94      parser    123:        last_chunk->count=new_chunk_count;
                    124:        link_row->link=last_chunk;
                    125:        append_here=last_chunk->rows;
                    126:        link_row=&last_chunk->rows[last_chunk->count];
                    127:        link_row->link=0;
1.5       paf       128: }
1.28      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;
1.122     paf       137: 
                    138:        while(size>MAX_USHORT) {
                    139:                if(chunk_is_full())
                    140:                        expand();
                    141: 
                    142:                append_here->item.ptr=src;
                    143:                append_here->item.size=MAX_USHORT;
                    144:                append_here->item.lang=lang;
                    145: #ifndef NO_STRING_ORIGIN
                    146:                append_here->item.origin.file=file;
                    147:                append_here->item.origin.line=line;
                    148: #endif
                    149:                append_here++;
                    150: 
                    151:                src+=MAX_USHORT;
                    152:                size-=MAX_USHORT;
                    153:        }
1.9       paf       154: 
1.1       paf       155:        if(chunk_is_full())
                    156:                expand();
                    157: 
                    158:        append_here->item.ptr=src;
1.121     paf       159:        append_here->item.size=size;
1.52      paf       160:        append_here->item.lang=lang;
1.13      paf       161: #ifndef NO_STRING_ORIGIN
1.14      paf       162:        append_here->item.origin.file=file;
                    163:        append_here->item.origin.line=line;
1.13      paf       164: #endif
1.115     paf       165:        append_here++;
1.1       paf       166: 
                    167:        return *this;
1.97      parser    168: }
                    169: 
                    170: char String::first_char() const {
1.120     paf       171:        if(!used_rows())
1.112     parser    172:                throw Exception(0, 0,
1.97      parser    173:                        this,
                    174:                        "getting first char of empty string");
                    175: 
                    176:        return *head.rows[0].item.ptr;
1.1       paf       177: }
                    178: 
1.16      paf       179: uint String::hash_code() const {
1.7       paf       180:        uint result=0;
1.123     paf       181:        STRING_FOREACH_ROW(
1.6       paf       182:                        result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.123     paf       183:        );
1.5       paf       184: break2:
                    185:        return result;
                    186: }
                    187: 
1.60      paf       188: /// @todo move 'lang' skipping to pos
                    189: int String::cmp(int& partial, const String& src, 
                    190:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       191:        partial=-1;
1.125     paf       192:        size_t a_size=size();
                    193:        this_offset=min(this_offset, a_size-1);
1.55      paf       194: 
1.16      paf       195:        const Chunk *a_chunk=&head;
                    196:        const Chunk *b_chunk=&src.head;
                    197:        const Chunk::Row *a_row=a_chunk->rows;
                    198:        const Chunk::Row *b_row=b_chunk->rows;
1.55      paf       199:        size_t a_offset=this_offset;
                    200:        size_t b_offset=0;
1.9       paf       201:        Chunk::Row *a_end=append_here;
                    202:        Chunk::Row *b_end=src.append_here;
1.116     paf       203:        uint a_countdown=a_chunk->count;
                    204:        uint b_countdown=b_chunk->count;
                    205:        int result;
1.60      paf       206:        size_t pos=0; 
1.33      paf       207: 
1.125     paf       208:        bool a_break=a_size==0;
1.91      parser    209:        bool b_break=src.size()==0;
1.83      parser    210:        if(!(a_break || b_break)) while(true) {
1.55      paf       211:                if(pos+a_row->item.size > this_offset) {
1.71      paf       212:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
1.60      paf       213:                                return -1; // wrong lang -- bail out
                    214: 
1.55      paf       215:                        int size_diff=
                    216:                                (a_row->item.size-a_offset)-
                    217:                                (b_row->item.size-b_offset);
                    218:                        
                    219:                        if(size_diff==0) { // a has same size as b
1.60      paf       220:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    221:                                        a_row->item.size-a_offset);
1.55      paf       222:                                if(result)
                    223:                                        return result;
1.60      paf       224:                                pos+=a_row->item.size;
1.55      paf       225:                                a_row++; a_countdown--; a_offset=0;
                    226:                                b_row++; b_countdown--; b_offset=0;
                    227:                        } else if (size_diff>0) { // a longer
1.60      paf       228:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    229:                                        b_row->item.size-b_offset);
1.55      paf       230:                                if(result)
                    231:                                        return result;
                    232:                                a_offset+=b_row->item.size-b_offset;
                    233:                                b_row++; b_countdown--; b_offset=0;
                    234:                        } else { // b longer
1.60      paf       235:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    236:                                        a_row->item.size-a_offset);
1.55      paf       237:                                if(result)
                    238:                                        return result;
                    239:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       240:                                pos+=a_row->item.size;
1.55      paf       241:                                a_row++; a_countdown--; a_offset=0;
                    242:                        }
1.83      parser    243:                        if(b_break=b_row==b_end) {
                    244:                                a_break=a_row==a_end;
                    245:                                break;                  
                    246:                        }
1.55      paf       247:                        if(!b_countdown) {
                    248:                                b_chunk=b_row->link;
                    249:                                b_row=b_chunk->rows;
                    250:                                b_countdown=b_chunk->count;
                    251:                        }
                    252:                } else {
1.60      paf       253:                        a_offset-=a_row->item.size;
                    254:                        pos+=a_row->item.size;
                    255:                        a_row++; a_countdown--; 
1.9       paf       256:                }
                    257: 
1.83      parser    258:                if(a_break=a_row==a_end) {
                    259:                        b_break=b_row==b_end;
                    260:                        break;
                    261:                }
1.11      paf       262:                if(!a_countdown) {
1.9       paf       263:                        a_chunk=a_row->link;
                    264:                        a_row=a_chunk->rows;
1.11      paf       265:                        a_countdown=a_chunk->count;
1.9       paf       266:                }
1.27      paf       267:        }
1.55      paf       268:        if(a_break==b_break) { // ended simultaneously
                    269:                partial=0; return 0;
                    270:        } else if(a_break) { // first bytes equal, but a ended before b
                    271:                partial=1; return -1;
                    272:        } else {
                    273:                partial=2; return +1;
                    274:        }
1.27      paf       275: }
                    276: 
1.60      paf       277: /// @todo move 'lang' skipping to pos
1.59      paf       278: int String::cmp(int& partial, const char* b_ptr, size_t src_size, 
1.60      paf       279:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       280:        partial=-1;
1.125     paf       281:        size_t a_size=size();
1.50      paf       282:        size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.125     paf       283:        this_offset=min(this_offset, a_size-1);
1.27      paf       284: 
                    285:        const Chunk *a_chunk=&head;
                    286:        const Chunk::Row *a_row=a_chunk->rows;
1.59      paf       287:        size_t a_offset=this_offset;
1.55      paf       288:        size_t b_offset=0;
1.27      paf       289:        Chunk::Row *a_end=append_here;
1.116     paf       290:        uint a_countdown=a_chunk->count;
1.60      paf       291:        size_t pos=0;
1.52      paf       292: 
1.125     paf       293:        bool a_break=a_size==0;
1.83      parser    294:        bool b_break=b_size==0;
                    295:        if(!(a_break || b_break)) while(true) {
1.59      paf       296:                if(pos+a_row->item.size > this_offset) {
1.71      paf       297:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) 
1.60      paf       298:                                return -1; // wrong lang -- bail out
                    299: 
1.59      paf       300:                        int size_diff=
                    301:                                (a_row->item.size-a_offset)-
                    302:                                (b_size-b_offset);
                    303:                        
                    304:                        if(size_diff==0) { // a has same size as b
1.116     paf       305:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       306:                                        a_row->item.size-a_offset)!=0)
                    307:                                        return result;
1.60      paf       308:                                pos+=a_row->item.size;
1.59      paf       309:                                a_row++; a_countdown--; a_offset=0;
                    310:                                b_break=true;
                    311:                        } else if (size_diff>0) { // a longer
1.116     paf       312:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       313:                                        b_size-b_offset)!=0)
                    314:                                        return result;
                    315:                                a_offset+=b_size-b_offset;
                    316:                                b_break=true;
                    317:                        } else { // b longer
1.116     paf       318:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       319:                                        a_row->item.size-a_offset)!=0)
                    320:                                        return result;
                    321:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       322:                                pos+=a_row->item.size;
1.59      paf       323:                                a_row++; a_countdown--; a_offset=0;
                    324:                        }
                    325:                } else {
1.60      paf       326:                        a_offset-=a_row->item.size; 
                    327:                        pos+=a_row->item.size;
                    328:                        a_row++; a_countdown--; 
1.27      paf       329:                }
                    330: 
1.86      parser    331:                a_break=a_row==a_end;
                    332:                if(a_break || b_break)
1.83      parser    333:                        break;
1.27      paf       334:                if(!a_countdown) {
                    335:                        a_chunk=a_row->link;
                    336:                        a_row=a_chunk->rows;
                    337:                        a_countdown=a_chunk->count;
1.9       paf       338:                }
                    339:        }
1.55      paf       340:        if(a_break==b_break) { // ended simultaneously
                    341:                partial=0; return 0;
                    342:        } else if(a_break) { // first bytes equal, but a ended before b
                    343:                partial=1; return -1;
                    344:        } else {
                    345:                partial=2; return +1;
                    346:        }
1.5       paf       347: }
1.46      paf       348: 
                    349: #ifndef NO_STRING_ORIGIN
                    350: const Origin& String::origin() const { 
1.120     paf       351:        if(!used_rows()) {
1.96      parser    352:                static const Origin empty_origin={"empty string"};
                    353:                return empty_origin;
                    354:        }
1.46      paf       355:        
1.49      paf       356:        // determining origin by last appended piece
1.50      paf       357:        // because first one frequently constant. 
                    358:        // ex: ^load[/file] "document_root" + "/file"
1.80      paf       359:        // when last peice is constant, 
                    360:        // ex: parser_root_auto_path{dynamic} / auto.p{const}
                    361:        // using first piece
                    362:        Origin& last_origin=append_here[-1].item.origin;
                    363:        return last_origin.file ? last_origin : head.rows[0].item.origin;
1.46      paf       364: }
                    365: #endif
1.53      paf       366: 
1.69      paf       367: String& String::mid(size_t start, size_t finish) const {
1.107     parser    368:        String& result=*NEW String(pool());
                    369: 
1.53      paf       370:        start=max(0, start);
1.111     parser    371:        finish=min(size(), finish);
1.60      paf       372:        if(start==finish)
1.107     parser    373:                return result;
1.53      paf       374: 
                    375:        size_t pos=0;
1.123     paf       376:        STRING_FOREACH_ROW(
                    377:                size_t item_finish=pos+row->item.size;
                    378:                if(item_finish > start) { // started now or already?
                    379:                        bool started=result.size()==0; // started now?
                    380:                        bool finished=finish <= item_finish; // finished now?
                    381:                        size_t offset=started?start-pos:0;
                    382:                        size_t size=finished?finish-pos:row->item.size;
                    383:                        result.APPEND(
                    384:                                row->item.ptr+offset, size-offset, 
                    385:                                row->item.lang,
                    386:                                row->item.origin.file, row->item.origin.line);
                    387:                        if(finished)
1.53      paf       388:                                goto break2;
                    389:                }
1.123     paf       390:                pos+=row->item.size;
                    391:        );
1.53      paf       392: break2:
1.60      paf       393: //     SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
                    394:                //cstr(), start, finish, result.cstr());
1.53      paf       395:        return result;
1.54      paf       396: }
                    397: 
1.60      paf       398: int String::pos(const String& substr, 
1.116     paf       399:                                int result, Untaint_lang lang) const {
1.125     paf       400:        size_t self_size=size();
                    401:        for(; result<self_size; result++) {
1.60      paf       402:                int partial; cmp(partial, substr, result, lang);
1.58      paf       403:                if(
                    404:                        partial==0 || // full match
                    405:                        partial==2) // 'substr' starts 'this'+'result'
                    406:                        return result;
                    407:        }
                    408:        
                    409:        return -1;
                    410: }
                    411: 
1.60      paf       412: int String::pos(const char *substr, size_t substr_size, 
1.116     paf       413:                                int result, Untaint_lang lang) const {
1.125     paf       414:        size_t self_size=size();
                    415:        for(; result<self_size; result++) {
1.60      paf       416:                int partial; cmp(partial, substr, substr_size, result, lang);
1.55      paf       417:                if(
                    418:                        partial==0 || // full match
                    419:                        partial==2) // 'substr' starts 'this'+'result'
                    420:                        return result;
                    421:        }
                    422:        
                    423:        return -1;
1.60      paf       424: }
                    425: 
                    426: void String::split(Array& result, 
                    427:                                   size_t* pos_after_ref, 
                    428:                                   const char *delim, size_t delim_size, 
                    429:                                   Untaint_lang lang, int limit) const {
1.125     paf       430:        size_t self_size=size();
1.60      paf       431:        if(delim_size) {
                    432:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    433:                int pos_before;
                    434:                // while we have 'delim'...
                    435:                for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       436:                        result+=&mid(pos_after, pos_before);
1.60      paf       437:                        pos_after=pos_before+delim_size;
                    438:                }
                    439:                // last piece
1.124     paf       440:                if(pos_after<self_size && limit) {
                    441:                        result+=&mid(pos_after, self_size);
                    442:                        pos_after=self_size;
1.60      paf       443:                }
                    444:                if(pos_after_ref)
                    445:                        *pos_after_ref=pos_after;
                    446:        } else { // empty delim
                    447:                result+=this;
                    448:                if(pos_after_ref)
1.124     paf       449:                        *pos_after_ref+=self_size;
1.60      paf       450:        }
                    451: }
                    452: 
                    453: void String::split(Array& result, 
                    454:                                   size_t* pos_after_ref, 
                    455:                                   const String& delim, Untaint_lang lang, 
                    456:                                   int limit) const {
                    457:        if(delim.size()) {
                    458:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    459:                int pos_before;
                    460:                // while we have 'delim'...
                    461:                for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       462:                        result+=&mid(pos_after, pos_before);
1.60      paf       463:                        pos_after=pos_before+delim.size();
                    464:                }
                    465:                // last piece
                    466:                if(pos_after<size() && limit) {
1.69      paf       467:                        result+=&mid(pos_after, size());
1.60      paf       468:                        pos_after=size();
                    469:                }
                    470:                if(pos_after_ref)
                    471:                        *pos_after_ref=pos_after;
                    472:        } else { // empty delim
                    473:                result+=this;
                    474:                if(pos_after_ref)
                    475:                        *pos_after_ref+=size();
                    476:        }
1.61      paf       477: }
                    478: 
1.63      paf       479: static void regex_options(char *options, int *result){
                    480:     struct Regex_option {
                    481:                char key;
                    482:                int clear, set;
                    483:                int *result;
                    484:     } regex_option[]={
                    485:                {'i', 0, PCRE_CASELESS, result}, // a=A
1.79      paf       486:                {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63      paf       487:                {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    488:                {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    489:                {'g', 0, true, result+1}, // many rows
                    490:                {0},
                    491:     };
                    492:        result[0]=PCRE_EXTRA | PCRE_DOTALL;
                    493:        result[1]=0;
                    494: 
                    495:     if(options) 
                    496:                for(Regex_option *o=regex_option; o->key; o++) 
                    497:                        if(
                    498:                                strchr(options, o->key) || 
                    499:                                strchr(options, toupper(o->key))) {
                    500:                                *(o->result)&=~o->clear;
                    501:                                *(o->result)|=o->set;
                    502:                        }
                    503: }
                    504: 
1.88      parser    505: /// @todo maybe need speedup: some option to remove pre/match/post string generation
1.77      paf       506: bool String::match(const unsigned char *pcre_tables,
                    507:                                   const String *aorigin,
1.62      paf       508:                                   const String& regexp, 
1.63      paf       509:                                   const String *options,
1.64      paf       510:                                   Table **table,
1.95      parser    511:                                   Row_action row_action, void *info,
                    512:                                   bool *was_global) const { 
1.64      paf       513: 
1.73      paf       514:        if(!regexp.size())
1.112     parser    515:                throw Exception(0, 0,
1.73      paf       516:                        aorigin,
                    517:                        "regexp is empty");
1.118     paf       518:        const char *pattern=regexp.cstr();
1.62      paf       519:        const char *errptr;
                    520:        int erroffset;
1.63      paf       521:     int option_bits[2];  regex_options(options?options->cstr():0, option_bits);
1.95      parser    522:        if(was_global)
                    523:                *was_global=option_bits[1]!=0;
1.63      paf       524:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       525:                &errptr, &erroffset,
1.74      paf       526:                pcre_tables);
1.62      paf       527: 
1.67      paf       528:        if(!code)
1.112     parser    529:                throw Exception(0, 0,
1.69      paf       530:                        &regexp.mid(erroffset, regexp.size()),
1.74      paf       531:                        "regular expression syntax error - %s", errptr);
1.62      paf       532:        
1.63      paf       533:        int info_substrings=pcre_info(code, 0, 0);
                    534:        if(info_substrings<0) {
1.100     parser    535:                pcre_free(code);
1.112     parser    536:                throw Exception(0, 0,
1.73      paf       537:                        aorigin,
1.76      paf       538:                        "pcre_info error (%d)", 
1.73      paf       539:                                info_substrings);
1.63      paf       540:        }
                    541: 
                    542:        int startoffset=0;
1.118     paf       543:        const char *subject=cstr();
1.62      paf       544:        int length=strlen(subject);
1.63      paf       545:        int ovecsize;
                    546:        int *ovector=(int *)malloc(sizeof(int)*
1.114     paf       547:                (ovecsize=(1/*match*/+info_substrings)*3), 11);
1.62      paf       548: 
1.64      paf       549:        { // create table
                    550:                Array& columns=*NEW Array(pool());
                    551:                columns+=string_pre_match_name;
                    552:                columns+=string_match_name;
                    553:                columns+=string_post_match_name;
                    554:                for(int i=1; i<=info_substrings; i++) {
                    555:                        char *column=(char *)malloc(MAX_NUMBER);
                    556:                        snprintf(column, MAX_NUMBER, "%d", i);
                    557:                        columns+=NEW String(pool(), column); // .i column name
                    558:                }
                    559:                *table=NEW Table(pool(), aorigin, &columns);
1.62      paf       560:        }
1.63      paf       561: 
1.64      paf       562:        int exec_option_bits=0;
1.63      paf       563:        while(true) {
                    564:                int exec_substrings=pcre_exec(code, 0,
                    565:                        subject, length, startoffset,
1.64      paf       566:                        exec_option_bits, ovector, ovecsize);
1.63      paf       567:                
                    568:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    569:                        pcre_free(code);
                    570:                        row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       571:                        return option_bits[1]!=0; // global=true+table, not global=false
                    572:                }
                    573: 
                    574:                if(exec_substrings<0) {
1.100     parser    575:                        pcre_free(code);
1.112     parser    576:                        throw Exception(0, 0,
1.63      paf       577:                                aorigin,
1.76      paf       578:                                "regular expression execute error (%d)", 
1.63      paf       579:                                        exec_substrings);
                    580:                }
                    581: 
                    582:                Array& row=*NEW Array(pool());
1.81      paf       583:                row+=&mid(0, ovector[0]); // .prematch column value
1.69      paf       584:                row+=&mid(ovector[0], ovector[1]); // .match
1.81      paf       585:                row+=&mid(ovector[1], size()); // .postmatch
1.63      paf       586:                
                    587:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       588:                        // -1:-1 case handled peacefully by mid() itself
                    589:                        row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       590:                }
                    591:                
1.100     parser    592:                row_action(**table, &row, startoffset, ovector[0], info);
1.63      paf       593: 
1.100     parser    594:                if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang
                    595:                        pcre_free(code);
                    596:                        row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       597:                        return true;
                    598:                }
1.100     parser    599:                startoffset=ovector[1];
1.63      paf       600: 
                    601: /*
                    602:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       603:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       604: */
                    605:        }
1.82      parser    606: }
                    607: 
                    608: String& String::change_case(Pool& pool, const unsigned char *tables, 
                    609:                                                        Change_case_kind kind) const {
                    610:        String& result=*new(pool) String(pool);
                    611: 
                    612:        const unsigned char *a;
                    613:        const unsigned char *b;
                    614:        switch(kind) {
                    615:        case CC_UPPER:
                    616:                a=tables+lcc_offset;
                    617:                b=tables+fcc_offset;
                    618:                break;
                    619:        case CC_LOWER:
                    620:                a=tables+lcc_offset;
                    621:                b=0;
                    622:                break;
                    623:        default:
1.112     parser    624:                throw Exception(0, 0, 
1.82      parser    625:                        this, 
                    626:                        "unknown change case kind #%d", 
                    627:                                static_cast<int>(kind)); // never
                    628:                a=b=0; // calm, compiler
                    629:                break; // never
                    630:        }       
                    631: 
                    632:        const Chunk *chunk=&head; 
                    633:        do {
                    634:                const Chunk::Row *row=chunk->rows;
1.116     paf       635:                for(uint i=0; i<chunk->count; i++, row++) {
1.82      parser    636:                        if(row==append_here)
                    637:                                goto break2;
                    638: 
1.114     paf       639:                        char *new_cstr=(char *)pool.malloc(row->item.size, 12);
1.82      parser    640:                        char *dest=new_cstr;
                    641:                        const char *src=row->item.ptr; 
                    642:                        for(int size=row->item.size; size--; src++) {
                    643:                                unsigned char c=a[(unsigned char)*src];
                    644:                                if(b)
                    645:                                        c=b[c];
                    646: 
                    647:                                *dest++=(char)c;
                    648:                        }
                    649:                        
                    650:                        result.APPEND(new_cstr, row->item.size, 
1.123     paf       651:                                row->item.lang,
1.82      parser    652:                                row->item.origin.file, row->item.origin.line);
                    653:                }
                    654:                chunk=row->link;
                    655:        } while(chunk);
                    656: break2:
1.89      parser    657: 
1.101     parser    658:        return result;
                    659: }
                    660: 
1.108     parser    661: void String::join_chain(Pool& pool, 
1.116     paf       662:                                           uint& ai, const Chunk*& achunk, const Chunk::Row*& arow,
1.123     paf       663:                                           uchar& joined_lang, const char *& joined_ptr, size_t& joined_size) const {
                    664:        joined_lang=arow->item.lang;
1.108     parser    665:        
                    666:        // calc size
                    667:        joined_size=0;
                    668:        {
1.116     paf       669:                uint start_i=ai;
1.108     parser    670:                const Chunk::Row *start_row=arow;
                    671:                const Chunk *chunk=achunk;
                    672:                do {
                    673:                        const Chunk::Row *row=start_row;
1.116     paf       674:                        for(uint i=start_i; i<chunk->count; i++, row++) {
1.108     parser    675:                                if(row==append_here)
                    676:                                        goto break21;
                    677:                                
                    678:                                if(row->item.lang==joined_lang)
                    679:                                        joined_size+=row->item.size;
                    680:                                else
                    681:                                        break;
                    682:                        }
                    683:                        if(chunk=row->link) {
                    684:                                start_i=0;
                    685:                                start_row=chunk->rows;
                    686:                        } else
                    687:                                break;
                    688:                } while(true);
                    689: break21:;
                    690:        }
                    691: 
                    692:        // if one row, return simply itself
                    693:        if(joined_size==arow->item.size) {
                    694:                joined_ptr=arow->item.ptr;
                    695:                ai++; arow++;
                    696:                if(ai==achunk->count)
                    697:                        achunk=arow->link;              
                    698:        } else {
                    699:                // join adjacent rows
1.114     paf       700:                char *ptr=(char *)pool.malloc(joined_size,13);
1.108     parser    701:                joined_ptr=ptr;
1.116     paf       702:                uint start_i=ai;
1.108     parser    703:                const Chunk::Row *start_row=arow;
                    704:                const Chunk *chunk=achunk;
1.116     paf       705:                uint i;
1.108     parser    706:                const Chunk::Row *row;
                    707:                do {
                    708:                        row=start_row;
                    709:                        for(i=start_i; i<chunk->count; i++, row++) {
                    710:                                if(row==append_here)
                    711:                                        goto break22;
                    712:                                
                    713:                                if(row->item.lang==joined_lang) {
                    714:                                        memcpy(ptr, row->item.ptr, row->item.size);
                    715:                                        ptr+=row->item.size;
                    716:                                } else
                    717:                                        break;
                    718:                        }
                    719:                        if(chunk=row->link) {
                    720:                                start_i=0;
                    721:                                start_row=chunk->rows;
                    722:                        } else
                    723:                                break;
                    724:                } while(true);
                    725: break22:;
                    726:                
                    727:                // return joined rows
                    728:                ai=i;
                    729:                arow=row;
                    730:                achunk=chunk;
                    731:        }
                    732: }
                    733: 
                    734: String& String::reconstruct(Pool& pool) const {
                    735:        //_asm int 3;
                    736:        String& result=*new(pool) String(pool);
                    737:        const Chunk *chunk=&head; 
                    738:        do {
                    739:                const Chunk::Row *row=chunk->rows;
1.116     paf       740:                for(uint i=0; i<chunk->count; ) {
1.108     parser    741:                        if(row==append_here)
                    742:                                goto break2;
                    743: 
1.123     paf       744:                        uchar joined_lang;
1.109     parser    745:                        const char *joined_ptr;
1.108     parser    746:                        size_t joined_size;
                    747:                        join_chain(pool, i, chunk, row,
                    748:                                joined_lang, joined_ptr, joined_size);
                    749: 
                    750:                        result.APPEND(joined_ptr, joined_size, 
                    751:                                joined_lang,
                    752:                                row->item.origin.file, row->item.origin.line);
                    753:                        if(!chunk)
                    754:                                goto break2;
                    755:                }
                    756:        } while(true);
                    757: break2:
                    758: 
                    759:        return result;
                    760: };
                    761: 
                    762: String& String::replace_in_reconstructed(Pool& pool, Dictionary& dict) const {
1.106     parser    763:        //_asm int 3;
1.101     parser    764:        String& result=*new(pool) String(pool);
1.123     paf       765:        STRING_FOREACH_ROW(
                    766:                const char *src=row->item.ptr; 
                    767:                size_t src_size=row->item.size;
                    768:                char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);
                    769:                char *dest=new_cstr;
                    770:                while(src_size) {
                    771:                        // there is a row where first column starts 'src'
                    772:                        if(Table::Item *item=dict.first_that_starts(src, src_size)) {
                    773:                                // get a=>b values
                    774:                                const String& a=*static_cast<Array *>(item)->get_string(0);
                    775:                                const String& b=*static_cast<Array *>(item)->get_string(1);
                    776:                                // skip 'a' in 'src' && reduce work size
                    777:                                src+=a.size();  src_size-=a.size();
                    778:                                // write 'b' to 'dest' && skip 'b' in 'dest'
                    779:                                b.store_to(dest);  dest+=b.size();
                    780:                        } else {
                    781:                                // write a char to b && reduce work size
                    782:                                *dest++=*src++;  src_size--;
1.101     parser    783:                        }
                    784:                }
                    785: 
1.123     paf       786:                result.APPEND(new_cstr, dest-new_cstr, 
                    787:                        row->item.lang,
                    788:                        row->item.origin.file, row->item.origin.line);
                    789:        );
1.101     parser    790: break2:
1.89      parser    791:        return result;
1.108     parser    792: }
                    793: 
                    794: String& String::replace(Pool& pool, Dictionary& dict) const {
                    795:        return reconstruct(pool).replace_in_reconstructed(pool, dict);
1.89      parser    796: }
                    797: 
1.90      parser    798: double String::as_double() const { 
1.89      parser    799:        double result;
1.114     paf       800:        const char *cstr;
                    801:        char buf[MAX_NUMBER];
1.115     paf       802:        if(head.rows+1==append_here) {
1.114     paf       803:                int size=min(head.rows[0].item.size, MAX_NUMBER-1);
                    804:                memcpy(buf, head.rows[0].item.ptr, size);
                    805:                buf[size]=0;
                    806:                cstr=buf;
                    807:        } else
                    808:                cstr=this->cstr();
1.102     parser    809:        char *error_pos;
1.89      parser    810:        // 0xABC
1.99      parser    811:        if(cstr[0]=='0')
                    812:                if(cstr[1]=='x' || cstr[1]=='X')
                    813:                        result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
                    814:                else
1.102     parser    815:                        result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89      parser    816:        else
1.99      parser    817:                result=(double)strtod(cstr, &error_pos);
1.89      parser    818: 
1.103     parser    819:        if(*error_pos/*not EOS*/)
1.112     parser    820:                throw Exception(0, 0,
1.89      parser    821:                        this,
                    822:                        "invalid number (double)");
                    823: 
                    824:        return result;
                    825: }
1.90      parser    826: int String::as_int() const { 
1.89      parser    827:        int result;
1.114     paf       828:        const char *cstr;
                    829:        char buf[MAX_NUMBER];
1.115     paf       830:        if(head.rows+1==append_here) {
1.114     paf       831:                int size=min(head.rows[0].item.size, MAX_NUMBER-1);
                    832:                memcpy(buf, head.rows[0].item.ptr, size);
                    833:                buf[size]=0;
                    834:                cstr=buf;
                    835:        } else
                    836:                cstr=this->cstr();
1.102     parser    837:        char *error_pos;
1.89      parser    838:        // 0xABC
1.99      parser    839:        if(cstr[0]=='0')
                    840:                if(cstr[1]=='x' || cstr[1]=='X')
                    841:                        result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
                    842:                else
1.102     parser    843:                        result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89      parser    844:        else
                    845:                result=(int)strtol(cstr, &error_pos, 0);
                    846: 
1.103     parser    847:        if(*error_pos/*not EOS*/)
1.112     parser    848:                throw Exception(0, 0,
1.89      parser    849:                        this,
                    850:                        "invalid number (int)");
1.82      parser    851: 
                    852:        return result;
1.61      paf       853: }
1.113     parser    854: 
1.128   ! paf       855: inline void ushort2uchars(ushort word, uchar& byte1, uchar& byte2) {
        !           856:        byte1=word&0xFF;
        !           857:        byte2=word>>8;
        !           858: }
        !           859: inline ushort uchars2ushort(uchar byte1, uchar byte2) {
        !           860:        return (byte2<<8) | byte1;
        !           861: }
1.113     parser    862: /* @todo maybe network order worth spending some effort?
                    863:        don't bothering myself with network byte order,
                    864:        am not planning to be able to move resulting file across platforms
                    865:        for now
                    866: */
                    867: void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
                    868:        buf_size=
                    869:                prolog_size
1.126     paf       870:                +used_rows()*(sizeof(uchar)+sizeof(ushort))
1.113     parser    871:                +size();
1.114     paf       872:        buf=malloc(buf_size,15);
1.113     parser    873:        char *cur=(char *)buf+prolog_size;
                    874: 
1.123     paf       875:        STRING_FOREACH_ROW(
                    876:                // lang
1.126     paf       877:                memcpy(cur, &row->item.lang, sizeof(uchar));
                    878:                cur+=sizeof(uchar);
1.123     paf       879:                // size
1.128   ! paf       880:                uchar byte1; uchar byte2;
        !           881:                ushort2uchars(row->item.size, byte1, byte2);
        !           882:                memcpy(cur, &byte1, sizeof(uchar)); cur+=sizeof(uchar);
        !           883:                memcpy(cur, &byte2, sizeof(uchar)); cur+=sizeof(uchar);
1.123     paf       884:                // bytes
                    885:                memcpy(cur, row->item.ptr, row->item.size);
                    886:                cur+=row->item.size;
                    887:        );
1.113     parser    888: break2:
                    889:        ;
                    890: }
                    891: void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
1.126     paf       892:        char *cur=(char *)buf+prolog_size;
1.113     parser    893:        buf_size-=prolog_size;
                    894: 
                    895:        while(buf_size) {
1.128   ! paf       896:                uchar lang=*(uchar *)(cur);
        !           897:                
        !           898:                ushort size=uchars2ushort(
        !           899:                        *(uchar*)(cur+sizeof(uchar)*1),
        !           900:                        *(uchar*)(cur+sizeof(uchar)*2)
        !           901:                );
        !           902: 
        !           903:                const char *ptr=(const char*)(cur+sizeof(uchar)*3); 
1.126     paf       904:                APPEND(ptr, size, lang, file, 0);
1.113     parser    905: 
1.126     paf       906:                size_t piece_size=sizeof(uchar)+sizeof(ushort)+size;
1.113     parser    907:                cur+=piece_size;
                    908:                buf_size-=piece_size;
                    909:        }
                    910: }

E-mail: