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