Annotation of parser3/src/main/pa_string.C, revision 1.59
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.59 ! paf 8: $Id: pa_string.C,v 1.58 2001/03/30 09:58:59 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 {
1.59 ! paf 228: partial=-1;
1.55 paf 229: this_offset=min(this_offset, size()-1);
230:
1.16 paf 231: const Chunk *a_chunk=&head;
232: const Chunk *b_chunk=&src.head;
233: const Chunk::Row *a_row=a_chunk->rows;
234: const Chunk::Row *b_row=b_chunk->rows;
1.55 paf 235: size_t a_offset=this_offset;
236: size_t b_offset=0;
1.9 paf 237: Chunk::Row *a_end=append_here;
238: Chunk::Row *b_end=src.append_here;
1.55 paf 239: size_t a_countdown=a_chunk->count;
240: size_t b_countdown=b_chunk->count;
1.9 paf 241: bool a_break=false;
242: bool b_break=false;
1.55 paf 243: size_t result;
244: for(size_t pos=0; true; pos+=a_row->item.size) {
1.33 paf 245: a_break=a_row==a_end;
246: b_break=b_row==b_end;
247: if(a_break || b_break)
248: break;
249:
1.55 paf 250: if(pos+a_row->item.size > this_offset) {
251: int size_diff=
252: (a_row->item.size-a_offset)-
253: (b_row->item.size-b_offset);
254:
255: if(size_diff==0) { // a has same size as b
256: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
257: if(result)
258: return result;
259: a_row++; a_countdown--; a_offset=0;
260: b_row++; b_countdown--; b_offset=0;
261: } else if (size_diff>0) { // a longer
262: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset);
263: if(result)
264: return result;
265: a_offset+=b_row->item.size-b_offset;
266: b_row++; b_countdown--; b_offset=0;
267: } else { // b longer
268: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
269: if(result)
270: return result;
271: b_offset+=a_row->item.size-a_offset;
272: a_row++; a_countdown--; a_offset=0;
273: }
1.9 paf 274:
1.55 paf 275: if(!b_countdown) {
276: b_chunk=b_row->link;
277: b_row=b_chunk->rows;
278: b_countdown=b_chunk->count;
279: }
280: } else {
281: a_row++; a_countdown--; a_offset-=a_row->item.size;
1.9 paf 282: }
283:
1.11 paf 284: if(!a_countdown) {
1.9 paf 285: a_chunk=a_row->link;
286: a_row=a_chunk->rows;
1.11 paf 287: a_countdown=a_chunk->count;
1.9 paf 288: }
1.27 paf 289: }
1.55 paf 290: if(a_break==b_break) { // ended simultaneously
291: partial=0; return 0;
292: } else if(a_break) { // first bytes equal, but a ended before b
293: partial=1; return -1;
294: } else {
295: partial=2; return +1;
296: }
1.27 paf 297: }
298:
1.59 ! paf 299: int String::cmp(int& partial, const char* b_ptr, size_t src_size,
! 300: size_t this_offset) const {
! 301: partial=-1;
1.50 paf 302: size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.59 ! paf 303: this_offset=min(this_offset, size()-1);
1.27 paf 304:
305: const Chunk *a_chunk=&head;
306: const Chunk::Row *a_row=a_chunk->rows;
1.59 ! paf 307: size_t a_offset=this_offset;
1.55 paf 308: size_t b_offset=0;
1.27 paf 309: Chunk::Row *a_end=append_here;
1.55 paf 310: size_t a_countdown=a_chunk->count;
1.27 paf 311: bool a_break=false;
312: bool b_break=false;
1.59 ! paf 313: for(size_t pos=0; true; pos+=a_row->item.size) {
1.52 paf 314: a_break=a_row==a_end;
315: if(a_break || b_break)
316: break;
317:
1.59 ! paf 318: if(pos+a_row->item.size > this_offset) {
! 319: int size_diff=
! 320: (a_row->item.size-a_offset)-
! 321: (b_size-b_offset);
! 322:
! 323: if(size_diff==0) { // a has same size as b
! 324: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
! 325: a_row->item.size-a_offset)!=0)
! 326: return result;
! 327: a_row++; a_countdown--; a_offset=0;
! 328: b_break=true;
! 329: } else if (size_diff>0) { // a longer
! 330: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
! 331: b_size-b_offset)!=0)
! 332: return result;
! 333: a_offset+=b_size-b_offset;
! 334: b_break=true;
! 335: } else { // b longer
! 336: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
! 337: a_row->item.size-a_offset)!=0)
! 338: return result;
! 339: b_offset+=a_row->item.size-a_offset;
! 340: a_row++; a_countdown--; a_offset=0;
! 341: }
! 342: } else {
! 343: a_row++; a_countdown--; a_offset-=a_row->item.size;
1.27 paf 344: }
345:
346: if(!a_countdown) {
347: a_chunk=a_row->link;
348: a_row=a_chunk->rows;
349: a_countdown=a_chunk->count;
1.9 paf 350: }
351: }
1.55 paf 352: if(a_break==b_break) { // ended simultaneously
353: partial=0; return 0;
354: } else if(a_break) { // first bytes equal, but a ended before b
355: partial=1; return -1;
356: } else {
357: partial=2; return +1;
358: }
1.5 paf 359: }
1.46 paf 360:
361: #ifndef NO_STRING_ORIGIN
362: const Origin& String::origin() const {
363: if(!fused_rows)
364: THROW(0, 0,
1.50 paf 365: 0,
366: "String::origin() of empty string called");
1.46 paf 367:
1.49 paf 368: // determining origin by last appended piece
1.50 paf 369: // because first one frequently constant.
370: // ex: ^load[/file] "document_root" + "/file"
1.49 paf 371: return append_here[-1].item.origin;
1.46 paf 372: }
373: #endif
1.53 paf 374:
375: String& String::piece(size_t start, size_t finish) const {
376: start=max(0, start);
377: finish=min(size(), finish);
378:
379: String& result=*NEW String(pool());
380:
381: size_t pos=0;
382: const Chunk *chunk=&head;
383: do {
384: const Chunk::Row *row=chunk->rows;
1.55 paf 385: for(size_t i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53 paf 386: if(row==append_here)
387: goto break2;
388:
389: if(start>=pos) { // started now or already?
390: size_t item_finish=pos+row->item.size;
391: bool started=start < item_finish; // started now?
392: bool finished=finish < item_finish; // finished now?
393: size_t offset=started?start-pos:0;
394: size_t size=finished?finish-pos:row->item.size;
395: result.APPEND(
396: row->item.ptr+offset, size-offset,
397: row->item.lang,
398: row->item.origin.file, row->item.origin.line);
399: if(finished)
400: goto break2;
401: }
402: }
403: chunk=row->link;
404: } while(chunk);
405: break2:
406: return result;
1.54 paf 407: }
408:
1.57 paf 409: int String::pos(const String& substr, size_t result) const {
1.58 paf 410: for(; result<size(); result++) {
411: int partial; cmp(partial, substr, result);
412: if(
413: partial==0 || // full match
414: partial==2) // 'substr' starts 'this'+'result'
415: return result;
416: }
417:
418: return -1;
419: }
420:
421: int String::pos(const char *substr, size_t result) const {
1.57 paf 422: for(; result<size(); result++) {
1.59 ! paf 423: int partial; cmp(partial, substr, 0, result);
1.55 paf 424: if(
425: partial==0 || // full match
426: partial==2) // 'substr' starts 'this'+'result'
427: return result;
428: }
429:
430: return -1;
1.53 paf 431: }
E-mail: