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

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.152   ! paf         7:        $Id: pa_string.C,v 1.151 2002/04/10 08:53:55 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.63      paf       444: static void regex_options(char *options, int *result){
                    445:     struct Regex_option {
                    446:                char key;
                    447:                int clear, set;
                    448:                int *result;
                    449:     } regex_option[]={
                    450:                {'i', 0, PCRE_CASELESS, result}, // a=A
1.79      paf       451:                {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63      paf       452:                {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
                    453:                {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
                    454:                {'g', 0, true, result+1}, // many rows
                    455:                {0},
                    456:     };
                    457:        result[0]=PCRE_EXTRA | PCRE_DOTALL;
                    458:        result[1]=0;
                    459: 
                    460:     if(options) 
                    461:                for(Regex_option *o=regex_option; o->key; o++) 
                    462:                        if(
                    463:                                strchr(options, o->key) || 
                    464:                                strchr(options, toupper(o->key))) {
                    465:                                *(o->result)&=~o->clear;
                    466:                                *(o->result)|=o->set;
                    467:                        }
                    468: }
                    469: 
1.88      parser    470: /// @todo maybe need speedup: some option to remove pre/match/post string generation
1.132     paf       471: bool String::match(
1.77      paf       472:                                   const String *aorigin,
1.62      paf       473:                                   const String& regexp, 
1.63      paf       474:                                   const String *options,
1.64      paf       475:                                   Table **table,
1.95      parser    476:                                   Row_action row_action, void *info,
                    477:                                   bool *was_global) const { 
1.64      paf       478: 
1.140     paf       479:        if(regexp.is_empty())
1.149     paf       480:                throw Exception(0,
1.73      paf       481:                        aorigin,
                    482:                        "regexp is empty");
1.118     paf       483:        const char *pattern=regexp.cstr();
1.62      paf       484:        const char *errptr;
                    485:        int erroffset;
1.63      paf       486:     int option_bits[2];  regex_options(options?options->cstr():0, option_bits);
1.95      parser    487:        if(was_global)
                    488:                *was_global=option_bits[1]!=0;
1.63      paf       489:        pcre *code=pcre_compile(pattern, option_bits[0], 
1.62      paf       490:                &errptr, &erroffset,
1.132     paf       491:                pool().get_source_charset().pcre_tables);
1.62      paf       492: 
1.67      paf       493:        if(!code)
1.149     paf       494:                throw Exception(0,
1.69      paf       495:                        &regexp.mid(erroffset, regexp.size()),
1.74      paf       496:                        "regular expression syntax error - %s", errptr);
1.62      paf       497:        
1.63      paf       498:        int info_substrings=pcre_info(code, 0, 0);
                    499:        if(info_substrings<0) {
1.100     parser    500:                pcre_free(code);
1.149     paf       501:                throw Exception(0,
1.73      paf       502:                        aorigin,
1.76      paf       503:                        "pcre_info error (%d)", 
1.73      paf       504:                                info_substrings);
1.63      paf       505:        }
                    506: 
                    507:        int startoffset=0;
1.118     paf       508:        const char *subject=cstr();
1.62      paf       509:        int length=strlen(subject);
1.63      paf       510:        int ovecsize;
                    511:        int *ovector=(int *)malloc(sizeof(int)*
1.114     paf       512:                (ovecsize=(1/*match*/+info_substrings)*3), 11);
1.62      paf       513: 
1.64      paf       514:        { // create table
                    515:                Array& columns=*NEW Array(pool());
                    516:                columns+=string_pre_match_name;
                    517:                columns+=string_match_name;
                    518:                columns+=string_post_match_name;
                    519:                for(int i=1; i<=info_substrings; i++) {
                    520:                        char *column=(char *)malloc(MAX_NUMBER);
                    521:                        snprintf(column, MAX_NUMBER, "%d", i);
                    522:                        columns+=NEW String(pool(), column); // .i column name
                    523:                }
                    524:                *table=NEW Table(pool(), aorigin, &columns);
1.62      paf       525:        }
1.63      paf       526: 
1.64      paf       527:        int exec_option_bits=0;
1.63      paf       528:        while(true) {
                    529:                int exec_substrings=pcre_exec(code, 0,
                    530:                        subject, length, startoffset,
1.64      paf       531:                        exec_option_bits, ovector, ovecsize);
1.63      paf       532:                
                    533:                if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100     parser    534:                        pcre_free(code);
                    535:                        row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       536:                        return option_bits[1]!=0; // global=true+table, not global=false
                    537:                }
                    538: 
                    539:                if(exec_substrings<0) {
1.100     parser    540:                        pcre_free(code);
1.149     paf       541:                        throw Exception(0,
1.63      paf       542:                                aorigin,
1.76      paf       543:                                "regular expression execute error (%d)", 
1.63      paf       544:                                        exec_substrings);
                    545:                }
                    546: 
                    547:                Array& row=*NEW Array(pool());
1.81      paf       548:                row+=&mid(0, ovector[0]); // .prematch column value
1.69      paf       549:                row+=&mid(ovector[0], ovector[1]); // .match
1.81      paf       550:                row+=&mid(ovector[1], size()); // .postmatch
1.63      paf       551:                
                    552:                for(int i=1; i<exec_substrings; i++) {
1.69      paf       553:                        // -1:-1 case handled peacefully by mid() itself
                    554:                        row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63      paf       555:                }
                    556:                
1.100     parser    557:                row_action(**table, &row, startoffset, ovector[0], info);
1.63      paf       558: 
1.100     parser    559:                if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang
                    560:                        pcre_free(code);
                    561:                        row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63      paf       562:                        return true;
                    563:                }
1.100     parser    564:                startoffset=ovector[1];
1.63      paf       565: 
                    566: /*
                    567:                if(option_bits[0] & PCRE_MULTILINE)
1.64      paf       568:                        exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63      paf       569: */
                    570:        }
1.82      parser    571: }
                    572: 
1.132     paf       573: String& String::change_case(Pool& pool, 
1.82      parser    574:                                                        Change_case_kind kind) const {
1.132     paf       575:        const unsigned char *tables=pool.get_source_charset().pcre_tables;
1.82      parser    576:        String& result=*new(pool) String(pool);
                    577: 
                    578:        const unsigned char *a;
                    579:        const unsigned char *b;
                    580:        switch(kind) {
                    581:        case CC_UPPER:
                    582:                a=tables+lcc_offset;
                    583:                b=tables+fcc_offset;
                    584:                break;
                    585:        case CC_LOWER:
                    586:                a=tables+lcc_offset;
                    587:                b=0;
                    588:                break;
                    589:        default:
1.149     paf       590:                throw Exception(0, 
1.82      parser    591:                        this, 
                    592:                        "unknown change case kind #%d", 
                    593:                                static_cast<int>(kind)); // never
                    594:                a=b=0; // calm, compiler
                    595:                break; // never
                    596:        }       
                    597: 
1.143     paf       598:        STRING_FOREACH_ROW(
                    599:                char *new_cstr=(char *)pool.malloc(row->item.size, 12);
                    600:                char *dest=new_cstr;
                    601:                const char *src=row->item.ptr; 
                    602:                for(int size=row->item.size; size--; src++) {
                    603:                        unsigned char c=a[(unsigned char)*src];
                    604:                        if(b)
                    605:                                c=b[c];
1.82      parser    606: 
1.143     paf       607:                        *dest++=(char)c;
1.82      parser    608:                }
1.143     paf       609:                
                    610:                result.APPEND(new_cstr, row->item.size, 
                    611:                        row->item.lang,
                    612:                        row->item.origin.file, row->item.origin.line);
                    613:        );
1.89      parser    614: 
1.101     parser    615:        return result;
                    616: }
                    617: 
1.108     parser    618: void String::join_chain(Pool& pool, 
1.143     paf       619:                                           const Chunk*& achunk, const Chunk::Row*& arow, uint& acountdown, 
1.123     paf       620:                                           uchar& joined_lang, const char *& joined_ptr, size_t& joined_size) const {
                    621:        joined_lang=arow->item.lang;
1.108     parser    622:        
                    623:        // calc size
                    624:        joined_size=0;
                    625:        {
1.143     paf       626:                const Chunk* chunk=achunk;
                    627:                const Chunk::Row* row=arow;
                    628:                uint countdown=acountdown;
                    629:                STRING_PREPARED_FOREACH_ROW(*this, 
                    630:                        if(row->item.lang==joined_lang)
                    631:                                joined_size+=row->item.size;
                    632:                        else
1.108     parser    633:                                break;
1.143     paf       634:                );
1.108     parser    635:        }
                    636: 
                    637:        // if one row, return simply itself
                    638:        if(joined_size==arow->item.size) {
                    639:                joined_ptr=arow->item.ptr;
                    640:        } else {
                    641:                // join adjacent rows
1.143     paf       642:                char *ptr=(char *)pool.malloc(joined_size,13);  
1.108     parser    643:                joined_ptr=ptr;
1.143     paf       644: 
                    645:                const Chunk* chunk=achunk;
                    646:                const Chunk::Row* row=arow;
                    647:                uint countdown=acountdown;
                    648:                STRING_PREPARED_FOREACH_ROW(*this, 
                    649:                        if(row->item.lang==joined_lang) {
                    650:                                memcpy(ptr, row->item.ptr, row->item.size);  ptr+=row->item.size;
1.146     paf       651:                        } else
                    652:                                break; // before non-ours
1.143     paf       653:                );
1.108     parser    654:                
1.145     paf       655:                // set pointers after joined piece
                    656:                achunk=chunk;  arow=row;  acountdown=countdown;
                    657:                // & one step back, see String::reconstruct
1.146     paf       658:                --arow;  ++acountdown;
1.108     parser    659:        }
                    660: }
                    661: 
1.150     paf       662: /// @test if in some piece were found no dict words, append it, not it's duplicate
                    663: String& String::replace(Pool& pool, Dictionary& dict) const {
                    664: //     return reconstruct(pool).replace_in_reconstructed(pool, dict);
1.108     parser    665:        String& result=*new(pool) String(pool);
1.150     paf       666: 
1.143     paf       667:        STRING_FOREACH_ROW(
1.133     paf       668:                uchar joined_lang;
                    669:                const char *joined_ptr;
                    670:                size_t joined_size;
1.143     paf       671: IFNDEF_NO_STRING_ORIGIN(
1.133     paf       672:                const char *joined_origin_file=row->item.origin.file;
                    673:                const size_t joined_origin_line=row->item.origin.line;
1.143     paf       674: );
                    675:                join_chain(pool, chunk, row, countdown,
1.133     paf       676:                        joined_lang, joined_ptr, joined_size);
1.150     paf       677:                
                    678:                const char *src=joined_ptr; 
                    679:                size_t src_size=joined_size;
1.123     paf       680:                char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);
                    681:                char *dest=new_cstr;
                    682:                while(src_size) {
                    683:                        // there is a row where first column starts 'src'
                    684:                        if(Table::Item *item=dict.first_that_starts(src, src_size)) {
                    685:                                // get a=>b values
                    686:                                const String& a=*static_cast<Array *>(item)->get_string(0);
                    687:                                const String& b=*static_cast<Array *>(item)->get_string(1);
                    688:                                // skip 'a' in 'src' && reduce work size
                    689:                                src+=a.size();  src_size-=a.size();
                    690:                                // write 'b' to 'dest' && skip 'b' in 'dest'
                    691:                                b.store_to(dest);  dest+=b.size();
                    692:                        } else {
                    693:                                // write a char to b && reduce work size
                    694:                                *dest++=*src++;  src_size--;
1.101     parser    695:                        }
                    696:                }
                    697: 
1.150     paf       698:                result.APPEND(new_cstr, dest-new_cstr, joined_lang,
                    699:                        joined_origin_file, joined_origin_line);
1.123     paf       700:        );
1.89      parser    701:        return result;
                    702: }
                    703: 
1.90      parser    704: double String::as_double() const { 
1.89      parser    705:        double result;
1.114     paf       706:        const char *cstr;
                    707:        char buf[MAX_NUMBER];
1.151     paf       708:        if(head.chunk.rows+1==append_here) {
                    709:                int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
                    710:                memcpy(buf, head.chunk.rows[0].item.ptr, size);
1.114     paf       711:                buf[size]=0;
                    712:                cstr=buf;
                    713:        } else
                    714:                cstr=this->cstr();
1.102     parser    715:        char *error_pos;
1.89      parser    716:        // 0xABC
1.99      parser    717:        if(cstr[0]=='0')
                    718:                if(cstr[1]=='x' || cstr[1]=='X')
                    719:                        result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
                    720:                else
1.102     parser    721:                        result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89      parser    722:        else
1.99      parser    723:                result=(double)strtod(cstr, &error_pos);
1.89      parser    724: 
1.103     parser    725:        if(*error_pos/*not EOS*/)
1.149     paf       726:                throw Exception("number.format",
1.89      parser    727:                        this,
                    728:                        "invalid number (double)");
                    729: 
                    730:        return result;
                    731: }
1.90      parser    732: int String::as_int() const { 
1.89      parser    733:        int result;
1.114     paf       734:        const char *cstr;
                    735:        char buf[MAX_NUMBER];
1.151     paf       736:        if(head.chunk.rows+1==append_here) {
                    737:                int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
                    738:                memcpy(buf, head.chunk.rows[0].item.ptr, size);
1.114     paf       739:                buf[size]=0;
                    740:                cstr=buf;
                    741:        } else
                    742:                cstr=this->cstr();
1.102     parser    743:        char *error_pos;
1.89      parser    744:        // 0xABC
1.99      parser    745:        if(cstr[0]=='0')
                    746:                if(cstr[1]=='x' || cstr[1]=='X')
                    747:                        result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
                    748:                else
1.102     parser    749:                        result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89      parser    750:        else
                    751:                result=(int)strtol(cstr, &error_pos, 0);
                    752: 
1.103     parser    753:        if(*error_pos/*not EOS*/)
1.149     paf       754:                throw Exception("number.format",
1.89      parser    755:                        this,
                    756:                        "invalid number (int)");
1.82      parser    757: 
                    758:        return result;
1.61      paf       759: }
1.113     parser    760: 
1.128     paf       761: inline void ushort2uchars(ushort word, uchar& byte1, uchar& byte2) {
                    762:        byte1=word&0xFF;
                    763:        byte2=word>>8;
                    764: }
                    765: inline ushort uchars2ushort(uchar byte1, uchar byte2) {
                    766:        return (byte2<<8) | byte1;
                    767: }
1.113     parser    768: /* @todo maybe network order worth spending some effort?
                    769:        don't bothering myself with network byte order,
                    770:        am not planning to be able to move resulting file across platforms
                    771:        for now
                    772: */
                    773: void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
                    774:        buf_size=
                    775:                prolog_size
1.126     paf       776:                +used_rows()*(sizeof(uchar)+sizeof(ushort))
1.113     parser    777:                +size();
1.114     paf       778:        buf=malloc(buf_size,15);
1.113     parser    779:        char *cur=(char *)buf+prolog_size;
                    780: 
1.123     paf       781:        STRING_FOREACH_ROW(
                    782:                // lang
1.126     paf       783:                memcpy(cur, &row->item.lang, sizeof(uchar));
                    784:                cur+=sizeof(uchar);
1.123     paf       785:                // size
1.128     paf       786:                uchar byte1; uchar byte2;
                    787:                ushort2uchars(row->item.size, byte1, byte2);
                    788:                memcpy(cur, &byte1, sizeof(uchar)); cur+=sizeof(uchar);
                    789:                memcpy(cur, &byte2, sizeof(uchar)); cur+=sizeof(uchar);
1.123     paf       790:                // bytes
                    791:                memcpy(cur, row->item.ptr, row->item.size);
                    792:                cur+=row->item.size;
                    793:        );
1.113     parser    794: }
1.148     paf       795: bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
1.135     paf       796:        if(buf_size<=prolog_size)
1.148     paf       797:                return false;
1.135     paf       798: 
1.126     paf       799:        char *cur=(char *)buf+prolog_size;
1.113     parser    800:        buf_size-=prolog_size;
                    801: 
                    802:        while(buf_size) {
1.148     paf       803:                if(sizeof(uchar)+sizeof(ushort)>buf_size) // lang+size
                    804:                        return false;
                    805: 
                    806:                uchar lang=*(uchar *)(cur);             
1.128     paf       807:                ushort size=uchars2ushort(
                    808:                        *(uchar*)(cur+sizeof(uchar)*1),
                    809:                        *(uchar*)(cur+sizeof(uchar)*2)
                    810:                );
                    811: 
1.148     paf       812:                size_t piece_size=sizeof(uchar)+sizeof(ushort)+size;
                    813:                if(piece_size>buf_size) // buffer overrun, can be on incomplete cache files
                    814:                        return false;
                    815: 
1.128     paf       816:                const char *ptr=(const char*)(cur+sizeof(uchar)*3); 
1.126     paf       817:                APPEND(ptr, size, lang, file, 0);
1.113     parser    818: 
                    819:                cur+=piece_size;
                    820:                buf_size-=piece_size;
                    821:        }
1.148     paf       822:        return true;
1.113     parser    823: }

E-mail: