Annotation of parser3/src/main/pa_string.C, revision 1.12
1.4 paf 1: /*
1.12 ! paf 2: $Id: pa_string.C,v 1.11 2001/01/27 15:21:05 paf Exp $
1.4 paf 3: */
4:
1.1 paf 5: #include <string.h>
6:
1.12 ! paf 7: #include "pa_string.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.10 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.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) {
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.10 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: }
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;
1.11 paf 162: int a_countdown=a_chunk->count;
163: int b_countdown=b_chunk->count;
1.9 paf 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;
1.11 paf 174: a_row++; a_countdown--; a_offset=0;
175: b_row++; b_countdown--; b_offset=0;
1.9 paf 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;
1.11 paf 180: b_row++; b_countdown--; b_offset=0;
1.9 paf 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;
1.11 paf 185: a_row++; a_countdown--; a_offset=0;
1.9 paf 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:
1.11 paf 193: if(!a_countdown) {
1.9 paf 194: a_chunk=a_row->link;
195: a_row=a_chunk->rows;
1.11 paf 196: a_countdown=a_chunk->count;
1.9 paf 197: }
1.11 paf 198: if(!b_countdown) {
1.9 paf 199: b_chunk=b_row->link;
200: b_row=b_chunk->rows;
1.11 paf 201: b_countdown=b_chunk->count;
1.9 paf 202: }
203: }
204: return a_break==b_break;
1.5 paf 205: }
E-mail: