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

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.158   ! paf         7:        $Id: pa_string.C,v 1.157 2002/04/22 14:11:28 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.151     paf        32:        last_chunk=&head.chunk;
                     33:        head.chunk.count=CR_PREALLOCATED_COUNT;
                     34:        append_here=head.chunk.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()) {
1.151     paf        45:        last_chunk=&head.chunk;
                     46:        head.chunk.count=CR_PREALLOCATED_COUNT;
                     47:        append_here=head.chunk.rows;
1.140     paf        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.152     paf        73:        Chunk *new_chunk=static_cast<Chunk *>(malloc(
1.151     paf        74:                        sizeof(Chunk)// count+interpadding(?)+rows[CR_PREALLOCATED_COUNT]+tailpadding(??)
                     75:                        -sizeof(Chunk::rows_type) // PREALLOCATED rows
                     76:                        +sizeof(Chunk::Row)*new_chunk_count // neaded rows
                     77:                        +sizeof(Chunk *) // link size
                     78:                , 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]
1.149     paf        88:                throw Exception(0,
1.139     paf        89:                        this,
1.142     paf        90:                        "string growth stopped (append cstr)");
1.139     paf        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.149     paf       138:                throw Exception(0,
1.97      parser    139:                        this,
                    140:                        "getting first char of empty string");
                    141: 
1.151     paf       142:        return *head.chunk.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:        return result;
                    151: }
                    152: 
1.60      paf       153: /// @todo move 'lang' skipping to pos
                    154: int String::cmp(int& partial, const String& src, 
                    155:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       156:        partial=-1;
1.125     paf       157:        size_t a_size=size();
                    158:        this_offset=min(this_offset, a_size-1);
1.55      paf       159: 
1.151     paf       160:        const Chunk *a_chunk=&head.chunk;
                    161:        const Chunk *b_chunk=&src.head.chunk;
1.16      paf       162:        const Chunk::Row *a_row=a_chunk->rows;
                    163:        const Chunk::Row *b_row=b_chunk->rows;
1.55      paf       164:        size_t a_offset=this_offset;
                    165:        size_t b_offset=0;
1.9       paf       166:        Chunk::Row *a_end=append_here;
                    167:        Chunk::Row *b_end=src.append_here;
1.116     paf       168:        uint a_countdown=a_chunk->count;
                    169:        uint b_countdown=b_chunk->count;
                    170:        int result;
1.60      paf       171:        size_t pos=0; 
1.33      paf       172: 
1.125     paf       173:        bool a_break=a_size==0;
1.140     paf       174:        bool b_break=src.is_empty();
1.83      parser    175:        if(!(a_break || b_break)) while(true) {
1.55      paf       176:                if(pos+a_row->item.size > this_offset) {
1.136     paf       177:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang>lang) 
1.60      paf       178:                                return -1; // wrong lang -- bail out
                    179: 
1.55      paf       180:                        int size_diff=
                    181:                                (a_row->item.size-a_offset)-
                    182:                                (b_row->item.size-b_offset);
                    183:                        
                    184:                        if(size_diff==0) { // a has same size as b
1.60      paf       185:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    186:                                        a_row->item.size-a_offset);
1.55      paf       187:                                if(result)
                    188:                                        return result;
1.60      paf       189:                                pos+=a_row->item.size;
1.55      paf       190:                                a_row++; a_countdown--; a_offset=0;
                    191:                                b_row++; b_countdown--; b_offset=0;
                    192:                        } else if (size_diff>0) { // a longer
1.60      paf       193:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    194:                                        b_row->item.size-b_offset);
1.55      paf       195:                                if(result)
                    196:                                        return result;
                    197:                                a_offset+=b_row->item.size-b_offset;
                    198:                                b_row++; b_countdown--; b_offset=0;
                    199:                        } else { // b longer
1.60      paf       200:                                result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, 
                    201:                                        a_row->item.size-a_offset);
1.55      paf       202:                                if(result)
                    203:                                        return result;
                    204:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       205:                                pos+=a_row->item.size;
1.55      paf       206:                                a_row++; a_countdown--; a_offset=0;
                    207:                        }
1.83      parser    208:                        if(b_break=b_row==b_end) {
                    209:                                a_break=a_row==a_end;
                    210:                                break;                  
                    211:                        }
1.55      paf       212:                        if(!b_countdown) {
                    213:                                b_chunk=b_row->link;
                    214:                                b_row=b_chunk->rows;
                    215:                                b_countdown=b_chunk->count;
                    216:                        }
                    217:                } else {
1.60      paf       218:                        a_offset-=a_row->item.size;
                    219:                        pos+=a_row->item.size;
                    220:                        a_row++; a_countdown--; 
1.9       paf       221:                }
                    222: 
1.83      parser    223:                if(a_break=a_row==a_end) {
                    224:                        b_break=b_row==b_end;
                    225:                        break;
                    226:                }
1.11      paf       227:                if(!a_countdown) {
1.9       paf       228:                        a_chunk=a_row->link;
                    229:                        a_row=a_chunk->rows;
1.11      paf       230:                        a_countdown=a_chunk->count;
1.9       paf       231:                }
1.27      paf       232:        }
1.55      paf       233:        if(a_break==b_break) { // ended simultaneously
                    234:                partial=0; return 0;
                    235:        } else if(a_break) { // first bytes equal, but a ended before b
                    236:                partial=1; return -1;
                    237:        } else {
                    238:                partial=2; return +1;
                    239:        }
1.27      paf       240: }
                    241: 
1.60      paf       242: /// @todo move 'lang' skipping to pos
1.59      paf       243: int String::cmp(int& partial, const char* b_ptr, size_t src_size, 
1.60      paf       244:                                size_t this_offset, Untaint_lang lang) const {
1.59      paf       245:        partial=-1;
1.125     paf       246:        size_t a_size=size();
1.50      paf       247:        size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.125     paf       248:        this_offset=min(this_offset, a_size-1);
1.27      paf       249: 
1.151     paf       250:        const Chunk *a_chunk=&head.chunk;
1.27      paf       251:        const Chunk::Row *a_row=a_chunk->rows;
1.59      paf       252:        size_t a_offset=this_offset;
1.55      paf       253:        size_t b_offset=0;
1.27      paf       254:        Chunk::Row *a_end=append_here;
1.116     paf       255:        uint a_countdown=a_chunk->count;
1.60      paf       256:        size_t pos=0;
1.52      paf       257: 
1.125     paf       258:        bool a_break=a_size==0;
1.83      parser    259:        bool b_break=b_size==0;
                    260:        if(!(a_break || b_break)) while(true) {
1.59      paf       261:                if(pos+a_row->item.size > this_offset) {
1.136     paf       262:                        if(lang!=UL_UNSPECIFIED && a_row->item.lang>lang) 
1.60      paf       263:                                return -1; // wrong lang -- bail out
                    264: 
1.59      paf       265:                        int size_diff=
                    266:                                (a_row->item.size-a_offset)-
                    267:                                (b_size-b_offset);
                    268:                        
                    269:                        if(size_diff==0) { // a has same size as b
1.116     paf       270:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       271:                                        a_row->item.size-a_offset)!=0)
                    272:                                        return result;
1.60      paf       273:                                pos+=a_row->item.size;
1.59      paf       274:                                a_row++; a_countdown--; a_offset=0;
                    275:                                b_break=true;
                    276:                        } else if (size_diff>0) { // a longer
1.116     paf       277:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       278:                                        b_size-b_offset)!=0)
                    279:                                        return result;
                    280:                                a_offset+=b_size-b_offset;
                    281:                                b_break=true;
                    282:                        } else { // b longer
1.116     paf       283:                                if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, 
1.59      paf       284:                                        a_row->item.size-a_offset)!=0)
                    285:                                        return result;
                    286:                                b_offset+=a_row->item.size-a_offset;
1.60      paf       287:                                pos+=a_row->item.size;
1.59      paf       288:                                a_row++; a_countdown--; a_offset=0;
                    289:                        }
                    290:                } else {
1.60      paf       291:                        a_offset-=a_row->item.size; 
                    292:                        pos+=a_row->item.size;
                    293:                        a_row++; a_countdown--; 
1.27      paf       294:                }
                    295: 
1.86      parser    296:                a_break=a_row==a_end;
                    297:                if(a_break || b_break)
1.83      parser    298:                        break;
1.27      paf       299:                if(!a_countdown) {
                    300:                        a_chunk=a_row->link;
                    301:                        a_row=a_chunk->rows;
                    302:                        a_countdown=a_chunk->count;
1.9       paf       303:                }
                    304:        }
1.55      paf       305:        if(a_break==b_break) { // ended simultaneously
                    306:                partial=0; return 0;
                    307:        } else if(a_break) { // first bytes equal, but a ended before b
                    308:                partial=1; return -1;
                    309:        } else {
                    310:                partial=2; return +1;
                    311:        }
1.5       paf       312: }
1.46      paf       313: 
                    314: #ifndef NO_STRING_ORIGIN
                    315: const Origin& String::origin() const { 
1.140     paf       316:        if(is_empty()) {
1.96      parser    317:                static const Origin empty_origin={"empty string"};
                    318:                return empty_origin;
                    319:        }
1.46      paf       320:        
1.147     paf       321:        // determining origin by first piece or last appended piece
                    322:        // because any of them can be constant=without origin: 
1.50      paf       323:        // ex: ^load[/file] "document_root" + "/file"
1.80      paf       324:        // when last peice is constant, 
                    325:        // ex: parser_root_auto_path{dynamic} / auto.p{const}
                    326:        // using first piece
1.151     paf       327:        Origin& first_origin=head.chunk.rows[0].item.origin;
1.147     paf       328:        return first_origin.file ? first_origin : append_here[-1].item.origin;
1.46      paf       329: }
                    330: #endif
1.53      paf       331: 
1.69      paf       332: String& String::mid(size_t start, size_t finish) const {
1.107     parser    333:        String& result=*NEW String(pool());
                    334: 
1.139     paf       335:        start=max(size_t(0), start);
1.111     parser    336:        finish=min(size(), finish);
1.60      paf       337:        if(start==finish)
1.107     parser    338:                return result;
1.53      paf       339: 
                    340:        size_t pos=0;
1.123     paf       341:        STRING_FOREACH_ROW(
                    342:                size_t item_finish=pos+row->item.size;
                    343:                if(item_finish > start) { // started now or already?
1.140     paf       344:                        bool started=result.is_empty(); // started now?
1.123     paf       345:                        bool finished=finish <= item_finish; // finished now?
                    346:                        size_t offset=started?start-pos:0;
                    347:                        size_t size=finished?finish-pos:row->item.size;
                    348:                        result.APPEND(
                    349:                                row->item.ptr+offset, size-offset, 
                    350:                                row->item.lang,
                    351:                                row->item.origin.file, row->item.origin.line);
                    352:                        if(finished)
1.53      paf       353:                                goto break2;
                    354:                }
1.123     paf       355:                pos+=row->item.size;
                    356:        );
1.53      paf       357: break2:
1.60      paf       358: //     SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
                    359:                //cstr(), start, finish, result.cstr());
1.53      paf       360:        return result;
1.54      paf       361: }
                    362: 
1.60      paf       363: int String::pos(const String& substr, 
1.116     paf       364:                                int result, Untaint_lang lang) const {
1.125     paf       365:        size_t self_size=size();
1.131     paf       366:        for(; size_t(result)<self_size; result++) {
1.60      paf       367:                int partial; cmp(partial, substr, result, lang);
1.58      paf       368:                if(
                    369:                        partial==0 || // full match
                    370:                        partial==2) // 'substr' starts 'this'+'result'
                    371:                        return result;
                    372:        }
                    373:        
                    374:        return -1;
                    375: }
                    376: 
1.60      paf       377: int String::pos(const char *substr, size_t substr_size, 
1.116     paf       378:                                int result, Untaint_lang lang) const {
1.125     paf       379:        size_t self_size=size();
1.131     paf       380:        for(; size_t(result)<self_size; result++) {
1.60      paf       381:                int partial; cmp(partial, substr, substr_size, result, lang);
1.55      paf       382:                if(
                    383:                        partial==0 || // full match
                    384:                        partial==2) // 'substr' starts 'this'+'result'
                    385:                        return result;
                    386:        }
                    387:        
                    388:        return -1;
1.60      paf       389: }
                    390: 
                    391: void String::split(Array& result, 
                    392:                                   size_t* pos_after_ref, 
                    393:                                   const char *delim, size_t delim_size, 
                    394:                                   Untaint_lang lang, int limit) const {
1.125     paf       395:        size_t self_size=size();
1.60      paf       396:        if(delim_size) {
                    397:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    398:                int pos_before;
                    399:                // while we have 'delim'...
                    400:                for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       401:                        result+=&mid(pos_after, pos_before);
1.60      paf       402:                        pos_after=pos_before+delim_size;
                    403:                }
                    404:                // last piece
1.124     paf       405:                if(pos_after<self_size && limit) {
                    406:                        result+=&mid(pos_after, self_size);
                    407:                        pos_after=self_size;
1.60      paf       408:                }
                    409:                if(pos_after_ref)
                    410:                        *pos_after_ref=pos_after;
                    411:        } else { // empty delim
                    412:                result+=this;
                    413:                if(pos_after_ref)
1.124     paf       414:                        *pos_after_ref+=self_size;
1.60      paf       415:        }
                    416: }
                    417: 
                    418: void String::split(Array& result, 
                    419:                                   size_t* pos_after_ref, 
                    420:                                   const String& delim, Untaint_lang lang, 
                    421:                                   int limit) const {
1.140     paf       422:        if(!delim.is_empty()) {
1.60      paf       423:                size_t pos_after=pos_after_ref?*pos_after_ref:0;
                    424:                int pos_before;
                    425:                // while we have 'delim'...
                    426:                for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69      paf       427:                        result+=&mid(pos_after, pos_before);
1.60      paf       428:                        pos_after=pos_before+delim.size();
                    429:                }
                    430:                // last piece
                    431:                if(pos_after<size() && limit) {
1.69      paf       432:                        result+=&mid(pos_after, size());
1.60      paf       433:                        pos_after=size();
                    434:                }
                    435:                if(pos_after_ref)
                    436:                        *pos_after_ref=pos_after;
                    437:        } else { // empty delim
                    438:                result+=this;
                    439:                if(pos_after_ref)
                    440:                        *pos_after_ref+=size();
                    441:        }
1.61      paf       442: }
                    443: 
1.154     paf       444: static void regex_options(const String *options, int *result, bool& need_pre_post_match){
1.63      paf       445:     struct Regex_option {
1.153     paf       446:                const char *keyL;
                    447:                const char *keyU;
1.63      paf       448:                int clear, set;
                    449:                int *result;
1.154     paf       450:                bool *flag;
1.63      paf       451:     } regex_option[]={
1.153     paf       452:                {"i", "I", 0, PCRE_CASELESS, result}, // a=A
                    453:                {"s", "S", 0, PCRE_DOTALL, result}, // \n\n$ [default]
                    454:                {"x", "U", 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    455:                {"m", "M", PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    456:                {"g", "G", 0, true, result+1}, // many rows
1.154     paf       457:                {"'", 0, 0, 0, 0, &need_pre_post_match},
                    458:                {0}
1.63      paf       459:     };
                    460:        result[0]=PCRE_EXTRA | PCRE_DOTALL;
                    461:        result[1]=0;
                    462: 
                    463:     if(options) 
1.153     paf       464:                for(Regex_option *o=regex_option; o->keyL; o++) 
1.154     paf       465:                        if(options->pos(o->keyL)>=0
                    466:                                || (o->keyU && options->pos(o->keyU)>=0)) {
                    467:                                if(o->flag)
                    468:                                        *o->flag=true;
                    469:                                else { // result
                    470:                                        *o->result &= ~o->clear;
                    471:                                        *o->result |= o->set;
                    472:                                }
1.63      paf       473:                        }
                    474: }
                    475: 
1.155     paf       476: /// @todo make replacement Table stacked
1.158   ! paf       477: bool String::match(
1.77      paf       478:                                   const String *aorigin,
1.62      paf       479:                                   const String& regexp, 
1.63      paf       480:                                   const String *options,
1.64      paf       481:                                   Table **table,
1.95      parser    482:                                   Row_action row_action, void *info,
                    483:                                   bool *was_global) const { 
1.64      paf       484: 
1.140     paf       485:        if(regexp.is_empty())
1.149     paf       486:                throw Exception(0,
1.73      paf       487:                        aorigin,
                    488:                        "regexp is empty");
1.154     paf       489: 
1.118     paf       490:        const char *pattern=regexp.cstr();
1.62      paf       491:        const char *errptr;
                    492:        int erroffset;
1.154     paf       493:     bool need_pre_post_match=false;
                    494:        int option_bits[2];  regex_options(options, option_bits, need_pre_post_match);
1.95      parser    495:        if(was_global)
                    496:                *was_global=option_bits[1]!=0;
1.63      paf       497:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       498:                &errptr, &erroffset,
1.132     paf       499:                pool().get_source_charset().pcre_tables);
1.62      paf       500: 
1.67      paf       501:        if(!code)
1.149     paf       502:                throw Exception(0,
1.69      paf       503:                        &regexp.mid(erroffset, regexp.size()),
1.74      paf       504:                        "regular expression syntax error - %s", errptr);
1.62      paf       505:        
1.63      paf       506:        int info_substrings=pcre_info(code, 0, 0);
                    507:        if(info_substrings<0) {
1.100     parser    508:                pcre_free(code);
1.149     paf       509:                throw Exception(0,
1.73      paf       510:                        aorigin,
1.76      paf       511:                        "pcre_info error (%d)", 
1.73      paf       512:                                info_substrings);
1.63      paf       513:        }
                    514: 
1.158   ! paf       515:        const char *subject=cstr();
1.62      paf       516:        int length=strlen(subject);
1.155     paf       517:        const int ovecsize=(1/*match*/+MAX_STRING_MATCH_TABLE_COLUMNS)*3;
                    518:        int ovector[ovecsize];
                    519: 
                    520:        // create table
1.157     paf       521:        *table=NEW Table(pool(), *string_match_table_template);
1.63      paf       522: 
1.64      paf       523:        int exec_option_bits=0;
1.154     paf       524:        int prestart=0;
                    525:        int poststart=0;
                    526:        int postfinish=size();
1.63      paf       527:        while(true) {
                    528:                int exec_substrings=pcre_exec(code, 0,
1.154     paf       529:                        subject, length, prestart,
1.64      paf       530:                        exec_option_bits, ovector, ovecsize);
1.63      paf       531:                
                    532:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    533:                        pcre_free(code);
1.154     paf       534:                        row_action(**table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
1.63      paf       535:                        return option_bits[1]!=0; // global=true+table, not global=false
                    536:                }
                    537: 
                    538:                if(exec_substrings<0) {
1.100     parser    539:                        pcre_free(code);
1.149     paf       540:                        throw Exception(0,
1.63      paf       541:                                aorigin,
1.76      paf       542:                                "regular expression execute error (%d)", 
1.63      paf       543:                                        exec_substrings);
                    544:                }
                    545: 
1.154     paf       546:                int prefinish=ovector[0];
                    547:                poststart=ovector[1];
1.63      paf       548:                Array& row=*NEW Array(pool());
1.154     paf       549:                row+=need_pre_post_match?&mid(0, prefinish):0; // .prematch column value
                    550:                row+=need_pre_post_match?&mid(prefinish, poststart):0; // .match
                    551:                row+=need_pre_post_match?&mid(poststart, postfinish):0; // .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.154     paf       558:                row_action(**table, &row, prestart, prefinish, poststart, postfinish, info);
1.63      paf       559: 
1.154     paf       560:                if(!option_bits[1] || prestart==poststart) { // not global | going to hang
1.100     parser    561:                        pcre_free(code);
1.154     paf       562:                        row_action(**table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
1.63      paf       563:                        return true;
                    564:                }
1.154     paf       565:                prestart=poststart;
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.149     paf       591:                throw Exception(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: 
1.143     paf       599:        STRING_FOREACH_ROW(
                    600:                char *new_cstr=(char *)pool.malloc(row->item.size, 12);
                    601:                char *dest=new_cstr;
                    602:                const char *src=row->item.ptr; 
                    603:                for(int size=row->item.size; size--; src++) {
                    604:                        unsigned char c=a[(unsigned char)*src];
                    605:                        if(b)
                    606:                                c=b[c];
1.82      parser    607: 
1.143     paf       608:                        *dest++=(char)c;
1.82      parser    609:                }
1.143     paf       610:                
                    611:                result.APPEND(new_cstr, row->item.size, 
                    612:                        row->item.lang,
                    613:                        row->item.origin.file, row->item.origin.line);
                    614:        );
1.89      parser    615: 
1.101     parser    616:        return result;
                    617: }
                    618: 
1.150     paf       619: /// @test if in some piece were found no dict words, append it, not it's duplicate
                    620: String& String::replace(Pool& pool, Dictionary& dict) const {
                    621: //     return reconstruct(pool).replace_in_reconstructed(pool, dict);
1.108     parser    622:        String& result=*new(pool) String(pool);
1.150     paf       623: 
1.143     paf       624:        STRING_FOREACH_ROW(
1.156     paf       625:                const char *src=row->item.ptr; 
                    626:                size_t src_size=row->item.size;
1.123     paf       627:                char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);
                    628:                char *dest=new_cstr;
                    629:                while(src_size) {
                    630:                        // there is a row where first column starts 'src'
                    631:                        if(Table::Item *item=dict.first_that_starts(src, src_size)) {
                    632:                                // get a=>b values
                    633:                                const String& a=*static_cast<Array *>(item)->get_string(0);
                    634:                                const String& b=*static_cast<Array *>(item)->get_string(1);
                    635:                                // skip 'a' in 'src' && reduce work size
                    636:                                src+=a.size();  src_size-=a.size();
                    637:                                // write 'b' to 'dest' && skip 'b' in 'dest'
                    638:                                b.store_to(dest);  dest+=b.size();
                    639:                        } else {
                    640:                                // write a char to b && reduce work size
                    641:                                *dest++=*src++;  src_size--;
1.101     parser    642:                        }
                    643:                }
                    644: 
1.156     paf       645:                result.APPEND(new_cstr, dest-new_cstr, row->item.lang,
                    646:                        row->item.origin.file, row->item.origin.line);
                    647:        );
                    648:        return result;
                    649: }
                    650: 
                    651: String& String::join_chains(Pool& pool, char** acstr) const {
                    652:        char *lcstr=cstr();
                    653:        const char *current=lcstr;
                    654: 
                    655:        String& result=*new(pool) String(pool);
                    656:        STRING_FOREACH_ROW(
                    657: IFNDEF_NO_STRING_ORIGIN(
                    658:                const char *joined_origin_file=row->item.origin.file;
                    659:                const size_t joined_origin_line=row->item.origin.line;
                    660: );
                    661:                uchar joined_lang=row->item.lang;
                    662:                const char *joined_ptr=current;
                    663:                // calc size
                    664:                size_t joined_size=0;
                    665:                STRING_PREPARED_FOREACH_ROW(*this, 
                    666:                        if(row->item.lang==joined_lang)
                    667:                                joined_size+=row->item.size;
                    668:                        else
                    669:                                break; // before non-ours
                    670:                );
                    671:                current+=joined_size;
                    672: 
                    673:                // pointers are after joined piece
                    674:                // & one step back, see STRING_FOREACH_ROW
                    675:                --row;  ++countdown;
                    676:                
                    677:                result.APPEND(joined_ptr, joined_size, joined_lang,
1.150     paf       678:                        joined_origin_file, joined_origin_line);
1.123     paf       679:        );
1.156     paf       680: 
                    681:        if(acstr)
                    682:                *acstr=lcstr;
1.89      parser    683:        return result;
                    684: }
                    685: 
1.90      parser    686: double String::as_double() const { 
1.89      parser    687:        double result;
1.114     paf       688:        const char *cstr;
                    689:        char buf[MAX_NUMBER];
1.151     paf       690:        if(head.chunk.rows+1==append_here) {
                    691:                int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
                    692:                memcpy(buf, head.chunk.rows[0].item.ptr, size);
1.114     paf       693:                buf[size]=0;
                    694:                cstr=buf;
                    695:        } else
                    696:                cstr=this->cstr();
1.102     parser    697:        char *error_pos;
1.89      parser    698:        // 0xABC
1.99      parser    699:        if(cstr[0]=='0')
                    700:                if(cstr[1]=='x' || cstr[1]=='X')
                    701:                        result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
                    702:                else
1.102     parser    703:                        result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89      parser    704:        else
1.99      parser    705:                result=(double)strtod(cstr, &error_pos);
1.89      parser    706: 
1.103     parser    707:        if(*error_pos/*not EOS*/)
1.149     paf       708:                throw Exception("number.format",
1.89      parser    709:                        this,
                    710:                        "invalid number (double)");
                    711: 
                    712:        return result;
                    713: }
1.90      parser    714: int String::as_int() const { 
1.89      parser    715:        int result;
1.114     paf       716:        const char *cstr;
                    717:        char buf[MAX_NUMBER];
1.151     paf       718:        if(head.chunk.rows+1==append_here) {
                    719:                int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
                    720:                memcpy(buf, head.chunk.rows[0].item.ptr, size);
1.114     paf       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=(int)(unsigned long)strtol(cstr, &error_pos, 0);
                    730:                else
1.102     parser    731:                        result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89      parser    732:        else
                    733:                result=(int)strtol(cstr, &error_pos, 0);
                    734: 
1.103     parser    735:        if(*error_pos/*not EOS*/)
1.149     paf       736:                throw Exception("number.format",
1.89      parser    737:                        this,
                    738:                        "invalid number (int)");
1.82      parser    739: 
                    740:        return result;
1.61      paf       741: }
1.113     parser    742: 
1.128     paf       743: inline void ushort2uchars(ushort word, uchar& byte1, uchar& byte2) {
                    744:        byte1=word&0xFF;
                    745:        byte2=word>>8;
                    746: }
                    747: inline ushort uchars2ushort(uchar byte1, uchar byte2) {
                    748:        return (byte2<<8) | byte1;
                    749: }
1.113     parser    750: /* @todo maybe network order worth spending some effort?
                    751:        don't bothering myself with network byte order,
                    752:        am not planning to be able to move resulting file across platforms
                    753:        for now
                    754: */
                    755: void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
                    756:        buf_size=
                    757:                prolog_size
1.126     paf       758:                +used_rows()*(sizeof(uchar)+sizeof(ushort))
1.113     parser    759:                +size();
1.114     paf       760:        buf=malloc(buf_size,15);
1.113     parser    761:        char *cur=(char *)buf+prolog_size;
                    762: 
1.123     paf       763:        STRING_FOREACH_ROW(
                    764:                // lang
1.126     paf       765:                memcpy(cur, &row->item.lang, sizeof(uchar));
                    766:                cur+=sizeof(uchar);
1.123     paf       767:                // size
1.128     paf       768:                uchar byte1; uchar byte2;
                    769:                ushort2uchars(row->item.size, byte1, byte2);
                    770:                memcpy(cur, &byte1, sizeof(uchar)); cur+=sizeof(uchar);
                    771:                memcpy(cur, &byte2, sizeof(uchar)); cur+=sizeof(uchar);
1.123     paf       772:                // bytes
                    773:                memcpy(cur, row->item.ptr, row->item.size);
                    774:                cur+=row->item.size;
                    775:        );
1.113     parser    776: }
1.148     paf       777: bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
1.135     paf       778:        if(buf_size<=prolog_size)
1.148     paf       779:                return false;
1.135     paf       780: 
1.126     paf       781:        char *cur=(char *)buf+prolog_size;
1.113     parser    782:        buf_size-=prolog_size;
                    783: 
                    784:        while(buf_size) {
1.148     paf       785:                if(sizeof(uchar)+sizeof(ushort)>buf_size) // lang+size
                    786:                        return false;
                    787: 
                    788:                uchar lang=*(uchar *)(cur);             
1.128     paf       789:                ushort size=uchars2ushort(
                    790:                        *(uchar*)(cur+sizeof(uchar)*1),
                    791:                        *(uchar*)(cur+sizeof(uchar)*2)
                    792:                );
                    793: 
1.148     paf       794:                size_t piece_size=sizeof(uchar)+sizeof(ushort)+size;
                    795:                if(piece_size>buf_size) // buffer overrun, can be on incomplete cache files
                    796:                        return false;
                    797: 
1.128     paf       798:                const char *ptr=(const char*)(cur+sizeof(uchar)*3); 
1.126     paf       799:                APPEND(ptr, size, lang, file, 0);
1.113     parser    800: 
                    801:                cur+=piece_size;
                    802:                buf_size-=piece_size;
                    803:        }
1.148     paf       804:        return true;
1.113     parser    805: }

E-mail: