Annotation of parser3/src/main/pa_string.C, revision 1.29
1.4 paf 1: /*
1.29 ! paf 2: $Id: pa_string.C,v 1.28 2001/02/22 08:16:31 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.28 paf 16: last_chunk=&head;
17: head.count=CR_PREALLOCATED_COUNT;
1.5 paf 18: append_here=head.rows;
1.2 paf 19: head.preallocated_link=0;
1.28 paf 20: link_row=&head.rows[head.count];
1.8 paf 21: fused_rows=fsize=0;
1.1 paf 22: }
23:
24: void String::expand() {
1.28 paf 25: int new_chunk_count=last_chunk->count+last_chunk->count*CR_GROW_PERCENT/100;
26: last_chunk=static_cast<Chunk *>(
27: pool().malloc(sizeof(int)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
28: last_chunk->count=new_chunk_count;
29: link_row->link=last_chunk;
30: append_here=last_chunk->rows;
31: link_row=&last_chunk->rows[last_chunk->count];
1.8 paf 32: link_row->link=0;
1.1 paf 33: }
34:
1.16 paf 35: String::String(const String& src) :
1.26 paf 36: Pooled(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.28 paf 42: int curr_chunk_rows=head.count;
1.8 paf 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
1.28 paf 59: int curr_chunk_rows=src_used_rows-head.count;
1.8 paf 60: Chunk *new_chunk=static_cast<Chunk *>(
1.26 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;
1.28 paf 64: append_here=link_row=&new_chunk->rows[new_chunk->count];
1.8 paf 65:
66: Chunk *old_chunk=src.head.preallocated_link;
67: Chunk::Row *new_rows=new_chunk->rows;
1.28 paf 68: int rows_left_to_copy=new_chunk->count;
1.8 paf 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: }
1.28 paf 92:
93: String& String::operator += (const String& src) {
94: int src_used_rows=src.used_rows();
95: int dst_free_rows=link_row-append_here;
96:
97: if(src_used_rows<=dst_free_rows) {
98: // all new rows fit into last chunk
99: memcpy(append_here, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
100: append_here+=src_used_rows;
101: } else {
102: // not all new rows don't fit into last chunk: shrinking it to used part,
103: int used_rows=last_chunk->count-dst_free_rows;
104: //int *countp=append_here
105: link_row=&last_chunk->rows[last_chunk->count=used_rows];
106: // allocating only enough mem to fit src string rows
107: // next append would allocate a new chunk
108: last_chunk=static_cast<Chunk *>(
109: pool().malloc(sizeof(int)+sizeof(Chunk::Row)*src_used_rows+sizeof(Chunk *)));
110: last_chunk->count=src_used_rows;
111: link_row->link=last_chunk;
112: append_here=link_row=&last_chunk->rows[src_used_rows];
113:
114: Chunk *old_chunk=src.head.preallocated_link;
115: Chunk::Row *new_rows=last_chunk->rows;
116: int rows_left_to_copy=src_used_rows;
117: while(true) {
118: int old_count=old_chunk->count;
119: Chunk *next_chunk=old_chunk->rows[old_count].link;
120: if(next_chunk) {
121: // not last source chunk
122: // taking it all
123: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
124: new_rows+=old_count;
125: rows_left_to_copy-=old_count;
126:
127: old_chunk=next_chunk;
128: } else {
129: // the last source chunk
130: // taking only those rows of chunk that _left_to_copy
131: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
132: break;
133: }
134: }
1.29 ! paf 135: link_row->link=0;
1.28 paf 136: }
137: fused_rows+=src_used_rows;
138: fsize+=src.fsize;
139:
140: return *this;
1.23 paf 141: }
1.5 paf 142:
1.13 paf 143: String& String::real_append(STRING_APPEND_PARAMS) {
1.9 paf 144: if(!src)
145: return *this;
1.26 paf 146: if(!size)
147: size=strlen(src);
148: if(!size)
1.9 paf 149: return *this;
150:
1.1 paf 151: if(chunk_is_full())
152: expand();
153:
154: append_here->item.ptr=src;
1.26 paf 155: fsize+=append_here->item.size=size;
1.13 paf 156: #ifndef NO_STRING_ORIGIN
1.14 paf 157: append_here->item.origin.file=file;
158: append_here->item.origin.line=line;
1.13 paf 159: #endif
1.8 paf 160: append_here++; fused_rows++;
1.1 paf 161:
162: return *this;
163: }
164:
1.16 paf 165: char *String::cstr() const {
1.26 paf 166: char *result=static_cast<char *>(pool().malloc(size()+1));
1.1 paf 167:
168: char *copy_here=result;
1.16 paf 169: const Chunk *chunk=&head;
1.2 paf 170: do {
1.16 paf 171: const Chunk::Row *row=chunk->rows;
1.2 paf 172: for(int i=0; i<chunk->count; i++) {
1.1 paf 173: if(row==append_here)
174: goto break2;
175:
176: memcpy(copy_here, row->item.ptr, row->item.size);
177: copy_here+=row->item.size;
178: row++;
179: }
1.2 paf 180: chunk=row->link;
181: } while(chunk);
1.1 paf 182: break2:
183: *copy_here=0;
184: return result;
185: }
186:
1.16 paf 187: uint String::hash_code() const {
1.7 paf 188: uint result=0;
1.5 paf 189:
1.16 paf 190: const Chunk *chunk=&head;
1.5 paf 191: do {
1.16 paf 192: const Chunk::Row *row=chunk->rows;
1.5 paf 193: for(int i=0; i<chunk->count; i++) {
194: if(row==append_here)
195: goto break2;
196:
1.6 paf 197: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 198: row++;
199: }
200: chunk=row->link;
201: } while(chunk);
202: break2:
203: return result;
204: }
205:
1.16 paf 206: bool String::operator == (const String& src) const {
1.8 paf 207: if(size() != src.size())
208: return false;
209:
1.16 paf 210: const Chunk *a_chunk=&head;
211: const Chunk *b_chunk=&src.head;
212: const Chunk::Row *a_row=a_chunk->rows;
213: const Chunk::Row *b_row=b_chunk->rows;
1.9 paf 214: int a_offset=0;
215: int b_offset=0;
216: Chunk::Row *a_end=append_here;
217: Chunk::Row *b_end=src.append_here;
1.11 paf 218: int a_countdown=a_chunk->count;
219: int b_countdown=b_chunk->count;
1.9 paf 220: bool a_break=false;
221: bool b_break=false;
222: while(true) {
223: int size_diff=
224: (a_row->item.size-a_offset)-
225: (b_row->item.size-b_offset);
226:
227: if(size_diff==0) { // a has same size as b
228: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
229: return false;
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
233: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset)!=0)
234: return false;
235: a_offset+=b_row->item.size-b_offset;
1.11 paf 236: b_row++; b_countdown--; b_offset=0;
1.9 paf 237: } else { // b longer
238: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
239: return false;
240: b_offset+=a_row->item.size-a_offset;
1.11 paf 241: a_row++; a_countdown--; a_offset=0;
1.9 paf 242: }
243:
244: a_break=a_row==a_end;
245: b_break=b_row==b_end;
246: if(a_break || b_break)
247: break;
248:
1.11 paf 249: if(!a_countdown) {
1.9 paf 250: a_chunk=a_row->link;
251: a_row=a_chunk->rows;
1.11 paf 252: a_countdown=a_chunk->count;
1.9 paf 253: }
1.11 paf 254: if(!b_countdown) {
1.9 paf 255: b_chunk=b_row->link;
256: b_row=b_chunk->rows;
1.11 paf 257: b_countdown=b_chunk->count;
1.27 paf 258: }
259: }
260: return a_break==b_break;
261: }
262:
263: bool String::operator == (char* b_ptr) const {
264: size_t b_size=b_ptr?strlen(b_ptr):0;
265: if(size() != b_size)
266: return false;
267:
268: const Chunk *a_chunk=&head;
269: const Chunk::Row *a_row=a_chunk->rows;
270: int a_offset=0;
271: int b_offset=0;
272: Chunk::Row *a_end=append_here;
273: int a_countdown=a_chunk->count;
274: bool a_break=false;
275: bool b_break=false;
276: while(true) {
277: int size_diff=
278: (a_row->item.size-a_offset)-
279: (b_size-b_offset);
280:
281: if(size_diff==0) { // a has same size as b
282: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
283: return false;
284: a_row++; a_countdown--; a_offset=0;
285: b_break=true;
286: } else if (size_diff>0) { // a longer
287: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
288: return false;
289: a_offset+=b_size-b_offset;
290: b_break=true;
291: } else { // b longer
292: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
293: return false;
294: b_offset+=a_row->item.size-a_offset;
295: a_row++; a_countdown--; a_offset=0;
296: }
297:
298: a_break=a_row==a_end;
299: if(a_break || b_break)
300: break;
301:
302: if(!a_countdown) {
303: a_chunk=a_row->link;
304: a_row=a_chunk->rows;
305: a_countdown=a_chunk->count;
1.9 paf 306: }
307: }
308: return a_break==b_break;
1.5 paf 309: }
E-mail: