Annotation of parser3/src/main/pa_string.C, revision 1.122
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.119 paf 5: Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.46 paf 6:
1.122 ! paf 7: $Id: pa_string.C,v 1.121 2001/11/16 13:58:28 paf Exp $
1.4 paf 8: */
9:
1.48 paf 10: #include "pa_config_includes.h"
1.1 paf 11:
1.70 paf 12: #include "pcre.h"
1.82 parser 13: #include "internal.h"
1.70 paf 14:
1.13 paf 15: #include "pa_pool.h"
1.12 paf 16: #include "pa_string.h"
1.5 paf 17: #include "pa_hash.h"
1.22 paf 18: #include "pa_exception.h"
1.53 paf 19: #include "pa_common.h"
1.60 paf 20: #include "pa_array.h"
21: #include "pa_globals.h"
1.61 paf 22: #include "pa_table.h"
1.101 parser 23: #include "pa_dictionary.h"
1.60 paf 24:
1.75 paf 25: String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
1.120 paf 26: Pooled(apool) {
1.28 paf 27: last_chunk=&head;
28: head.count=CR_PREALLOCATED_COUNT;
1.5 paf 29: append_here=head.rows;
1.2 paf 30: head.preallocated_link=0;
1.28 paf 31: link_row=&head.rows[head.count];
1.41 paf 32:
33: if(src)
1.75 paf 34: if(tainted)
35: APPEND_TAINTED(src, src_size, 0, 0);
1.41 paf 36: else
1.75 paf 37: APPEND_CLEAN(src, src_size, 0, 0);
1.1 paf 38: }
39:
1.94 parser 40: String::String(const String& src) :
1.120 paf 41: Pooled(src.pool()) {
1.8 paf 42: head.count=CR_PREALLOCATED_COUNT;
43:
1.116 paf 44: uint src_used_rows=src.used_rows();
1.8 paf 45: if(src_used_rows<=head.count) {
1.55 paf 46: // all new rows fit size_to preallocated area
1.98 parser 47: last_chunk=&head;
1.116 paf 48: uint curr_chunk_rows=head.count;
1.8 paf 49: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
50: append_here=&head.rows[src_used_rows];
51: link_row=&head.rows[curr_chunk_rows];
52: } else {
53: // warning:
1.10 paf 54: // heavily relies on the fact
55: // "preallocated area is the same for all strings"
1.8 paf 56: //
57: // info:
58: // allocating only enough mem to fit src string rows
59: // next append would allocate a new chunk
60: //
1.55 paf 61: // new rows don't fit size_to preallocated area: splitting size_to two chunks
1.8 paf 62: // preallocated chunk src to constructing head
63: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
1.55 paf 64: // remaining rows size_to new_chunk
1.116 paf 65: uint curr_chunk_rows=src_used_rows-head.count;
1.98 parser 66: last_chunk=static_cast<Chunk *>(
1.116 paf 67: malloc(sizeof(uint)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *), 9));
1.98 parser 68: last_chunk->count=curr_chunk_rows;
69: head.preallocated_link=last_chunk;
70: append_here=link_row=&last_chunk->rows[last_chunk->count];
1.8 paf 71:
72: Chunk *old_chunk=src.head.preallocated_link;
1.98 parser 73: Chunk::Row *new_rows=last_chunk->rows;
1.116 paf 74: uint rows_left_to_copy=last_chunk->count;
1.8 paf 75: while(true) {
1.116 paf 76: uint old_count=old_chunk->count;
1.8 paf 77: Chunk *next_chunk=old_chunk->rows[old_count].link;
78: if(next_chunk) {
79: // not last source chunk
80: // taking it all
81: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
82: new_rows+=old_count;
83: rows_left_to_copy-=old_count;
84:
85: old_chunk=next_chunk;
86: } else {
87: // the last source chunk
88: // taking only those rows of chunk that _left_to_copy
89: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
90: break;
91: }
92: }
1.5 paf 93: }
1.8 paf 94: link_row->link=0;
1.115 paf 95: src_used_rows;
1.120 paf 96: }
97:
98: size_t String::size() const {
99: size_t result=0;
100: const Chunk *chunk=&head;
101: do {
102: const Chunk::Row *row=chunk->rows;
103: for(uint i=0; i<chunk->count; i++, row++) {
104: if(row==append_here)
105: goto break2;
106:
107: result+=row->item.size;
108: }
109: chunk=row->link;
110: } while(chunk);
111:
112: break2:
113: return result;
1.94 parser 114: }
115:
1.115 paf 116: /// @todo not very optimal
117: uint String::used_rows() const {
118: uint result=0;
119: const Chunk *chunk=&head;
120: do {
121: const Chunk::Row *row=chunk->rows;
1.116 paf 122: for(uint i=0; i<chunk->count; i++, row++) {
1.115 paf 123: if(row==append_here)
124: goto break2;
125:
126: result++;
127: }
128: chunk=row->link;
129: } while(chunk);
130:
131: break2:
132: return result;
133: }
1.94 parser 134: void String::expand() {
1.116 paf 135: uint new_chunk_count=last_chunk->count+CR_GROW_COUNT;
1.122 ! paf 136: if(new_chunk_count>MAX_USHORT)
! 137: new_chunk_count=MAX_USHORT;
! 138:
1.94 parser 139: last_chunk=static_cast<Chunk *>(
1.116 paf 140: malloc(sizeof(uint)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *), 10));
1.94 parser 141: last_chunk->count=new_chunk_count;
142: link_row->link=last_chunk;
143: append_here=last_chunk->rows;
144: link_row=&last_chunk->rows[last_chunk->count];
145: link_row->link=0;
1.5 paf 146: }
1.28 paf 147:
1.42 paf 148: String& String::append(const String& src, Untaint_lang lang, bool forced) {
1.60 paf 149: const Chunk *chunk=&src.head;
1.40 paf 150: do {
1.60 paf 151: const Chunk::Row *row=chunk->rows;
1.116 paf 152: for(uint i=0; i<chunk->count; i++, row++) {
1.60 paf 153: if(row==src.append_here)
1.40 paf 154: goto break2;
1.60 paf 155:
156: APPEND(row->item.ptr, row->item.size,
1.117 paf 157: (lang!=UL_PASS_APPENDED && (row->item.lang==UL_TAINTED || forced))?lang:(Untaint_lang)row->item.lang,
1.60 paf 158: row->item.origin.file, row->item.origin.line);
1.40 paf 159: }
160: chunk=row->link;
161: } while(chunk);
162: break2:
1.60 paf 163: return *this;
1.34 paf 164: }
1.60 paf 165:
1.13 paf 166: String& String::real_append(STRING_APPEND_PARAMS) {
1.9 paf 167: if(!src)
168: return *this;
1.26 paf 169: if(!size)
170: size=strlen(src);
171: if(!size)
1.9 paf 172: return *this;
1.122 ! paf 173:
! 174: while(size>MAX_USHORT) {
! 175: if(chunk_is_full())
! 176: expand();
! 177:
! 178: append_here->item.ptr=src;
! 179: append_here->item.size=MAX_USHORT;
! 180: append_here->item.lang=lang;
! 181: #ifndef NO_STRING_ORIGIN
! 182: append_here->item.origin.file=file;
! 183: append_here->item.origin.line=line;
! 184: #endif
! 185: append_here++;
! 186:
! 187: src+=MAX_USHORT;
! 188: size-=MAX_USHORT;
! 189: }
1.9 paf 190:
1.1 paf 191: if(chunk_is_full())
192: expand();
193:
194: append_here->item.ptr=src;
1.121 paf 195: append_here->item.size=size;
1.52 paf 196: append_here->item.lang=lang;
1.13 paf 197: #ifndef NO_STRING_ORIGIN
1.14 paf 198: append_here->item.origin.file=file;
199: append_here->item.origin.line=line;
1.13 paf 200: #endif
1.115 paf 201: append_here++;
1.1 paf 202:
203: return *this;
1.97 parser 204: }
205:
206: char String::first_char() const {
1.120 paf 207: if(!used_rows())
1.112 parser 208: throw Exception(0, 0,
1.97 parser 209: this,
210: "getting first char of empty string");
211:
212: return *head.rows[0].item.ptr;
1.1 paf 213: }
214:
1.16 paf 215: uint String::hash_code() const {
1.7 paf 216: uint result=0;
1.5 paf 217:
1.16 paf 218: const Chunk *chunk=&head;
1.5 paf 219: do {
1.16 paf 220: const Chunk::Row *row=chunk->rows;
1.116 paf 221: for(uint i=0; i<chunk->count; i++) {
1.5 paf 222: if(row==append_here)
223: goto break2;
224:
1.6 paf 225: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 226: row++;
227: }
228: chunk=row->link;
229: } while(chunk);
230: break2:
231: return result;
232: }
233:
1.60 paf 234: /// @todo move 'lang' skipping to pos
235: int String::cmp(int& partial, const String& src,
236: size_t this_offset, Untaint_lang lang) const {
1.59 paf 237: partial=-1;
1.55 paf 238: this_offset=min(this_offset, size()-1);
239:
1.16 paf 240: const Chunk *a_chunk=&head;
241: const Chunk *b_chunk=&src.head;
242: const Chunk::Row *a_row=a_chunk->rows;
243: const Chunk::Row *b_row=b_chunk->rows;
1.55 paf 244: size_t a_offset=this_offset;
245: size_t b_offset=0;
1.9 paf 246: Chunk::Row *a_end=append_here;
247: Chunk::Row *b_end=src.append_here;
1.116 paf 248: uint a_countdown=a_chunk->count;
249: uint b_countdown=b_chunk->count;
250: int result;
1.60 paf 251: size_t pos=0;
1.33 paf 252:
1.83 parser 253: bool a_break=size()==0;
1.91 parser 254: bool b_break=src.size()==0;
1.83 parser 255: if(!(a_break || b_break)) while(true) {
1.55 paf 256: if(pos+a_row->item.size > this_offset) {
1.71 paf 257: if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang)
1.60 paf 258: return -1; // wrong lang -- bail out
259:
1.55 paf 260: int size_diff=
261: (a_row->item.size-a_offset)-
262: (b_row->item.size-b_offset);
263:
264: if(size_diff==0) { // a has same size as b
1.60 paf 265: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset,
266: a_row->item.size-a_offset);
1.55 paf 267: if(result)
268: return result;
1.60 paf 269: pos+=a_row->item.size;
1.55 paf 270: a_row++; a_countdown--; a_offset=0;
271: b_row++; b_countdown--; b_offset=0;
272: } else if (size_diff>0) { // a longer
1.60 paf 273: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset,
274: b_row->item.size-b_offset);
1.55 paf 275: if(result)
276: return result;
277: a_offset+=b_row->item.size-b_offset;
278: b_row++; b_countdown--; b_offset=0;
279: } else { // b longer
1.60 paf 280: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset,
281: a_row->item.size-a_offset);
1.55 paf 282: if(result)
283: return result;
284: b_offset+=a_row->item.size-a_offset;
1.60 paf 285: pos+=a_row->item.size;
1.55 paf 286: a_row++; a_countdown--; a_offset=0;
287: }
1.83 parser 288: if(b_break=b_row==b_end) {
289: a_break=a_row==a_end;
290: break;
291: }
1.55 paf 292: if(!b_countdown) {
293: b_chunk=b_row->link;
294: b_row=b_chunk->rows;
295: b_countdown=b_chunk->count;
296: }
297: } else {
1.60 paf 298: a_offset-=a_row->item.size;
299: pos+=a_row->item.size;
300: a_row++; a_countdown--;
1.9 paf 301: }
302:
1.83 parser 303: if(a_break=a_row==a_end) {
304: b_break=b_row==b_end;
305: break;
306: }
1.11 paf 307: if(!a_countdown) {
1.9 paf 308: a_chunk=a_row->link;
309: a_row=a_chunk->rows;
1.11 paf 310: a_countdown=a_chunk->count;
1.9 paf 311: }
1.27 paf 312: }
1.55 paf 313: if(a_break==b_break) { // ended simultaneously
314: partial=0; return 0;
315: } else if(a_break) { // first bytes equal, but a ended before b
316: partial=1; return -1;
317: } else {
318: partial=2; return +1;
319: }
1.27 paf 320: }
321:
1.60 paf 322: /// @todo move 'lang' skipping to pos
1.59 paf 323: int String::cmp(int& partial, const char* b_ptr, size_t src_size,
1.60 paf 324: size_t this_offset, Untaint_lang lang) const {
1.59 paf 325: partial=-1;
1.50 paf 326: size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.59 paf 327: this_offset=min(this_offset, size()-1);
1.27 paf 328:
329: const Chunk *a_chunk=&head;
330: const Chunk::Row *a_row=a_chunk->rows;
1.59 paf 331: size_t a_offset=this_offset;
1.55 paf 332: size_t b_offset=0;
1.27 paf 333: Chunk::Row *a_end=append_here;
1.116 paf 334: uint a_countdown=a_chunk->count;
1.60 paf 335: size_t pos=0;
1.52 paf 336:
1.83 parser 337: bool a_break=size()==0;
338: bool b_break=b_size==0;
339: if(!(a_break || b_break)) while(true) {
1.59 paf 340: if(pos+a_row->item.size > this_offset) {
1.71 paf 341: if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang)
1.60 paf 342: return -1; // wrong lang -- bail out
343:
1.59 paf 344: int size_diff=
345: (a_row->item.size-a_offset)-
346: (b_size-b_offset);
347:
348: if(size_diff==0) { // a has same size as b
1.116 paf 349: if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
1.59 paf 350: a_row->item.size-a_offset)!=0)
351: return result;
1.60 paf 352: pos+=a_row->item.size;
1.59 paf 353: a_row++; a_countdown--; a_offset=0;
354: b_break=true;
355: } else if (size_diff>0) { // a longer
1.116 paf 356: if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
1.59 paf 357: b_size-b_offset)!=0)
358: return result;
359: a_offset+=b_size-b_offset;
360: b_break=true;
361: } else { // b longer
1.116 paf 362: if(int result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
1.59 paf 363: a_row->item.size-a_offset)!=0)
364: return result;
365: b_offset+=a_row->item.size-a_offset;
1.60 paf 366: pos+=a_row->item.size;
1.59 paf 367: a_row++; a_countdown--; a_offset=0;
368: }
369: } else {
1.60 paf 370: a_offset-=a_row->item.size;
371: pos+=a_row->item.size;
372: a_row++; a_countdown--;
1.27 paf 373: }
374:
1.86 parser 375: a_break=a_row==a_end;
376: if(a_break || b_break)
1.83 parser 377: break;
1.27 paf 378: if(!a_countdown) {
379: a_chunk=a_row->link;
380: a_row=a_chunk->rows;
381: a_countdown=a_chunk->count;
1.9 paf 382: }
383: }
1.55 paf 384: if(a_break==b_break) { // ended simultaneously
385: partial=0; return 0;
386: } else if(a_break) { // first bytes equal, but a ended before b
387: partial=1; return -1;
388: } else {
389: partial=2; return +1;
390: }
1.5 paf 391: }
1.46 paf 392:
393: #ifndef NO_STRING_ORIGIN
394: const Origin& String::origin() const {
1.120 paf 395: if(!used_rows()) {
1.96 parser 396: static const Origin empty_origin={"empty string"};
397: return empty_origin;
398: }
1.46 paf 399:
1.49 paf 400: // determining origin by last appended piece
1.50 paf 401: // because first one frequently constant.
402: // ex: ^load[/file] "document_root" + "/file"
1.80 paf 403: // when last peice is constant,
404: // ex: parser_root_auto_path{dynamic} / auto.p{const}
405: // using first piece
406: Origin& last_origin=append_here[-1].item.origin;
407: return last_origin.file ? last_origin : head.rows[0].item.origin;
1.46 paf 408: }
409: #endif
1.53 paf 410:
1.69 paf 411: String& String::mid(size_t start, size_t finish) const {
1.107 parser 412: String& result=*NEW String(pool());
413:
1.53 paf 414: start=max(0, start);
1.111 parser 415: finish=min(size(), finish);
1.60 paf 416: if(start==finish)
1.107 parser 417: return result;
1.53 paf 418:
419: size_t pos=0;
420: const Chunk *chunk=&head;
421: do {
422: const Chunk::Row *row=chunk->rows;
1.116 paf 423: for(uint i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53 paf 424: if(row==append_here)
425: goto break2;
426:
1.60 paf 427: size_t item_finish=pos+row->item.size;
428: if(item_finish > start) { // started now or already?
429: bool started=result.size()==0; // started now?
430: bool finished=finish <= item_finish; // finished now?
1.53 paf 431: size_t offset=started?start-pos:0;
432: size_t size=finished?finish-pos:row->item.size;
433: result.APPEND(
434: row->item.ptr+offset, size-offset,
1.117 paf 435: (Untaint_lang)row->item.lang,
1.53 paf 436: row->item.origin.file, row->item.origin.line);
437: if(finished)
438: goto break2;
439: }
440: }
441: chunk=row->link;
442: } while(chunk);
443: break2:
1.60 paf 444: // SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
445: //cstr(), start, finish, result.cstr());
1.53 paf 446: return result;
1.54 paf 447: }
448:
1.60 paf 449: int String::pos(const String& substr,
1.116 paf 450: int result, Untaint_lang lang) const {
1.58 paf 451: for(; result<size(); result++) {
1.60 paf 452: int partial; cmp(partial, substr, result, lang);
1.58 paf 453: if(
454: partial==0 || // full match
455: partial==2) // 'substr' starts 'this'+'result'
456: return result;
457: }
458:
459: return -1;
460: }
461:
1.60 paf 462: int String::pos(const char *substr, size_t substr_size,
1.116 paf 463: int result, Untaint_lang lang) const {
1.57 paf 464: for(; result<size(); result++) {
1.60 paf 465: int partial; cmp(partial, substr, substr_size, result, lang);
1.55 paf 466: if(
467: partial==0 || // full match
468: partial==2) // 'substr' starts 'this'+'result'
469: return result;
470: }
471:
472: return -1;
1.60 paf 473: }
474:
475: void String::split(Array& result,
476: size_t* pos_after_ref,
477: const char *delim, size_t delim_size,
478: Untaint_lang lang, int limit) const {
479: if(delim_size) {
480: size_t pos_after=pos_after_ref?*pos_after_ref:0;
481: int pos_before;
482: // while we have 'delim'...
483: for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69 paf 484: result+=&mid(pos_after, pos_before);
1.60 paf 485: pos_after=pos_before+delim_size;
486: }
487: // last piece
488: if(pos_after<size() && limit) {
1.69 paf 489: result+=&mid(pos_after, size());
1.60 paf 490: pos_after=size();
491: }
492: if(pos_after_ref)
493: *pos_after_ref=pos_after;
494: } else { // empty delim
495: result+=this;
496: if(pos_after_ref)
497: *pos_after_ref+=size();
498: }
499: }
500:
501: void String::split(Array& result,
502: size_t* pos_after_ref,
503: const String& delim, Untaint_lang lang,
504: int limit) const {
505: if(delim.size()) {
506: size_t pos_after=pos_after_ref?*pos_after_ref:0;
507: int pos_before;
508: // while we have 'delim'...
509: for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69 paf 510: result+=&mid(pos_after, pos_before);
1.60 paf 511: pos_after=pos_before+delim.size();
512: }
513: // last piece
514: if(pos_after<size() && limit) {
1.69 paf 515: result+=&mid(pos_after, size());
1.60 paf 516: pos_after=size();
517: }
518: if(pos_after_ref)
519: *pos_after_ref=pos_after;
520: } else { // empty delim
521: result+=this;
522: if(pos_after_ref)
523: *pos_after_ref+=size();
524: }
1.61 paf 525: }
526:
1.63 paf 527: static void regex_options(char *options, int *result){
528: struct Regex_option {
529: char key;
530: int clear, set;
531: int *result;
532: } regex_option[]={
533: {'i', 0, PCRE_CASELESS, result}, // a=A
1.79 paf 534: {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63 paf 535: {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
536: {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
537: {'g', 0, true, result+1}, // many rows
538: {0},
539: };
540: result[0]=PCRE_EXTRA | PCRE_DOTALL;
541: result[1]=0;
542:
543: if(options)
544: for(Regex_option *o=regex_option; o->key; o++)
545: if(
546: strchr(options, o->key) ||
547: strchr(options, toupper(o->key))) {
548: *(o->result)&=~o->clear;
549: *(o->result)|=o->set;
550: }
551: }
552:
1.88 parser 553: /// @todo maybe need speedup: some option to remove pre/match/post string generation
1.77 paf 554: bool String::match(const unsigned char *pcre_tables,
555: const String *aorigin,
1.62 paf 556: const String& regexp,
1.63 paf 557: const String *options,
1.64 paf 558: Table **table,
1.95 parser 559: Row_action row_action, void *info,
560: bool *was_global) const {
1.64 paf 561:
1.73 paf 562: if(!regexp.size())
1.112 parser 563: throw Exception(0, 0,
1.73 paf 564: aorigin,
565: "regexp is empty");
1.118 paf 566: const char *pattern=regexp.cstr();
1.62 paf 567: const char *errptr;
568: int erroffset;
1.63 paf 569: int option_bits[2]; regex_options(options?options->cstr():0, option_bits);
1.95 parser 570: if(was_global)
571: *was_global=option_bits[1]!=0;
1.63 paf 572: pcre *code=pcre_compile(pattern, option_bits[0],
1.62 paf 573: &errptr, &erroffset,
1.74 paf 574: pcre_tables);
1.62 paf 575:
1.67 paf 576: if(!code)
1.112 parser 577: throw Exception(0, 0,
1.69 paf 578: ®exp.mid(erroffset, regexp.size()),
1.74 paf 579: "regular expression syntax error - %s", errptr);
1.62 paf 580:
1.63 paf 581: int info_substrings=pcre_info(code, 0, 0);
582: if(info_substrings<0) {
1.100 parser 583: pcre_free(code);
1.112 parser 584: throw Exception(0, 0,
1.73 paf 585: aorigin,
1.76 paf 586: "pcre_info error (%d)",
1.73 paf 587: info_substrings);
1.63 paf 588: }
589:
590: int startoffset=0;
1.118 paf 591: const char *subject=cstr();
1.62 paf 592: int length=strlen(subject);
1.63 paf 593: int ovecsize;
594: int *ovector=(int *)malloc(sizeof(int)*
1.114 paf 595: (ovecsize=(1/*match*/+info_substrings)*3), 11);
1.62 paf 596:
1.64 paf 597: { // create table
598: Array& columns=*NEW Array(pool());
599: columns+=string_pre_match_name;
600: columns+=string_match_name;
601: columns+=string_post_match_name;
602: for(int i=1; i<=info_substrings; i++) {
603: char *column=(char *)malloc(MAX_NUMBER);
604: snprintf(column, MAX_NUMBER, "%d", i);
605: columns+=NEW String(pool(), column); // .i column name
606: }
607: *table=NEW Table(pool(), aorigin, &columns);
1.62 paf 608: }
1.63 paf 609:
1.64 paf 610: int exec_option_bits=0;
1.63 paf 611: while(true) {
612: int exec_substrings=pcre_exec(code, 0,
613: subject, length, startoffset,
1.64 paf 614: exec_option_bits, ovector, ovecsize);
1.63 paf 615:
616: if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100 parser 617: pcre_free(code);
618: row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63 paf 619: return option_bits[1]!=0; // global=true+table, not global=false
620: }
621:
622: if(exec_substrings<0) {
1.100 parser 623: pcre_free(code);
1.112 parser 624: throw Exception(0, 0,
1.63 paf 625: aorigin,
1.76 paf 626: "regular expression execute error (%d)",
1.63 paf 627: exec_substrings);
628: }
629:
630: Array& row=*NEW Array(pool());
1.81 paf 631: row+=&mid(0, ovector[0]); // .prematch column value
1.69 paf 632: row+=&mid(ovector[0], ovector[1]); // .match
1.81 paf 633: row+=&mid(ovector[1], size()); // .postmatch
1.63 paf 634:
635: for(int i=1; i<exec_substrings; i++) {
1.69 paf 636: // -1:-1 case handled peacefully by mid() itself
637: row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63 paf 638: }
639:
1.100 parser 640: row_action(**table, &row, startoffset, ovector[0], info);
1.63 paf 641:
1.100 parser 642: if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang
643: pcre_free(code);
644: row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63 paf 645: return true;
646: }
1.100 parser 647: startoffset=ovector[1];
1.63 paf 648:
649: /*
650: if(option_bits[0] & PCRE_MULTILINE)
1.64 paf 651: exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63 paf 652: */
653: }
1.82 parser 654: }
655:
656: String& String::change_case(Pool& pool, const unsigned char *tables,
657: Change_case_kind kind) const {
658: String& result=*new(pool) String(pool);
659:
660: const unsigned char *a;
661: const unsigned char *b;
662: switch(kind) {
663: case CC_UPPER:
664: a=tables+lcc_offset;
665: b=tables+fcc_offset;
666: break;
667: case CC_LOWER:
668: a=tables+lcc_offset;
669: b=0;
670: break;
671: default:
1.112 parser 672: throw Exception(0, 0,
1.82 parser 673: this,
674: "unknown change case kind #%d",
675: static_cast<int>(kind)); // never
676: a=b=0; // calm, compiler
677: break; // never
678: }
679:
680: const Chunk *chunk=&head;
681: do {
682: const Chunk::Row *row=chunk->rows;
1.116 paf 683: for(uint i=0; i<chunk->count; i++, row++) {
1.82 parser 684: if(row==append_here)
685: goto break2;
686:
1.114 paf 687: char *new_cstr=(char *)pool.malloc(row->item.size, 12);
1.82 parser 688: char *dest=new_cstr;
689: const char *src=row->item.ptr;
690: for(int size=row->item.size; size--; src++) {
691: unsigned char c=a[(unsigned char)*src];
692: if(b)
693: c=b[c];
694:
695: *dest++=(char)c;
696: }
697:
698: result.APPEND(new_cstr, row->item.size,
1.117 paf 699: (Untaint_lang)row->item.lang,
1.82 parser 700: row->item.origin.file, row->item.origin.line);
701: }
702: chunk=row->link;
703: } while(chunk);
704: break2:
1.89 parser 705:
1.101 parser 706: return result;
707: }
708:
1.108 parser 709: void String::join_chain(Pool& pool,
1.116 paf 710: uint& ai, const Chunk*& achunk, const Chunk::Row*& arow,
1.108 parser 711: Untaint_lang& joined_lang, const char *& joined_ptr, size_t& joined_size) const {
1.117 paf 712: joined_lang=(Untaint_lang)arow->item.lang;
1.108 parser 713:
714: // calc size
715: joined_size=0;
716: {
1.116 paf 717: uint start_i=ai;
1.108 parser 718: const Chunk::Row *start_row=arow;
719: const Chunk *chunk=achunk;
720: do {
721: const Chunk::Row *row=start_row;
1.116 paf 722: for(uint i=start_i; i<chunk->count; i++, row++) {
1.108 parser 723: if(row==append_here)
724: goto break21;
725:
726: if(row->item.lang==joined_lang)
727: joined_size+=row->item.size;
728: else
729: break;
730: }
731: if(chunk=row->link) {
732: start_i=0;
733: start_row=chunk->rows;
734: } else
735: break;
736: } while(true);
737: break21:;
738: }
739:
740: // if one row, return simply itself
741: if(joined_size==arow->item.size) {
742: joined_ptr=arow->item.ptr;
743: ai++; arow++;
744: if(ai==achunk->count)
745: achunk=arow->link;
746: } else {
747: // join adjacent rows
1.114 paf 748: char *ptr=(char *)pool.malloc(joined_size,13);
1.108 parser 749: joined_ptr=ptr;
1.116 paf 750: uint start_i=ai;
1.108 parser 751: const Chunk::Row *start_row=arow;
752: const Chunk *chunk=achunk;
1.116 paf 753: uint i;
1.108 parser 754: const Chunk::Row *row;
755: do {
756: row=start_row;
757: for(i=start_i; i<chunk->count; i++, row++) {
758: if(row==append_here)
759: goto break22;
760:
761: if(row->item.lang==joined_lang) {
762: memcpy(ptr, row->item.ptr, row->item.size);
763: ptr+=row->item.size;
764: } else
765: break;
766: }
767: if(chunk=row->link) {
768: start_i=0;
769: start_row=chunk->rows;
770: } else
771: break;
772: } while(true);
773: break22:;
774:
775: // return joined rows
776: ai=i;
777: arow=row;
778: achunk=chunk;
779: }
780: }
781:
782: String& String::reconstruct(Pool& pool) const {
783: //_asm int 3;
784: String& result=*new(pool) String(pool);
785: const Chunk *chunk=&head;
786: do {
787: const Chunk::Row *row=chunk->rows;
1.116 paf 788: for(uint i=0; i<chunk->count; ) {
1.108 parser 789: if(row==append_here)
790: goto break2;
791:
792: Untaint_lang joined_lang;
1.109 parser 793: const char *joined_ptr;
1.108 parser 794: size_t joined_size;
795: join_chain(pool, i, chunk, row,
796: joined_lang, joined_ptr, joined_size);
797:
798: result.APPEND(joined_ptr, joined_size,
799: joined_lang,
800: row->item.origin.file, row->item.origin.line);
801: if(!chunk)
802: goto break2;
803: }
804: } while(true);
805: break2:
806:
807: return result;
808: };
809:
810: String& String::replace_in_reconstructed(Pool& pool, Dictionary& dict) const {
1.106 parser 811: //_asm int 3;
1.101 parser 812: String& result=*new(pool) String(pool);
813: const Chunk *chunk=&head;
814: do {
815: const Chunk::Row *row=chunk->rows;
1.116 paf 816: for(uint i=0; i<chunk->count; i++, row++) {
1.101 parser 817: if(row==append_here)
818: goto break2;
819:
820: const char *src=row->item.ptr;
821: size_t src_size=row->item.size;
1.114 paf 822: char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);
1.101 parser 823: char *dest=new_cstr;
824: while(src_size) {
825: // there is a row where first column starts 'src'
1.106 parser 826: if(Table::Item *item=dict.first_that_starts(src, src_size)) {
1.101 parser 827: // get a=>b values
828: const String& a=*static_cast<Array *>(item)->get_string(0);
829: const String& b=*static_cast<Array *>(item)->get_string(1);
1.105 parser 830: // skip 'a' in 'src' && reduce work size
831: src+=a.size(); src_size-=a.size();
832: // write 'b' to 'dest' && skip 'b' in 'dest'
833: b.store_to(dest); dest+=b.size();
1.101 parser 834: } else {
1.105 parser 835: // write a char to b && reduce work size
836: *dest++=*src++; src_size--;
1.101 parser 837: }
838: }
839:
840: result.APPEND(new_cstr, dest-new_cstr,
1.117 paf 841: (Untaint_lang)row->item.lang,
1.101 parser 842: row->item.origin.file, row->item.origin.line);
843: }
844: chunk=row->link;
845: } while(chunk);
846:
847: break2:
1.89 parser 848: return result;
1.108 parser 849: }
850:
851: String& String::replace(Pool& pool, Dictionary& dict) const {
852: return reconstruct(pool).replace_in_reconstructed(pool, dict);
1.89 parser 853: }
854:
1.90 parser 855: double String::as_double() const {
1.89 parser 856: double result;
1.114 paf 857: const char *cstr;
858: char buf[MAX_NUMBER];
1.115 paf 859: if(head.rows+1==append_here) {
1.114 paf 860: int size=min(head.rows[0].item.size, MAX_NUMBER-1);
861: memcpy(buf, head.rows[0].item.ptr, size);
862: buf[size]=0;
863: cstr=buf;
864: } else
865: cstr=this->cstr();
1.102 parser 866: char *error_pos;
1.89 parser 867: // 0xABC
1.99 parser 868: if(cstr[0]=='0')
869: if(cstr[1]=='x' || cstr[1]=='X')
870: result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
871: else
1.102 parser 872: result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89 parser 873: else
1.99 parser 874: result=(double)strtod(cstr, &error_pos);
1.89 parser 875:
1.103 parser 876: if(*error_pos/*not EOS*/)
1.112 parser 877: throw Exception(0, 0,
1.89 parser 878: this,
879: "invalid number (double)");
880:
881: return result;
882: }
1.90 parser 883: int String::as_int() const {
1.89 parser 884: int result;
1.114 paf 885: const char *cstr;
886: char buf[MAX_NUMBER];
1.115 paf 887: if(head.rows+1==append_here) {
1.114 paf 888: int size=min(head.rows[0].item.size, MAX_NUMBER-1);
889: memcpy(buf, head.rows[0].item.ptr, size);
890: buf[size]=0;
891: cstr=buf;
892: } else
893: cstr=this->cstr();
1.102 parser 894: char *error_pos;
1.89 parser 895: // 0xABC
1.99 parser 896: if(cstr[0]=='0')
897: if(cstr[1]=='x' || cstr[1]=='X')
898: result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
899: else
1.102 parser 900: result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89 parser 901: else
902: result=(int)strtol(cstr, &error_pos, 0);
903:
1.103 parser 904: if(*error_pos/*not EOS*/)
1.112 parser 905: throw Exception(0, 0,
1.89 parser 906: this,
907: "invalid number (int)");
1.82 parser 908:
909: return result;
1.61 paf 910: }
1.113 parser 911:
912: /* @todo maybe network order worth spending some effort?
913: don't bothering myself with network byte order,
914: am not planning to be able to move resulting file across platforms
915: for now
916: */
917: void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
918: buf_size=
919: prolog_size
1.115 paf 920: +used_rows()*(sizeof(Untaint_lang)+sizeof(size_t))
1.113 parser 921: +size();
1.114 paf 922: buf=malloc(buf_size,15);
1.113 parser 923: char *cur=(char *)buf+prolog_size;
924:
925: const Chunk *chunk=&head;
926: do {
927: const Chunk::Row *row=chunk->rows;
1.116 paf 928: for(uint i=0; i<chunk->count; i++) {
1.113 parser 929: if(row==append_here)
930: goto break2;
931:
932: // lang
933: memcpy(cur, &row->item.lang, sizeof(Untaint_lang));
934: cur+=sizeof(Untaint_lang);
935: // size
936: memcpy(cur, &row->item.size, sizeof(size_t));
937: cur+=sizeof(size_t);
938: // bytes
939: memcpy(cur, row->item.ptr, row->item.size);
940: cur+=row->item.size;
941:
942: row++;
943: }
944: chunk=row->link;
945: } while(chunk);
946: break2:
947: ;
948: }
949:
950: /* @todo maybe network order worth spending some effort?
951: don't bothering myself with network byte order,
952: am not planning to be able to move resulting file across platforms
953: for now
954: */
955: #ifndef DOXYGEN
956: struct Serialized_piece {
957: String::Untaint_lang lang;
958: size_t size;
959: char ptr[1];
960: };
961: #endif
962:
963: void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
964: char *cur=((char *)buf)+prolog_size;
965: buf_size-=prolog_size;
966:
967: while(buf_size) {
968: Serialized_piece& p=*(Serialized_piece *)cur;
969: APPEND(p.ptr, p.size, p.lang, file, 0);
970:
971: size_t piece_size=sizeof(p.lang)+sizeof(p.size)+p.size;
972: cur+=piece_size;
973: buf_size-=piece_size;
974: }
975: }
976:
E-mail: