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