Annotation of parser3/src/main/pa_string.C, revision 1.32
1.4 paf 1: /*
1.32 ! paf 2: $Id: pa_string.C,v 1.31 2001/02/22 12:43: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.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 *>(
1.30 paf 27: malloc(sizeof(int)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
1.28 paf 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.30 paf 61: 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 {
1.31 paf 102: // not all new rows fit into last chunk: shrinking it to used part,
1.28 paf 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 *>(
1.30 paf 109: malloc(sizeof(int)+sizeof(Chunk::Row)*src_used_rows+sizeof(Chunk *)));
1.28 paf 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:
1.31 paf 114: const Chunk *old_chunk=&src.head;
1.28 paf 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.30 paf 166: char *result=static_cast<char *>(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.32 ! paf 206: int String::cmp(const String& src) const {
1.16 paf 207: const Chunk *a_chunk=&head;
208: const Chunk *b_chunk=&src.head;
209: const Chunk::Row *a_row=a_chunk->rows;
210: const Chunk::Row *b_row=b_chunk->rows;
1.9 paf 211: int a_offset=0;
212: int b_offset=0;
213: Chunk::Row *a_end=append_here;
214: Chunk::Row *b_end=src.append_here;
1.11 paf 215: int a_countdown=a_chunk->count;
216: int b_countdown=b_chunk->count;
1.9 paf 217: bool a_break=false;
218: bool b_break=false;
1.32 ! paf 219: int result;
1.9 paf 220: while(true) {
221: int size_diff=
222: (a_row->item.size-a_offset)-
223: (b_row->item.size-b_offset);
224:
225: if(size_diff==0) { // a has same size as b
1.32 ! paf 226: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
! 227: if(result)
! 228: return result;
1.11 paf 229: a_row++; a_countdown--; a_offset=0;
230: b_row++; b_countdown--; b_offset=0;
1.9 paf 231: } else if (size_diff>0) { // a longer
1.32 ! paf 232: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset);
! 233: if(result)
! 234: return result;
1.9 paf 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
1.32 ! paf 238: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
! 239: if(result)
! 240: return result;
1.9 paf 241: b_offset+=a_row->item.size-a_offset;
1.11 paf 242: a_row++; a_countdown--; a_offset=0;
1.9 paf 243: }
244:
245: a_break=a_row==a_end;
246: b_break=b_row==b_end;
247: if(a_break || b_break)
248: break;
249:
1.11 paf 250: if(!a_countdown) {
1.9 paf 251: a_chunk=a_row->link;
252: a_row=a_chunk->rows;
1.11 paf 253: a_countdown=a_chunk->count;
1.9 paf 254: }
1.11 paf 255: if(!b_countdown) {
1.9 paf 256: b_chunk=b_row->link;
257: b_row=b_chunk->rows;
1.11 paf 258: b_countdown=b_chunk->count;
1.27 paf 259: }
260: }
1.32 ! paf 261: if(a_break==b_break) // ended simultaneously
! 262: result=0;
! 263: else if(a_break) // first bytes equal, but a ended before b
! 264: result=-1;
! 265: else
! 266: result=+1;
! 267: return result;
1.27 paf 268: }
269:
270: bool String::operator == (char* b_ptr) const {
271: size_t b_size=b_ptr?strlen(b_ptr):0;
272: if(size() != b_size)
273: return false;
274:
275: const Chunk *a_chunk=&head;
276: const Chunk::Row *a_row=a_chunk->rows;
277: int a_offset=0;
278: int b_offset=0;
279: Chunk::Row *a_end=append_here;
280: int a_countdown=a_chunk->count;
281: bool a_break=false;
282: bool b_break=false;
283: while(true) {
284: int size_diff=
285: (a_row->item.size-a_offset)-
286: (b_size-b_offset);
287:
288: if(size_diff==0) { // a has same size as b
289: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
290: return false;
291: a_row++; a_countdown--; a_offset=0;
292: b_break=true;
293: } else if (size_diff>0) { // a longer
294: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
295: return false;
296: a_offset+=b_size-b_offset;
297: b_break=true;
298: } else { // b longer
299: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
300: return false;
301: b_offset+=a_row->item.size-a_offset;
302: a_row++; a_countdown--; a_offset=0;
303: }
304:
305: a_break=a_row==a_end;
306: if(a_break || b_break)
307: break;
308:
309: if(!a_countdown) {
310: a_chunk=a_row->link;
311: a_row=a_chunk->rows;
312: a_countdown=a_chunk->count;
1.9 paf 313: }
314: }
315: return a_break==b_break;
1.5 paf 316: }
E-mail: