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

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

E-mail: