Annotation of parser3/src/main/pa_string.C, revision 1.58
1.45 paf 1: /** @file
1.55 paf 2: Parser: string class. @see untasize_t.C.
1.46 paf 3:
1.36 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.46 paf 5:
1.37 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.36 paf 7:
1.58 ! paf 8: $Id: pa_string.C,v 1.57 2001/03/29 17:23:22 paf Exp $
1.4 paf 9: */
10:
1.48 paf 11: #include "pa_config_includes.h"
1.1 paf 12:
1.13 paf 13: #include "pa_pool.h"
1.12 paf 14: #include "pa_string.h"
1.5 paf 15: #include "pa_hash.h"
1.22 paf 16: #include "pa_exception.h"
1.53 paf 17: #include "pa_common.h"
1.1 paf 18:
1.18 paf 19: // String
20:
1.55 paf 21: String::String(Pool& apool, const char *src, bool tasize_ted) :
1.17 paf 22: Pooled(apool) {
1.28 paf 23: last_chunk=&head;
24: head.count=CR_PREALLOCATED_COUNT;
1.5 paf 25: append_here=head.rows;
1.2 paf 26: head.preallocated_link=0;
1.28 paf 27: link_row=&head.rows[head.count];
1.8 paf 28: fused_rows=fsize=0;
1.41 paf 29:
30: if(src)
1.55 paf 31: if(tasize_ted)
1.41 paf 32: APPEND_TAINTED(src, 0, 0, 0);
33: else
1.53 paf 34: APPEND_CONST(src);
1.1 paf 35: }
36:
37: void String::expand() {
1.55 paf 38: size_t new_chunk_count=last_chunk->count+last_chunk->count*CR_GROW_PERCENT/100;
1.28 paf 39: last_chunk=static_cast<Chunk *>(
1.55 paf 40: malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
1.28 paf 41: last_chunk->count=new_chunk_count;
42: link_row->link=last_chunk;
43: append_here=last_chunk->rows;
44: link_row=&last_chunk->rows[last_chunk->count];
1.8 paf 45: link_row->link=0;
1.1 paf 46: }
47:
1.40 paf 48: String::String(const String& src) : Pooled(src.pool()) {
1.8 paf 49: head.count=CR_PREALLOCATED_COUNT;
50:
1.55 paf 51: size_t src_used_rows=src.fused_rows;
1.8 paf 52: if(src_used_rows<=head.count) {
1.55 paf 53: // all new rows fit size_to preallocated area
54: size_t curr_chunk_rows=head.count;
1.8 paf 55: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
56: append_here=&head.rows[src_used_rows];
57: link_row=&head.rows[curr_chunk_rows];
58: } else {
59: // warning:
1.10 paf 60: // heavily relies on the fact
61: // "preallocated area is the same for all strings"
1.8 paf 62: //
63: // info:
64: // allocating only enough mem to fit src string rows
65: // next append would allocate a new chunk
66: //
1.55 paf 67: // new rows don't fit size_to preallocated area: splitting size_to two chunks
1.8 paf 68: // preallocated chunk src to constructing head
69: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
1.55 paf 70: // remaining rows size_to new_chunk
71: size_t curr_chunk_rows=src_used_rows-head.count;
1.8 paf 72: Chunk *new_chunk=static_cast<Chunk *>(
1.55 paf 73: malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.8 paf 74: new_chunk->count=curr_chunk_rows;
75: head.preallocated_link=new_chunk;
1.28 paf 76: append_here=link_row=&new_chunk->rows[new_chunk->count];
1.8 paf 77:
78: Chunk *old_chunk=src.head.preallocated_link;
79: Chunk::Row *new_rows=new_chunk->rows;
1.55 paf 80: size_t rows_left_to_copy=new_chunk->count;
1.8 paf 81: while(true) {
1.55 paf 82: size_t old_count=old_chunk->count;
1.8 paf 83: Chunk *next_chunk=old_chunk->rows[old_count].link;
84: if(next_chunk) {
85: // not last source chunk
86: // taking it all
87: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
88: new_rows+=old_count;
89: rows_left_to_copy-=old_count;
90:
91: old_chunk=next_chunk;
92: } else {
93: // the last source chunk
94: // taking only those rows of chunk that _left_to_copy
95: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
96: break;
97: }
98: }
1.5 paf 99: }
1.8 paf 100: link_row->link=0;
101: fused_rows=src_used_rows;
102: fsize=src.fsize;
1.5 paf 103: }
1.28 paf 104:
1.42 paf 105: String& String::append(const String& src, Untaint_lang lang, bool forced) {
1.55 paf 106: size_t src_used_rows=src.fused_rows;
107: size_t dst_free_rows=link_row-append_here;
1.28 paf 108:
109: if(src_used_rows<=dst_free_rows) {
1.55 paf 110: // all new rows fit size_to last chunk
1.28 paf 111: memcpy(append_here, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
1.42 paf 112: set_lang(append_here, lang, forced, src_used_rows);
1.28 paf 113: append_here+=src_used_rows;
114: } else {
1.55 paf 115: // not all new rows fit size_to last chunk: shrinking it to used part,
116: size_t used_rows=last_chunk->count-dst_free_rows;
117: //size_t *countp=append_here
1.28 paf 118: link_row=&last_chunk->rows[last_chunk->count=used_rows];
119: // allocating only enough mem to fit src string rows
120: // next append would allocate a new chunk
121: last_chunk=static_cast<Chunk *>(
1.55 paf 122: malloc(sizeof(size_t)+sizeof(Chunk::Row)*src_used_rows+sizeof(Chunk *)));
1.28 paf 123: last_chunk->count=src_used_rows;
124: link_row->link=last_chunk;
125: append_here=link_row=&last_chunk->rows[src_used_rows];
126:
1.31 paf 127: const Chunk *old_chunk=&src.head;
1.28 paf 128: Chunk::Row *new_rows=last_chunk->rows;
1.55 paf 129: size_t rows_left_to_copy=src_used_rows;
1.28 paf 130: while(true) {
1.55 paf 131: size_t old_count=old_chunk->count;
1.28 paf 132: Chunk *next_chunk=old_chunk->rows[old_count].link;
133: if(next_chunk) {
134: // not last source chunk
135: // taking it all
136: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
1.42 paf 137: set_lang(new_rows, lang, forced, old_count);
1.28 paf 138: new_rows+=old_count;
139: rows_left_to_copy-=old_count;
140:
141: old_chunk=next_chunk;
142: } else {
143: // the last source chunk
144: // taking only those rows of chunk that _left_to_copy
145: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
1.42 paf 146: set_lang(new_rows, lang, forced, rows_left_to_copy);
1.28 paf 147: break;
148: }
149: }
1.29 paf 150: link_row->link=0;
1.28 paf 151: }
152: fused_rows+=src_used_rows;
153: fsize+=src.fsize;
154:
155: return *this;
1.23 paf 156: }
1.42 paf 157: void String::set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size) {
1.47 paf 158: if(lang==UL_PASS_APPENDED)
1.34 paf 159: return;
160:
161: while(size--) {
162: Untaint_lang& item_lang=(row++)->item.lang;
1.55 paf 163: if(item_lang==UL_YES || forced) // tasize_ted? need untasize_t language assignment
164: item_lang=lang; // assign untasize_t language
1.34 paf 165: }
1.40 paf 166: }
167:
1.42 paf 168: /*void String::change_lang(Untaint_lang lang) {
1.40 paf 169: Chunk *chunk=&head;
170: do {
171: Chunk::Row *row=chunk->rows;
1.55 paf 172: for(size_t i=0; i<chunk->count; i++) {
1.40 paf 173: if(row==append_here)
174: goto break2;
175:
176: row->item.lang=lang;
177: row++;
178: }
179: chunk=row->link;
180: } while(chunk);
181: break2:
182: return;
1.34 paf 183: }
1.42 paf 184: */
1.13 paf 185: String& String::real_append(STRING_APPEND_PARAMS) {
1.9 paf 186: if(!src)
187: return *this;
1.26 paf 188: if(!size)
189: size=strlen(src);
190: if(!size)
1.9 paf 191: return *this;
192:
1.1 paf 193: if(chunk_is_full())
194: expand();
195:
196: append_here->item.ptr=src;
1.26 paf 197: fsize+=append_here->item.size=size;
1.52 paf 198: append_here->item.lang=lang;
1.13 paf 199: #ifndef NO_STRING_ORIGIN
1.14 paf 200: append_here->item.origin.file=file;
201: append_here->item.origin.line=line;
1.13 paf 202: #endif
1.8 paf 203: append_here++; fused_rows++;
1.1 paf 204:
205: return *this;
206: }
207:
1.16 paf 208: uint String::hash_code() const {
1.7 paf 209: uint result=0;
1.5 paf 210:
1.16 paf 211: const Chunk *chunk=&head;
1.5 paf 212: do {
1.16 paf 213: const Chunk::Row *row=chunk->rows;
1.55 paf 214: for(size_t i=0; i<chunk->count; i++) {
1.5 paf 215: if(row==append_here)
216: goto break2;
217:
1.6 paf 218: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 219: row++;
220: }
221: chunk=row->link;
222: } while(chunk);
223: break2:
224: return result;
225: }
226:
1.55 paf 227: int String::cmp(int& partial, const String& src, size_t this_offset) const {
228: this_offset=min(this_offset, size()-1);
229:
1.16 paf 230: const Chunk *a_chunk=&head;
231: const Chunk *b_chunk=&src.head;
232: const Chunk::Row *a_row=a_chunk->rows;
233: const Chunk::Row *b_row=b_chunk->rows;
1.55 paf 234: size_t a_offset=this_offset;
235: size_t b_offset=0;
1.9 paf 236: Chunk::Row *a_end=append_here;
237: Chunk::Row *b_end=src.append_here;
1.55 paf 238: size_t a_countdown=a_chunk->count;
239: size_t b_countdown=b_chunk->count;
1.9 paf 240: bool a_break=false;
241: bool b_break=false;
1.55 paf 242: size_t result;
243: for(size_t pos=0; true; pos+=a_row->item.size) {
1.33 paf 244: a_break=a_row==a_end;
245: b_break=b_row==b_end;
246: if(a_break || b_break)
247: break;
248:
1.55 paf 249: if(pos+a_row->item.size > this_offset) {
250: int size_diff=
251: (a_row->item.size-a_offset)-
252: (b_row->item.size-b_offset);
253:
254: if(size_diff==0) { // a has same size as b
255: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
256: if(result)
257: return result;
258: a_row++; a_countdown--; a_offset=0;
259: b_row++; b_countdown--; b_offset=0;
260: } else if (size_diff>0) { // a longer
261: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset);
262: if(result)
263: return result;
264: a_offset+=b_row->item.size-b_offset;
265: b_row++; b_countdown--; b_offset=0;
266: } else { // b longer
267: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
268: if(result)
269: return result;
270: b_offset+=a_row->item.size-a_offset;
271: a_row++; a_countdown--; a_offset=0;
272: }
1.9 paf 273:
1.55 paf 274: if(!b_countdown) {
275: b_chunk=b_row->link;
276: b_row=b_chunk->rows;
277: b_countdown=b_chunk->count;
278: }
279: } else {
280: a_row++; a_countdown--; a_offset-=a_row->item.size;
1.9 paf 281: }
282:
1.11 paf 283: if(!a_countdown) {
1.9 paf 284: a_chunk=a_row->link;
285: a_row=a_chunk->rows;
1.11 paf 286: a_countdown=a_chunk->count;
1.9 paf 287: }
1.27 paf 288: }
1.55 paf 289: if(a_break==b_break) { // ended simultaneously
290: partial=0; return 0;
291: } else if(a_break) { // first bytes equal, but a ended before b
292: partial=1; return -1;
293: } else {
294: partial=2; return +1;
295: }
1.27 paf 296: }
297:
1.58 ! paf 298: /// @todo now fix like prev one. table:append doent not work because of this
1.55 paf 299: int String::cmp(int& partial, const char* b_ptr, size_t src_size) const {
1.50 paf 300: size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.27 paf 301:
1.51 paf 302: partial=-1;
1.27 paf 303: const Chunk *a_chunk=&head;
304: const Chunk::Row *a_row=a_chunk->rows;
1.55 paf 305: size_t a_offset=0;
306: size_t b_offset=0;
1.27 paf 307: Chunk::Row *a_end=append_here;
1.55 paf 308: size_t a_countdown=a_chunk->count;
1.27 paf 309: bool a_break=false;
310: bool b_break=false;
311: while(true) {
1.52 paf 312: a_break=a_row==a_end;
313: if(a_break || b_break)
314: break;
315:
1.27 paf 316: int size_diff=
317: (a_row->item.size-a_offset)-
318: (b_size-b_offset);
319:
320: if(size_diff==0) { // a has same size as b
1.55 paf 321: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
1.50 paf 322: return result;
1.27 paf 323: a_row++; a_countdown--; a_offset=0;
324: b_break=true;
325: } else if (size_diff>0) { // a longer
1.55 paf 326: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
1.50 paf 327: return result;
1.27 paf 328: a_offset+=b_size-b_offset;
329: b_break=true;
330: } else { // b longer
1.55 paf 331: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
1.50 paf 332: return result;
1.27 paf 333: b_offset+=a_row->item.size-a_offset;
334: a_row++; a_countdown--; a_offset=0;
335: }
336:
337: if(!a_countdown) {
338: a_chunk=a_row->link;
339: a_row=a_chunk->rows;
340: a_countdown=a_chunk->count;
1.9 paf 341: }
342: }
1.55 paf 343: if(a_break==b_break) { // ended simultaneously
344: partial=0; return 0;
345: } else if(a_break) { // first bytes equal, but a ended before b
346: partial=1; return -1;
347: } else {
348: partial=2; return +1;
349: }
1.5 paf 350: }
1.46 paf 351:
352: #ifndef NO_STRING_ORIGIN
353: const Origin& String::origin() const {
354: if(!fused_rows)
355: THROW(0, 0,
1.50 paf 356: 0,
357: "String::origin() of empty string called");
1.46 paf 358:
1.49 paf 359: // determining origin by last appended piece
1.50 paf 360: // because first one frequently constant.
361: // ex: ^load[/file] "document_root" + "/file"
1.49 paf 362: return append_here[-1].item.origin;
1.46 paf 363: }
364: #endif
1.53 paf 365:
366: String& String::piece(size_t start, size_t finish) const {
367: start=max(0, start);
368: finish=min(size(), finish);
369:
370: String& result=*NEW String(pool());
371:
372: size_t pos=0;
373: const Chunk *chunk=&head;
374: do {
375: const Chunk::Row *row=chunk->rows;
1.55 paf 376: for(size_t i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53 paf 377: if(row==append_here)
378: goto break2;
379:
380: if(start>=pos) { // started now or already?
381: size_t item_finish=pos+row->item.size;
382: bool started=start < item_finish; // started now?
383: bool finished=finish < item_finish; // finished now?
384: size_t offset=started?start-pos:0;
385: size_t size=finished?finish-pos:row->item.size;
386: result.APPEND(
387: row->item.ptr+offset, size-offset,
388: row->item.lang,
389: row->item.origin.file, row->item.origin.line);
390: if(finished)
391: goto break2;
392: }
393: }
394: chunk=row->link;
395: } while(chunk);
396: break2:
397: return result;
1.54 paf 398: }
399:
1.57 paf 400: int String::pos(const String& substr, size_t result) const {
1.58 ! paf 401: for(; result<size(); result++) {
! 402: int partial; cmp(partial, substr, result);
! 403: if(
! 404: partial==0 || // full match
! 405: partial==2) // 'substr' starts 'this'+'result'
! 406: return result;
! 407: }
! 408:
! 409: return -1;
! 410: }
! 411:
! 412: int String::pos(const char *substr, size_t result) const {
1.57 paf 413: for(; result<size(); result++) {
1.55 paf 414: int partial; cmp(partial, substr, result);
415: if(
416: partial==0 || // full match
417: partial==2) // 'substr' starts 'this'+'result'
418: return result;
419: }
420:
421: return -1;
1.53 paf 422: }
E-mail: