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

1.4       paf         1: /*
1.15    ! paf         2:   $Id: pa_string.C,v 1.14 2001/01/29 15:56:04 paf Exp $
1.4       paf         3: */
                      4: 
1.1       paf         5: #include <string.h>
                      6: 
1.13      paf         7: #include "pa_pool.h"
1.12      paf         8: #include "pa_string.h"
1.5       paf         9: #include "pa_hash.h"
1.1       paf        10: 
1.15    ! paf        11: void *String::operator new(size_t size, Pool& apool) {
        !            12:        return apool.malloc(size);
1.1       paf        13: }
                     14: 
1.15    ! paf        15: String::String(Pool& apool) :
1.13      paf        16:        pool(apool) {
1.2       paf        17:        head.count=curr_chunk_rows=CR_PREALLOCATED_COUNT;
1.5       paf        18:        append_here=head.rows;
1.2       paf        19:        head.preallocated_link=0;
1.5       paf        20:        link_row=&head.rows[curr_chunk_rows];
1.8       paf        21:        fused_rows=fsize=0;
1.1       paf        22: }
                     23: 
                     24: void String::expand() {
1.8       paf        25:        curr_chunk_rows+=curr_chunk_rows*CR_GROW_PERCENT/100;
1.2       paf        26:        Chunk *chunk=static_cast<Chunk *>(
1.15    ! paf        27:                pool.malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.2       paf        28:        chunk->count=curr_chunk_rows;
                     29:        link_row->link=chunk;
1.5       paf        30:        append_here=chunk->rows;
                     31:        link_row=&chunk->rows[curr_chunk_rows];
1.8       paf        32:        link_row->link=0;
1.1       paf        33: }
                     34: 
1.14      paf        35: String::String(String& src) :
                     36:        pool(src.pool) {
1.8       paf        37:        head.count=CR_PREALLOCATED_COUNT;
                     38:        
                     39:        int src_used_rows=src.used_rows();
                     40:        if(src_used_rows<=head.count) {
1.10      paf        41:                // all new rows fit into preallocated area
1.8       paf        42:                curr_chunk_rows=head.count;
                     43:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
                     44:                append_here=&head.rows[src_used_rows];
                     45:                link_row=&head.rows[curr_chunk_rows];
                     46:        } else {
                     47:                // warning: 
1.10      paf        48:                //   heavily relies on the fact 
                     49:                //   "preallocated area is the same for all strings"
1.8       paf        50:                //
                     51:                // info:
                     52:                //   allocating only enough mem to fit src string rows
                     53:                //   next append would allocate a new chunk
                     54:                //
                     55:                // new rows don't fit into preallocated area: splitting into two chunks
                     56:                // preallocated chunk src to constructing head
                     57:                memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
                     58:                // remaining rows into new_chunk
                     59:                curr_chunk_rows=src_used_rows-head.count;
                     60:                Chunk *new_chunk=static_cast<Chunk *>(
1.15    ! paf        61:                        pool.malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.8       paf        62:                new_chunk->count=curr_chunk_rows;
                     63:                head.preallocated_link=new_chunk;
                     64:                append_here=link_row=&new_chunk->rows[curr_chunk_rows];
                     65: 
                     66:                Chunk *old_chunk=src.head.preallocated_link; 
                     67:                Chunk::Row *new_rows=new_chunk->rows;
                     68:                int rows_left_to_copy=curr_chunk_rows;
                     69:                while(true) {
                     70:                        int old_count=old_chunk->count;
                     71:                        Chunk *next_chunk=old_chunk->rows[old_count].link;
                     72:                        if(next_chunk) {
                     73:                                // not last source chunk
                     74:                                // taking it all
                     75:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
                     76:                                new_rows+=old_count;
                     77:                                rows_left_to_copy-=old_count;
                     78: 
                     79:                                old_chunk=next_chunk;
                     80:                        } else {
                     81:                                // the last source chunk
                     82:                                // taking only those rows of chunk that _left_to_copy
                     83:                                memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
                     84:                                break;
                     85:                        }
                     86:                }
1.5       paf        87:        }
1.8       paf        88:        link_row->link=0;
                     89:        fused_rows=src_used_rows;
                     90:        fsize=src.fsize;
1.5       paf        91: }
                     92: 
1.13      paf        93: String& String::real_append(STRING_APPEND_PARAMS) {
1.9       paf        94:        if(!src)
                     95:                return *this;
                     96:        int len=strlen(src);
                     97:        if(!len)
                     98:                return *this;
                     99: 
1.1       paf       100:        if(chunk_is_full())
                    101:                expand();
                    102: 
                    103:        append_here->item.ptr=src;
1.9       paf       104:        fsize+=append_here->item.size=len;
1.13      paf       105: #ifndef NO_STRING_ORIGIN
1.14      paf       106:        append_here->item.origin.file=file;
                    107:        append_here->item.origin.line=line;
1.13      paf       108: #endif
1.8       paf       109:        append_here++; fused_rows++;
1.1       paf       110: 
                    111:        return *this;
                    112: }
                    113: 
1.15    ! paf       114: char *String::cstr() {
        !           115:        char *result=static_cast<char *>(pool.malloc(size()+1));
1.1       paf       116: 
                    117:        char *copy_here=result;
1.2       paf       118:        Chunk *chunk=&head; 
                    119:        do {
1.5       paf       120:                Chunk::Row *row=chunk->rows;
1.2       paf       121:                for(int i=0; i<chunk->count; i++) {
1.1       paf       122:                        if(row==append_here)
                    123:                                goto break2;
                    124: 
                    125:                        memcpy(copy_here, row->item.ptr, row->item.size);
                    126:                        copy_here+=row->item.size;
                    127:                        row++;
                    128:                }
1.2       paf       129:                chunk=row->link;
                    130:        } while(chunk);
1.1       paf       131: break2:
                    132:        *copy_here=0;
                    133:        return result;
                    134: }
                    135: 
1.7       paf       136: uint String::hash_code() {
                    137:        uint result=0;
1.5       paf       138: 
                    139:        Chunk *chunk=&head; 
                    140:        do {
                    141:                Chunk::Row *row=chunk->rows;
                    142:                for(int i=0; i<chunk->count; i++) {
                    143:                        if(row==append_here)
                    144:                                goto break2;
                    145: 
1.6       paf       146:                        result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5       paf       147:                        row++;
                    148:                }
                    149:                chunk=row->link;
                    150:        } while(chunk);
                    151: break2:
                    152:        return result;
                    153: }
                    154: 
                    155: bool String::operator == (String& src) {
1.8       paf       156:        if(size() != src.size())
                    157:                return false;
                    158: 
1.9       paf       159:        Chunk *a_chunk=&head;
                    160:        Chunk *b_chunk=&src.head;
                    161:        Chunk::Row *a_row=a_chunk->rows;
                    162:        Chunk::Row *b_row=b_chunk->rows;
                    163:        int a_offset=0;
                    164:        int b_offset=0;
                    165:        Chunk::Row *a_end=append_here;
                    166:        Chunk::Row *b_end=src.append_here;
1.11      paf       167:        int a_countdown=a_chunk->count;
                    168:        int b_countdown=b_chunk->count;
1.9       paf       169:        bool a_break=false;
                    170:        bool b_break=false;
                    171:        while(true) {
                    172:                int size_diff=
                    173:                        (a_row->item.size-a_offset)-
                    174:                        (b_row->item.size-b_offset);
                    175: 
                    176:                if(size_diff==0) { // a has same size as b
                    177:                        if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
                    178:                                return false;
1.11      paf       179:                        a_row++; a_countdown--; a_offset=0;
                    180:                        b_row++; b_countdown--; b_offset=0;
1.9       paf       181:                } else if (size_diff>0) { // a longer
                    182:                        if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset)!=0)
                    183:                                return false;
                    184:                        a_offset+=b_row->item.size-b_offset;
1.11      paf       185:                        b_row++; b_countdown--; b_offset=0;
1.9       paf       186:                } else { // b longer
                    187:                        if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
                    188:                                return false;
                    189:                        b_offset+=a_row->item.size-a_offset;
1.11      paf       190:                        a_row++; a_countdown--; a_offset=0;
1.9       paf       191:                }
                    192: 
                    193:                a_break=a_row==a_end;
                    194:                b_break=b_row==b_end;
                    195:                if(a_break || b_break)
                    196:                        break;
                    197: 
1.11      paf       198:                if(!a_countdown) {
1.9       paf       199:                        a_chunk=a_row->link;
                    200:                        a_row=a_chunk->rows;
1.11      paf       201:                        a_countdown=a_chunk->count;
1.9       paf       202:                }
1.11      paf       203:                if(!b_countdown) {
1.9       paf       204:                        b_chunk=b_row->link;
                    205:                        b_row=b_chunk->rows;
1.11      paf       206:                        b_countdown=b_chunk->count;
1.9       paf       207:                }
                    208:        }
                    209:        return a_break==b_break;
1.5       paf       210: }

E-mail: