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