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

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

E-mail: