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

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

E-mail: