Annotation of parser3/src/main/pa_string.C, revision 1.9
1.4 paf 1: /*
1.9 ! paf 2: $Id: pa_string.C,v 1.8 2001/01/27 12:04:53 paf Exp $
1.4 paf 3: */
4:
1.1 paf 5: #include <string.h>
6:
7: #include "pa_pool.h"
1.5 paf 8: #include "pa_hash.h"
1.1 paf 9:
10: void *String::operator new(size_t size, Pool *apool) {
1.5 paf 11: return apool->malloc(size);
1.1 paf 12: }
13:
1.2 paf 14: void String::construct(Pool *apool) {
1.1 paf 15: pool=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.8 paf 26: pool->malloc(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.8 paf 34: String::String(String& src) {
35: pool=src.pool;
36: head.count=CR_PREALLOCATED_COUNT;
37:
38: int src_used_rows=src.used_rows();
39: if(src_used_rows<=head.count) {
40: // new rows fit into preallocated area
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:
47: // heavy relies on the fact
48: // that preallocated area is the same for all strings
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 *>(
60: pool->malloc(sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
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: }
91:
1.1 paf 92: String& String::operator += (char *src) {
1.9 ! paf 93: if(!src)
! 94: return *this;
! 95: int len=strlen(src);
! 96: if(!len)
! 97: return *this;
! 98:
1.1 paf 99: if(chunk_is_full())
100: expand();
101:
102: append_here->item.ptr=src;
1.9 ! paf 103: fsize+=append_here->item.size=len;
1.8 paf 104: append_here++; fused_rows++;
1.1 paf 105:
106: return *this;
107: }
108:
109: char *String::c_str() {
1.5 paf 110: char *result=static_cast<char *>(pool->malloc(size()+1));
1.1 paf 111:
112: char *copy_here=result;
1.2 paf 113: Chunk *chunk=&head;
114: do {
1.5 paf 115: Chunk::Row *row=chunk->rows;
1.2 paf 116: for(int i=0; i<chunk->count; i++) {
1.1 paf 117: if(row==append_here)
118: goto break2;
119:
120: memcpy(copy_here, row->item.ptr, row->item.size);
121: copy_here+=row->item.size;
122: row++;
123: }
1.2 paf 124: chunk=row->link;
125: } while(chunk);
1.1 paf 126: break2:
127: *copy_here=0;
128: return result;
129: }
130:
1.7 paf 131: uint String::hash_code() {
132: uint result=0;
1.5 paf 133:
134: Chunk *chunk=&head;
135: do {
136: Chunk::Row *row=chunk->rows;
137: for(int i=0; i<chunk->count; i++) {
138: if(row==append_here)
139: goto break2;
140:
1.6 paf 141: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 142: row++;
143: }
144: chunk=row->link;
145: } while(chunk);
146: break2:
147: return result;
148: }
149:
150: bool String::operator == (String& src) {
1.8 paf 151: if(size() != src.size())
152: return false;
153:
1.9 ! paf 154: Chunk *a_chunk=&head;
! 155: Chunk *b_chunk=&src.head;
! 156: Chunk::Row *a_row=a_chunk->rows;
! 157: Chunk::Row *b_row=b_chunk->rows;
! 158: int a_offset=0;
! 159: int b_offset=0;
! 160: Chunk::Row *a_end=append_here;
! 161: Chunk::Row *b_end=src.append_here;
! 162: int a_count=a_chunk->count;
! 163: int b_count=b_chunk->count;
! 164: bool a_break=false;
! 165: bool b_break=false;
! 166: while(true) {
! 167: int size_diff=
! 168: (a_row->item.size-a_offset)-
! 169: (b_row->item.size-b_offset);
! 170:
! 171: if(size_diff==0) { // a has same size as b
! 172: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
! 173: return false;
! 174: a_row++; a_count--; a_offset=0;
! 175: b_row++; b_count--; b_offset=0;
! 176: } else if (size_diff>0) { // a longer
! 177: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset)!=0)
! 178: return false;
! 179: a_offset+=b_row->item.size-b_offset;
! 180: b_row++; b_count--; b_offset=0;
! 181: } else { // b longer
! 182: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
! 183: return false;
! 184: b_offset+=a_row->item.size-a_offset;
! 185: a_row++; a_count--; a_offset=0;
! 186: }
! 187:
! 188: a_break=a_row==a_end;
! 189: b_break=b_row==b_end;
! 190: if(a_break || b_break)
! 191: break;
! 192:
! 193: if(!a_count) {
! 194: a_chunk=a_row->link;
! 195: a_row=a_chunk->rows;
! 196: a_count=a_chunk->count;
! 197: }
! 198: if(!b_count) {
! 199: b_chunk=b_row->link;
! 200: b_row=b_chunk->rows;
! 201: b_count=b_chunk->count;
! 202: }
! 203: }
! 204: return a_break==b_break;
1.5 paf 205: }
E-mail: