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

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

E-mail: