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

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

E-mail: