Annotation of parser3/src/main/pa_string.C, revision 1.26
1.4 paf 1: /*
1.26 ! paf 2: $Id: pa_string.C,v 1.25 2001/02/14 15:19:02 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.26 ! 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.26 ! 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.26 ! 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;
1.26 ! paf 100: if(!size)
! 101: size=strlen(src);
! 102: if(!size)
1.9 paf 103: return *this;
104:
1.1 paf 105: if(chunk_is_full())
106: expand();
107:
108: append_here->item.ptr=src;
1.26 ! paf 109: fsize+=append_here->item.size=size;
1.13 paf 110: #ifndef NO_STRING_ORIGIN
1.14 paf 111: append_here->item.origin.file=file;
112: append_here->item.origin.line=line;
1.13 paf 113: #endif
1.8 paf 114: append_here++; fused_rows++;
1.1 paf 115:
116: return *this;
117: }
118:
1.16 paf 119: char *String::cstr() const {
1.26 ! paf 120: char *result=static_cast<char *>(pool().malloc(size()+1));
1.1 paf 121:
122: char *copy_here=result;
1.16 paf 123: const Chunk *chunk=&head;
1.2 paf 124: do {
1.16 paf 125: const Chunk::Row *row=chunk->rows;
1.2 paf 126: for(int i=0; i<chunk->count; i++) {
1.1 paf 127: if(row==append_here)
128: goto break2;
129:
130: memcpy(copy_here, row->item.ptr, row->item.size);
131: copy_here+=row->item.size;
132: row++;
133: }
1.2 paf 134: chunk=row->link;
135: } while(chunk);
1.1 paf 136: break2:
137: *copy_here=0;
138: return result;
139: }
140:
1.16 paf 141: uint String::hash_code() const {
1.7 paf 142: uint result=0;
1.5 paf 143:
1.16 paf 144: const Chunk *chunk=&head;
1.5 paf 145: do {
1.16 paf 146: const Chunk::Row *row=chunk->rows;
1.5 paf 147: for(int i=0; i<chunk->count; i++) {
148: if(row==append_here)
149: goto break2;
150:
1.6 paf 151: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 152: row++;
153: }
154: chunk=row->link;
155: } while(chunk);
156: break2:
157: return result;
158: }
159:
1.16 paf 160: bool String::operator == (const String& src) const {
1.8 paf 161: if(size() != src.size())
162: return false;
163:
1.16 paf 164: const Chunk *a_chunk=&head;
165: const Chunk *b_chunk=&src.head;
166: const Chunk::Row *a_row=a_chunk->rows;
167: const Chunk::Row *b_row=b_chunk->rows;
1.9 paf 168: int a_offset=0;
169: int b_offset=0;
170: Chunk::Row *a_end=append_here;
171: Chunk::Row *b_end=src.append_here;
1.11 paf 172: int a_countdown=a_chunk->count;
173: int b_countdown=b_chunk->count;
1.9 paf 174: bool a_break=false;
175: bool b_break=false;
176: while(true) {
177: int size_diff=
178: (a_row->item.size-a_offset)-
179: (b_row->item.size-b_offset);
180:
181: if(size_diff==0) { // a has same size as b
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;
1.11 paf 184: a_row++; a_countdown--; a_offset=0;
185: b_row++; b_countdown--; b_offset=0;
1.9 paf 186: } else if (size_diff>0) { // a longer
187: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset)!=0)
188: return false;
189: a_offset+=b_row->item.size-b_offset;
1.11 paf 190: b_row++; b_countdown--; b_offset=0;
1.9 paf 191: } else { // b longer
192: if(memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset)!=0)
193: return false;
194: b_offset+=a_row->item.size-a_offset;
1.11 paf 195: a_row++; a_countdown--; a_offset=0;
1.9 paf 196: }
197:
198: a_break=a_row==a_end;
199: b_break=b_row==b_end;
200: if(a_break || b_break)
201: break;
202:
1.11 paf 203: if(!a_countdown) {
1.9 paf 204: a_chunk=a_row->link;
205: a_row=a_chunk->rows;
1.11 paf 206: a_countdown=a_chunk->count;
1.9 paf 207: }
1.11 paf 208: if(!b_countdown) {
1.9 paf 209: b_chunk=b_row->link;
210: b_row=b_chunk->rows;
1.11 paf 211: b_countdown=b_chunk->count;
1.9 paf 212: }
213: }
214: return a_break==b_break;
1.5 paf 215: }
1.26 ! paf 216: /*
1.18 paf 217: String& String::append(const String_iterator& begin, const String_iterator& end) {
1.22 paf 218: //TODO
219: return *this;
1.18 paf 220: }
221:
1.21 paf 222: // Char_types
1.18 paf 223:
1.21 paf 224: Char_types::Char_types() {
225: memset(types, 0, sizeof(types));
1.23 paf 226: }
227:
228: void Char_types::set(char from, char to, int type) {
1.24 paf 229: memset(&types[static_cast<unsigned int>(from)], type, to-from+1);
1.18 paf 230: }
231:
232: // String_iterator
1.19 paf 233:
1.21 paf 234: String_iterator::String_iterator(String& astring) : string(astring) {
235: read_here=string.head.rows;
236: position=string.size()==0?0:read_here->item.ptr;
1.22 paf 237: link_row=reinterpret_cast<String::Chunk::Row*>(string.head.preallocated_link);
1.25 paf 238: }
1.26 ! paf 239: /*
1.25 paf 240: String_iterator::String_iterator(String_iterator& asi) {
241: //TODO
1.19 paf 242: }
243:
1.22 paf 244: char String_iterator::operator()() const {
1.21 paf 245: return position?*position:0;
1.19 paf 246: }
247:
248: void String_iterator::skip() {
1.21 paf 249: if(!position)
1.19 paf 250: return;
251:
1.21 paf 252: if(++position==
253: read_here->item.ptr+
254: read_here->item.size) {
255:
256: // next row
1.19 paf 257: if(++read_here==string.append_here) {
1.22 paf 258: position=0;
1.19 paf 259: return;
260: }
261: if(read_here==link_row) {
1.22 paf 262: String::Chunk *chunk=link_row->link;
1.19 paf 263: if(!chunk)
1.26 ! paf 264: string.pool().exception().raise(0, 0,
1.22 paf 265: &string,
1.19 paf 266: "String_iterator::skip() missed "
267: "read_here==string.append_here check");
268:
269: read_here=chunk->rows;
1.22 paf 270: link_row=&chunk->rows[chunk->count];
1.19 paf 271: }
1.21 paf 272: position=read_here->item.ptr;
1.19 paf 273: }
274: }
275:
276: bool String_iterator::skip_to(char c) {
1.21 paf 277: if(!position)
278: return false;
279:
280: while(true) {
281: if(char *found=static_cast<char *>(
1.22 paf 282: memchr(position, c, read_here->item.size-(position-read_here->item.ptr)))) {
1.21 paf 283: position=found;
1.19 paf 284: return true;
1.20 paf 285: }
286:
1.21 paf 287: // next row
1.20 paf 288: if(++read_here==string.append_here) {
1.21 paf 289: position=0;
1.20 paf 290: return false;
291: }
292: if(read_here==link_row) {
1.22 paf 293: String::Chunk *chunk=link_row->link;
1.20 paf 294: if(!chunk)
1.26 ! paf 295: string.pool().exception().raise(0, 0,
1.22 paf 296: &string,
1.20 paf 297: "String_iterator::skip_to(char) missed "
298: "read_here==string.append_here check");
299:
300: read_here=chunk->rows;
1.22 paf 301: link_row=&chunk->rows[chunk->count];
1.20 paf 302: }
1.21 paf 303: position=read_here->item.ptr;
1.20 paf 304: }
1.19 paf 305: }
306:
1.22 paf 307: int String_iterator::skip_to(Char_types& types) {
1.21 paf 308: if(!position)
309: return false;
310:
311: while(true) {
1.22 paf 312: int countdown=read_here->item.size-(position-read_here->item.ptr);
1.21 paf 313: for(; countdown--; position++)
314: if(int type=types.get(*position))
315: return type;
316:
317: // next row
318: if(++read_here==string.append_here) {
319: position=0;
1.22 paf 320: return -1;
1.21 paf 321: }
322: if(read_here==link_row) {
1.22 paf 323: String::Chunk *chunk=link_row->link;
1.21 paf 324: if(!chunk)
1.26 ! paf 325: string.pool().exception().raise(0, 0,
1.22 paf 326: &string,
1.21 paf 327: "String_iterator::skip_to(Char_type) missed "
328: "read_here==string.append_here check");
329:
330: read_here=chunk->rows;
1.22 paf 331: link_row=&chunk->rows[chunk->count];
1.21 paf 332: }
333: position=read_here->item.ptr;
334: }
1.19 paf 335: }
1.26 ! paf 336: */
E-mail: