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

1.4       paf         1: /*
1.36      paf         2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.37      paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.36      paf         5: 
1.44    ! paf         6:        $Id: pa_string.C,v 1.43 2001/03/18 20:31:27 paf Exp $
1.4       paf         7: */
                      8: 
1.1       paf         9: #include <string.h>
                     10: 
1.13      paf        11: #include "pa_pool.h"
1.12      paf        12: #include "pa_string.h"
1.5       paf        13: #include "pa_hash.h"
1.22      paf        14: #include "pa_exception.h"
1.1       paf        15: 
1.18      paf        16: // String
                     17: 
1.41      paf        18: String::String(Pool& apool, const char *src, bool tainted) :
1.17      paf        19:        Pooled(apool) {
1.28      paf        20:        last_chunk=&head;
                     21:        head.count=CR_PREALLOCATED_COUNT;
1.5       paf        22:        append_here=head.rows;
1.2       paf        23:        head.preallocated_link=0;
1.28      paf        24:        link_row=&head.rows[head.count];
1.8       paf        25:        fused_rows=fsize=0;
1.41      paf        26: 
                     27:        if(src)
                     28:                if(tainted)
                     29:                        APPEND_TAINTED(src, 0, 0, 0);
                     30:                else
                     31:                        APPEND(src, 0, 0, 0);
1.1       paf        32: }
                     33: 
                     34: void String::expand() {
1.28      paf        35:        int new_chunk_count=last_chunk->count+last_chunk->count*CR_GROW_PERCENT/100;
                     36:        last_chunk=static_cast<Chunk *>(
1.30      paf        37:                malloc(sizeof(int)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
1.28      paf        38:        last_chunk->count=new_chunk_count;
                     39:        link_row->link=last_chunk;
                     40:        append_here=last_chunk->rows;
                     41:        link_row=&last_chunk->rows[last_chunk->count];
1.8       paf        42:        link_row->link=0;
1.1       paf        43: }
                     44: 
1.40      paf        45: String::String(const String& src) :    Pooled(src.pool()) {
1.8       paf        46:        head.count=CR_PREALLOCATED_COUNT;
                     47:        
1.44    ! paf        48:        int src_used_rows=src.fused_rows;
1.8       paf        49:        if(src_used_rows<=head.count) {
1.10      paf        50:                // all new rows fit into preallocated area
1.28      paf        51:                int curr_chunk_rows=head.count;
1.8       paf        52:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
                     53:                append_here=&head.rows[src_used_rows];
                     54:                link_row=&head.rows[curr_chunk_rows];
                     55:        } else {
                     56:                // warning: 
1.10      paf        57:                //   heavily relies on the fact 
                     58:                //   "preallocated area is the same for all strings"
1.8       paf        59:                //
                     60:                // info:
                     61:                //   allocating only enough mem to fit src string rows
                     62:                //   next append would allocate a new chunk
                     63:                //
                     64:                // new rows don't fit into preallocated area: splitting into two chunks
                     65:                // preallocated chunk src to constructing head
                     66:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
                     67:                // remaining rows into new_chunk
1.28      paf        68:                int curr_chunk_rows=src_used_rows-head.count;
1.8       paf        69:                Chunk *new_chunk=static_cast<Chunk *>(
1.30      paf        70:                        malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.8       paf        71:                new_chunk->count=curr_chunk_rows;
                     72:                head.preallocated_link=new_chunk;
1.28      paf        73:                append_here=link_row=&new_chunk->rows[new_chunk->count];
1.8       paf        74: 
                     75:                Chunk *old_chunk=src.head.preallocated_link; 
                     76:                Chunk::Row *new_rows=new_chunk->rows;
1.28      paf        77:                int rows_left_to_copy=new_chunk->count;
1.8       paf        78:                while(true) {
                     79:                        int old_count=old_chunk->count;
                     80:                        Chunk *next_chunk=old_chunk->rows[old_count].link;
                     81:                        if(next_chunk) {
                     82:                                // not last source chunk
                     83:                                // taking it all
                     84:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
                     85:                                new_rows+=old_count;
                     86:                                rows_left_to_copy-=old_count;
                     87: 
                     88:                                old_chunk=next_chunk;
                     89:                        } else {
                     90:                                // the last source chunk
                     91:                                // taking only those rows of chunk that _left_to_copy
                     92:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
                     93:                                break;
                     94:                        }
                     95:                }
1.5       paf        96:        }
1.8       paf        97:        link_row->link=0;
                     98:        fused_rows=src_used_rows;
                     99:        fsize=src.fsize;
1.5       paf       100: }
1.28      paf       101: 
1.42      paf       102: String& String::append(const String& src, Untaint_lang lang, bool forced) {
1.44    ! paf       103:        int src_used_rows=src.fused_rows;
1.28      paf       104:        int dst_free_rows=link_row-append_here;
                    105:        
                    106:        if(src_used_rows<=dst_free_rows) {
                    107:                // all new rows fit into last chunk
                    108:                memcpy(append_here, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
1.42      paf       109:                set_lang(append_here, lang, forced, src_used_rows);
1.28      paf       110:                append_here+=src_used_rows;
                    111:        } else {
1.31      paf       112:                // not all new rows fit into last chunk: shrinking it to used part,
1.28      paf       113:                int used_rows=last_chunk->count-dst_free_rows;
                    114:                //int *countp=append_here
                    115:                link_row=&last_chunk->rows[last_chunk->count=used_rows];
                    116:                //   allocating only enough mem to fit src string rows
                    117:                //   next append would allocate a new chunk
                    118:                last_chunk=static_cast<Chunk *>(
1.30      paf       119:                        malloc(sizeof(int)+sizeof(Chunk::Row)*src_used_rows+sizeof(Chunk *)));
1.28      paf       120:                last_chunk->count=src_used_rows;
                    121:                link_row->link=last_chunk;
                    122:                append_here=link_row=&last_chunk->rows[src_used_rows];
                    123: 
1.31      paf       124:                const Chunk *old_chunk=&src.head; 
1.28      paf       125:                Chunk::Row *new_rows=last_chunk->rows;
                    126:                int rows_left_to_copy=src_used_rows;
                    127:                while(true) {
                    128:                        int old_count=old_chunk->count;
                    129:                        Chunk *next_chunk=old_chunk->rows[old_count].link;
                    130:                        if(next_chunk) {
                    131:                                // not last source chunk
                    132:                                // taking it all
                    133:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
1.42      paf       134:                                set_lang(new_rows, lang, forced, old_count);
1.28      paf       135:                                new_rows+=old_count;
                    136:                                rows_left_to_copy-=old_count;
                    137: 
                    138:                                old_chunk=next_chunk;
                    139:                        } else {
                    140:                                // the last source chunk
                    141:                                // taking only those rows of chunk that _left_to_copy
                    142:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
1.42      paf       143:                                set_lang(new_rows, lang, forced, rows_left_to_copy);
1.28      paf       144:                                break;
                    145:                        }
                    146:                }
1.29      paf       147:                link_row->link=0;
1.28      paf       148:        }
                    149:        fused_rows+=src_used_rows;
                    150:        fsize+=src.fsize;
                    151: 
                    152:        return *this;
1.23      paf       153: }
1.42      paf       154: void String::set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size) {
1.43      paf       155:        if(lang==PASS_APPENDED)
1.34      paf       156:                return;
                    157: 
                    158:        while(size--) {
                    159:                Untaint_lang& item_lang=(row++)->item.lang;
1.42      paf       160:                if(item_lang==YES || forced) // tainted? need untaint language assignment
1.34      paf       161:                        item_lang=lang;  // assign untaint language
                    162:        }
1.40      paf       163: }
                    164: 
1.42      paf       165: /*void String::change_lang(Untaint_lang lang) {
1.40      paf       166:        Chunk *chunk=&head; 
                    167:        do {
                    168:                Chunk::Row *row=chunk->rows;
                    169:                for(int i=0; i<chunk->count; i++) {
                    170:                        if(row==append_here)
                    171:                                goto break2;
                    172: 
                    173:                        row->item.lang=lang;
                    174:                        row++;
                    175:                }
                    176:                chunk=row->link;
                    177:        } while(chunk);
                    178: break2:
                    179:        return;
1.34      paf       180: }
1.42      paf       181: */
1.13      paf       182: String& String::real_append(STRING_APPEND_PARAMS) {
1.9       paf       183:        if(!src)
                    184:                return *this;
1.26      paf       185:        if(!size)
                    186:                size=strlen(src);
                    187:        if(!size)
1.9       paf       188:                return *this;
                    189: 
1.1       paf       190:        if(chunk_is_full())
                    191:                expand();
                    192: 
                    193:        append_here->item.ptr=src;
1.26      paf       194:        fsize+=append_here->item.size=size;
1.38      paf       195:        append_here->item.lang=tainted?YES:NO;
1.13      paf       196: #ifndef NO_STRING_ORIGIN
1.14      paf       197:        append_here->item.origin.file=file;
                    198:        append_here->item.origin.line=line;
1.13      paf       199: #endif
1.8       paf       200:        append_here++; fused_rows++;
1.1       paf       201: 
                    202:        return *this;
                    203: }
                    204: 
1.16      paf       205: uint String::hash_code() const {
1.7       paf       206:        uint result=0;
1.5       paf       207: 
1.16      paf       208:        const Chunk *chunk=&head; 
1.5       paf       209:        do {
1.16      paf       210:                const Chunk::Row *row=chunk->rows;
1.5       paf       211:                for(int i=0; i<chunk->count; i++) {
                    212:                        if(row==append_here)
                    213:                                goto break2;
                    214: 
1.6       paf       215:                        result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5       paf       216:                        row++;
                    217:                }
                    218:                chunk=row->link;
                    219:        } while(chunk);
                    220: break2:
                    221:        return result;
                    222: }
                    223: 
1.32      paf       224: int String::cmp(const String& src) const {
1.16      paf       225:        const Chunk *a_chunk=&head;
                    226:        const Chunk *b_chunk=&src.head;
                    227:        const Chunk::Row *a_row=a_chunk->rows;
                    228:        const Chunk::Row *b_row=b_chunk->rows;
1.9       paf       229:        int a_offset=0;
                    230:        int b_offset=0;
                    231:        Chunk::Row *a_end=append_here;
                    232:        Chunk::Row *b_end=src.append_here;
1.11      paf       233:        int a_countdown=a_chunk->count;
                    234:        int b_countdown=b_chunk->count;
1.9       paf       235:        bool a_break=false;
                    236:        bool b_break=false;
1.32      paf       237:        int result;
1.9       paf       238:        while(true) {
1.33      paf       239:                a_break=a_row==a_end;
                    240:                b_break=b_row==b_end;
                    241:                if(a_break || b_break)
                    242:                        break;
                    243: 
1.9       paf       244:                int size_diff=
                    245:                        (a_row->item.size-a_offset)-
                    246:                        (b_row->item.size-b_offset);
                    247: 
                    248:                if(size_diff==0) { // a has same size as b
1.32      paf       249:                        result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
                    250:                        if(result)
                    251:                                return result;
1.11      paf       252:                        a_row++; a_countdown--; a_offset=0;
                    253:                        b_row++; b_countdown--; b_offset=0;
1.9       paf       254:                } else if (size_diff>0) { // a longer
1.32      paf       255:                        result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset);
                    256:                        if(result)
                    257:                                return result;
1.9       paf       258:                        a_offset+=b_row->item.size-b_offset;
1.11      paf       259:                        b_row++; b_countdown--; b_offset=0;
1.9       paf       260:                } else { // b longer
1.32      paf       261:                        result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
                    262:                        if(result)
                    263:                                return result;
1.9       paf       264:                        b_offset+=a_row->item.size-a_offset;
1.11      paf       265:                        a_row++; a_countdown--; a_offset=0;
1.9       paf       266:                }
                    267: 
1.11      paf       268:                if(!a_countdown) {
1.9       paf       269:                        a_chunk=a_row->link;
                    270:                        a_row=a_chunk->rows;
1.11      paf       271:                        a_countdown=a_chunk->count;
1.9       paf       272:                }
1.11      paf       273:                if(!b_countdown) {
1.9       paf       274:                        b_chunk=b_row->link;
                    275:                        b_row=b_chunk->rows;
1.11      paf       276:                        b_countdown=b_chunk->count;
1.27      paf       277:                }
                    278:        }
1.32      paf       279:        if(a_break==b_break) // ended simultaneously
                    280:                result=0;
                    281:        else if(a_break) // first bytes equal, but a ended before b
                    282:                result=-1;
                    283:        else
                    284:                result=+1;
                    285:        return result;
1.27      paf       286: }
                    287: 
1.39      paf       288: bool String::operator == (const char* b_ptr) const {
1.27      paf       289:        size_t b_size=b_ptr?strlen(b_ptr):0;
                    290:        if(size() != b_size)
                    291:                return false;
                    292: 
                    293:        const Chunk *a_chunk=&head;
                    294:        const Chunk::Row *a_row=a_chunk->rows;
                    295:        int a_offset=0;
                    296:        int b_offset=0;
                    297:        Chunk::Row *a_end=append_here;
                    298:        int a_countdown=a_chunk->count;
                    299:        bool a_break=false;
                    300:        bool b_break=false;
                    301:        while(true) {
                    302:                int size_diff=
                    303:                        (a_row->item.size-a_offset)-
                    304:                        (b_size-b_offset);
                    305: 
                    306:                if(size_diff==0) { // a has same size as b
                    307:                        if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
                    308:                                return false;
                    309:                        a_row++; a_countdown--; a_offset=0;
                    310:                        b_break=true;
                    311:                } else if (size_diff>0) { // a longer
                    312:                        if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
                    313:                                return false;
                    314:                        a_offset+=b_size-b_offset;
                    315:                        b_break=true;
                    316:                } else { // b longer
                    317:                        if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
                    318:                                return false;
                    319:                        b_offset+=a_row->item.size-a_offset;
                    320:                        a_row++; a_countdown--; a_offset=0;
                    321:                }
                    322: 
                    323:                a_break=a_row==a_end;
                    324:                if(a_break || b_break)
                    325:                        break;
                    326: 
                    327:                if(!a_countdown) {
                    328:                        a_chunk=a_row->link;
                    329:                        a_row=a_chunk->rows;
                    330:                        a_countdown=a_chunk->count;
1.9       paf       331:                }
                    332:        }
                    333:        return a_break==b_break;
1.5       paf       334: }

E-mail: