Annotation of parser3/src/main/pa_string.C, revision 1.27
1.4 paf 1: /*
1.27 ! paf 2: $Id: pa_string.C,v 1.26 2001/02/20 18:45:53 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.27 ! paf 212: }
! 213: }
! 214: return a_break==b_break;
! 215: }
! 216:
! 217: bool String::operator == (char* b_ptr) const {
! 218: size_t b_size=b_ptr?strlen(b_ptr):0;
! 219: if(size() != b_size)
! 220: return false;
! 221:
! 222: const Chunk *a_chunk=&head;
! 223: const Chunk::Row *a_row=a_chunk->rows;
! 224: int a_offset=0;
! 225: int b_offset=0;
! 226: Chunk::Row *a_end=append_here;
! 227: int a_countdown=a_chunk->count;
! 228: bool a_break=false;
! 229: bool b_break=false;
! 230: while(true) {
! 231: int size_diff=
! 232: (a_row->item.size-a_offset)-
! 233: (b_size-b_offset);
! 234:
! 235: if(size_diff==0) { // a has same size as b
! 236: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
! 237: return false;
! 238: a_row++; a_countdown--; a_offset=0;
! 239: b_break=true;
! 240: } else if (size_diff>0) { // a longer
! 241: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
! 242: return false;
! 243: a_offset+=b_size-b_offset;
! 244: b_break=true;
! 245: } else { // b longer
! 246: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
! 247: return false;
! 248: b_offset+=a_row->item.size-a_offset;
! 249: a_row++; a_countdown--; a_offset=0;
! 250: }
! 251:
! 252: a_break=a_row==a_end;
! 253: if(a_break || b_break)
! 254: break;
! 255:
! 256: if(!a_countdown) {
! 257: a_chunk=a_row->link;
! 258: a_row=a_chunk->rows;
! 259: a_countdown=a_chunk->count;
1.9 paf 260: }
261: }
262: return a_break==b_break;
1.5 paf 263: }
1.26 paf 264: /*
1.18 paf 265: String& String::append(const String_iterator& begin, const String_iterator& end) {
1.22 paf 266: //TODO
267: return *this;
1.18 paf 268: }
269:
1.21 paf 270: // Char_types
1.18 paf 271:
1.21 paf 272: Char_types::Char_types() {
273: memset(types, 0, sizeof(types));
1.23 paf 274: }
275:
276: void Char_types::set(char from, char to, int type) {
1.24 paf 277: memset(&types[static_cast<unsigned int>(from)], type, to-from+1);
1.18 paf 278: }
279:
280: // String_iterator
1.19 paf 281:
1.21 paf 282: String_iterator::String_iterator(String& astring) : string(astring) {
283: read_here=string.head.rows;
284: position=string.size()==0?0:read_here->item.ptr;
1.22 paf 285: link_row=reinterpret_cast<String::Chunk::Row*>(string.head.preallocated_link);
1.25 paf 286: }
1.26 paf 287: /*
1.25 paf 288: String_iterator::String_iterator(String_iterator& asi) {
289: //TODO
1.19 paf 290: }
291:
1.22 paf 292: char String_iterator::operator()() const {
1.21 paf 293: return position?*position:0;
1.19 paf 294: }
295:
296: void String_iterator::skip() {
1.21 paf 297: if(!position)
1.19 paf 298: return;
299:
1.21 paf 300: if(++position==
301: read_here->item.ptr+
302: read_here->item.size) {
303:
304: // next row
1.19 paf 305: if(++read_here==string.append_here) {
1.22 paf 306: position=0;
1.19 paf 307: return;
308: }
309: if(read_here==link_row) {
1.22 paf 310: String::Chunk *chunk=link_row->link;
1.19 paf 311: if(!chunk)
1.26 paf 312: string.pool().exception().raise(0, 0,
1.22 paf 313: &string,
1.19 paf 314: "String_iterator::skip() missed "
315: "read_here==string.append_here check");
316:
317: read_here=chunk->rows;
1.22 paf 318: link_row=&chunk->rows[chunk->count];
1.19 paf 319: }
1.21 paf 320: position=read_here->item.ptr;
1.19 paf 321: }
322: }
323:
324: bool String_iterator::skip_to(char c) {
1.21 paf 325: if(!position)
326: return false;
327:
328: while(true) {
329: if(char *found=static_cast<char *>(
1.22 paf 330: memchr(position, c, read_here->item.size-(position-read_here->item.ptr)))) {
1.21 paf 331: position=found;
1.19 paf 332: return true;
1.20 paf 333: }
334:
1.21 paf 335: // next row
1.20 paf 336: if(++read_here==string.append_here) {
1.21 paf 337: position=0;
1.20 paf 338: return false;
339: }
340: if(read_here==link_row) {
1.22 paf 341: String::Chunk *chunk=link_row->link;
1.20 paf 342: if(!chunk)
1.26 paf 343: string.pool().exception().raise(0, 0,
1.22 paf 344: &string,
1.20 paf 345: "String_iterator::skip_to(char) missed "
346: "read_here==string.append_here check");
347:
348: read_here=chunk->rows;
1.22 paf 349: link_row=&chunk->rows[chunk->count];
1.20 paf 350: }
1.21 paf 351: position=read_here->item.ptr;
1.20 paf 352: }
1.19 paf 353: }
354:
1.22 paf 355: int String_iterator::skip_to(Char_types& types) {
1.21 paf 356: if(!position)
357: return false;
358:
359: while(true) {
1.22 paf 360: int countdown=read_here->item.size-(position-read_here->item.ptr);
1.21 paf 361: for(; countdown--; position++)
362: if(int type=types.get(*position))
363: return type;
364:
365: // next row
366: if(++read_here==string.append_here) {
367: position=0;
1.22 paf 368: return -1;
1.21 paf 369: }
370: if(read_here==link_row) {
1.22 paf 371: String::Chunk *chunk=link_row->link;
1.21 paf 372: if(!chunk)
1.26 paf 373: string.pool().exception().raise(0, 0,
1.22 paf 374: &string,
1.21 paf 375: "String_iterator::skip_to(Char_type) missed "
376: "read_here==string.append_here check");
377:
378: read_here=chunk->rows;
1.22 paf 379: link_row=&chunk->rows[chunk->count];
1.21 paf 380: }
381: position=read_here->item.ptr;
382: }
1.19 paf 383: }
1.26 paf 384: */
E-mail: