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

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

E-mail: