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

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

E-mail: