Annotation of parser3/src/main/pa_string.C, revision 1.107
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.107 ! parser 7: $Id: pa_string.C,v 1.106 2001/10/05 16:12:40 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);
363: finish=min(size(), finish);
1.60 paf 364: if(start==finish)
1.107 ! parser 365: return result;
1.53 paf 366:
367: size_t pos=0;
368: const Chunk *chunk=&head;
369: do {
370: const Chunk::Row *row=chunk->rows;
1.55 paf 371: for(size_t i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53 paf 372: if(row==append_here)
373: goto break2;
374:
1.60 paf 375: size_t item_finish=pos+row->item.size;
376: if(item_finish > start) { // started now or already?
377: bool started=result.size()==0; // started now?
378: bool finished=finish <= item_finish; // finished now?
1.53 paf 379: size_t offset=started?start-pos:0;
380: size_t size=finished?finish-pos:row->item.size;
381: result.APPEND(
382: row->item.ptr+offset, size-offset,
383: row->item.lang,
384: row->item.origin.file, row->item.origin.line);
385: if(finished)
386: goto break2;
387: }
388: }
389: chunk=row->link;
390: } while(chunk);
391: break2:
1.60 paf 392: // SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
393: //cstr(), start, finish, result.cstr());
1.53 paf 394: return result;
1.54 paf 395: }
396:
1.60 paf 397: int String::pos(const String& substr,
398: size_t result, Untaint_lang lang) const {
1.58 paf 399: for(; result<size(); result++) {
1.60 paf 400: int partial; cmp(partial, substr, result, lang);
1.58 paf 401: if(
402: partial==0 || // full match
403: partial==2) // 'substr' starts 'this'+'result'
404: return result;
405: }
406:
407: return -1;
408: }
409:
1.60 paf 410: int String::pos(const char *substr, size_t substr_size,
411: size_t result, Untaint_lang lang) const {
1.57 paf 412: for(; result<size(); result++) {
1.60 paf 413: int partial; cmp(partial, substr, substr_size, result, lang);
1.55 paf 414: if(
415: partial==0 || // full match
416: partial==2) // 'substr' starts 'this'+'result'
417: return result;
418: }
419:
420: return -1;
1.60 paf 421: }
422:
423: void String::split(Array& result,
424: size_t* pos_after_ref,
425: const char *delim, size_t delim_size,
426: Untaint_lang lang, int limit) const {
427: if(delim_size) {
428: size_t pos_after=pos_after_ref?*pos_after_ref:0;
429: int pos_before;
430: // while we have 'delim'...
431: for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69 paf 432: result+=&mid(pos_after, pos_before);
1.60 paf 433: pos_after=pos_before+delim_size;
434: }
435: // last piece
436: if(pos_after<size() && limit) {
1.69 paf 437: result+=&mid(pos_after, size());
1.60 paf 438: pos_after=size();
439: }
440: if(pos_after_ref)
441: *pos_after_ref=pos_after;
442: } else { // empty delim
443: result+=this;
444: if(pos_after_ref)
445: *pos_after_ref+=size();
446: }
447: }
448:
449: void String::split(Array& result,
450: size_t* pos_after_ref,
451: const String& delim, Untaint_lang lang,
452: int limit) const {
453: if(delim.size()) {
454: size_t pos_after=pos_after_ref?*pos_after_ref:0;
455: int pos_before;
456: // while we have 'delim'...
457: for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69 paf 458: result+=&mid(pos_after, pos_before);
1.60 paf 459: pos_after=pos_before+delim.size();
460: }
461: // last piece
462: if(pos_after<size() && limit) {
1.69 paf 463: result+=&mid(pos_after, size());
1.60 paf 464: pos_after=size();
465: }
466: if(pos_after_ref)
467: *pos_after_ref=pos_after;
468: } else { // empty delim
469: result+=this;
470: if(pos_after_ref)
471: *pos_after_ref+=size();
472: }
1.61 paf 473: }
474:
1.63 paf 475: static void regex_options(char *options, int *result){
476: struct Regex_option {
477: char key;
478: int clear, set;
479: int *result;
480: } regex_option[]={
481: {'i', 0, PCRE_CASELESS, result}, // a=A
1.79 paf 482: {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63 paf 483: {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
484: {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
485: {'g', 0, true, result+1}, // many rows
486: {0},
487: };
488: result[0]=PCRE_EXTRA | PCRE_DOTALL;
489: result[1]=0;
490:
491: if(options)
492: for(Regex_option *o=regex_option; o->key; o++)
493: if(
494: strchr(options, o->key) ||
495: strchr(options, toupper(o->key))) {
496: *(o->result)&=~o->clear;
497: *(o->result)|=o->set;
498: }
499: }
500:
1.88 parser 501: /// @todo maybe need speedup: some option to remove pre/match/post string generation
1.77 paf 502: bool String::match(const unsigned char *pcre_tables,
503: const String *aorigin,
1.62 paf 504: const String& regexp,
1.63 paf 505: const String *options,
1.64 paf 506: Table **table,
1.95 parser 507: Row_action row_action, void *info,
508: bool *was_global) const {
1.64 paf 509:
1.73 paf 510: if(!regexp.size())
511: THROW(0, 0,
512: aorigin,
513: "regexp is empty");
1.68 paf 514: const char *pattern=regexp.cstr(UL_AS_IS);
1.62 paf 515: const char *errptr;
516: int erroffset;
1.63 paf 517: int option_bits[2]; regex_options(options?options->cstr():0, option_bits);
1.95 parser 518: if(was_global)
519: *was_global=option_bits[1]!=0;
1.63 paf 520: pcre *code=pcre_compile(pattern, option_bits[0],
1.62 paf 521: &errptr, &erroffset,
1.74 paf 522: pcre_tables);
1.62 paf 523:
1.67 paf 524: if(!code)
1.62 paf 525: THROW(0, 0,
1.69 paf 526: ®exp.mid(erroffset, regexp.size()),
1.74 paf 527: "regular expression syntax error - %s", errptr);
1.62 paf 528:
1.63 paf 529: int info_substrings=pcre_info(code, 0, 0);
530: if(info_substrings<0) {
1.100 parser 531: pcre_free(code);
1.63 paf 532: THROW(0, 0,
1.73 paf 533: aorigin,
1.76 paf 534: "pcre_info error (%d)",
1.73 paf 535: info_substrings);
1.63 paf 536: }
537:
538: int startoffset=0;
1.68 paf 539: const char *subject=cstr(UL_AS_IS);
1.62 paf 540: int length=strlen(subject);
1.63 paf 541: int ovecsize;
542: int *ovector=(int *)malloc(sizeof(int)*
1.65 paf 543: (ovecsize=(1/*match*/+info_substrings)*3));
1.62 paf 544:
1.64 paf 545: { // create table
546: Array& columns=*NEW Array(pool());
547: columns+=string_pre_match_name;
548: columns+=string_match_name;
549: columns+=string_post_match_name;
550: for(int i=1; i<=info_substrings; i++) {
551: char *column=(char *)malloc(MAX_NUMBER);
552: snprintf(column, MAX_NUMBER, "%d", i);
553: columns+=NEW String(pool(), column); // .i column name
554: }
555: *table=NEW Table(pool(), aorigin, &columns);
1.62 paf 556: }
1.63 paf 557:
1.64 paf 558: int exec_option_bits=0;
1.63 paf 559: while(true) {
560: int exec_substrings=pcre_exec(code, 0,
561: subject, length, startoffset,
1.64 paf 562: exec_option_bits, ovector, ovecsize);
1.63 paf 563:
564: if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100 parser 565: pcre_free(code);
566: row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63 paf 567: return option_bits[1]!=0; // global=true+table, not global=false
568: }
569:
570: if(exec_substrings<0) {
1.100 parser 571: pcre_free(code);
1.63 paf 572: THROW(0, 0,
573: aorigin,
1.76 paf 574: "regular expression execute error (%d)",
1.63 paf 575: exec_substrings);
576: }
577:
578: Array& row=*NEW Array(pool());
1.81 paf 579: row+=&mid(0, ovector[0]); // .prematch column value
1.69 paf 580: row+=&mid(ovector[0], ovector[1]); // .match
1.81 paf 581: row+=&mid(ovector[1], size()); // .postmatch
1.63 paf 582:
583: for(int i=1; i<exec_substrings; i++) {
1.69 paf 584: // -1:-1 case handled peacefully by mid() itself
585: row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63 paf 586: }
587:
1.100 parser 588: row_action(**table, &row, startoffset, ovector[0], info);
1.63 paf 589:
1.100 parser 590: if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang
591: pcre_free(code);
592: row_action(**table, 0/*last time, no row*/, 0, 0, info);
1.63 paf 593: return true;
594: }
1.100 parser 595: startoffset=ovector[1];
1.63 paf 596:
597: /*
598: if(option_bits[0] & PCRE_MULTILINE)
1.64 paf 599: exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63 paf 600: */
601: }
1.82 parser 602: }
603:
604: String& String::change_case(Pool& pool, const unsigned char *tables,
605: Change_case_kind kind) const {
606: String& result=*new(pool) String(pool);
607:
608: const unsigned char *a;
609: const unsigned char *b;
610: switch(kind) {
611: case CC_UPPER:
612: a=tables+lcc_offset;
613: b=tables+fcc_offset;
614: break;
615: case CC_LOWER:
616: a=tables+lcc_offset;
617: b=0;
618: break;
619: default:
620: PTHROW(0, 0,
621: this,
622: "unknown change case kind #%d",
623: static_cast<int>(kind)); // never
624: a=b=0; // calm, compiler
625: break; // never
626: }
627:
628: const Chunk *chunk=&head;
629: do {
630: const Chunk::Row *row=chunk->rows;
631: for(size_t i=0; i<chunk->count; i++, row++) {
632: if(row==append_here)
633: goto break2;
634:
635: char *new_cstr=(char *)pool.malloc(row->item.size);
636: char *dest=new_cstr;
637: const char *src=row->item.ptr;
638: for(int size=row->item.size; size--; src++) {
639: unsigned char c=a[(unsigned char)*src];
640: if(b)
641: c=b[c];
642:
643: *dest++=(char)c;
644: }
645:
646: result.APPEND(new_cstr, row->item.size,
647: row->item.lang,
648: row->item.origin.file, row->item.origin.line);
649: }
650: chunk=row->link;
651: } while(chunk);
652: break2:
1.89 parser 653:
1.101 parser 654: return result;
655: }
656:
1.106 parser 657: /// @test restructure string to link pieces of same language together prior to this.
1.101 parser 658: String& String::replace(Pool& pool, Dictionary& dict) const {
1.106 parser 659: //_asm int 3;
1.101 parser 660: String& result=*new(pool) String(pool);
661: const Chunk *chunk=&head;
662: do {
663: const Chunk::Row *row=chunk->rows;
664: for(size_t i=0; i<chunk->count; i++, row++) {
665: if(row==append_here)
666: goto break2;
667:
668: const char *src=row->item.ptr;
669: size_t src_size=row->item.size;
670: char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()));
671: char *dest=new_cstr;
672: while(src_size) {
673: // there is a row where first column starts 'src'
1.106 parser 674: if(Table::Item *item=dict.first_that_starts(src, src_size)) {
1.101 parser 675: // get a=>b values
676: const String& a=*static_cast<Array *>(item)->get_string(0);
677: const String& b=*static_cast<Array *>(item)->get_string(1);
1.105 parser 678: // skip 'a' in 'src' && reduce work size
679: src+=a.size(); src_size-=a.size();
680: // write 'b' to 'dest' && skip 'b' in 'dest'
681: b.store_to(dest); dest+=b.size();
1.101 parser 682: } else {
1.105 parser 683: // write a char to b && reduce work size
684: *dest++=*src++; src_size--;
1.101 parser 685: }
686: }
687:
688: result.APPEND(new_cstr, dest-new_cstr,
689: row->item.lang,
690: row->item.origin.file, row->item.origin.line);
691: }
692: chunk=row->link;
693: } while(chunk);
694:
695: break2:
1.89 parser 696: return result;
697: }
698:
1.90 parser 699: double String::as_double() const {
1.89 parser 700: double result;
701: const char *cstr=this->cstr();
1.102 parser 702: char *error_pos;
1.89 parser 703: // 0xABC
1.99 parser 704: if(cstr[0]=='0')
705: if(cstr[1]=='x' || cstr[1]=='X')
706: result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
707: else
1.102 parser 708: result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89 parser 709: else
1.99 parser 710: result=(double)strtod(cstr, &error_pos);
1.89 parser 711:
1.103 parser 712: if(*error_pos/*not EOS*/)
1.89 parser 713: THROW(0, 0,
714: this,
715: "invalid number (double)");
716:
717: return result;
718: }
1.90 parser 719: int String::as_int() const {
1.89 parser 720: int result;
721: const char *cstr=this->cstr();
1.102 parser 722: char *error_pos;
1.89 parser 723: // 0xABC
1.99 parser 724: if(cstr[0]=='0')
725: if(cstr[1]=='x' || cstr[1]=='X')
726: result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
727: else
1.102 parser 728: result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89 parser 729: else
730: result=(int)strtol(cstr, &error_pos, 0);
731:
1.103 parser 732: if(*error_pos/*not EOS*/)
1.89 parser 733: THROW(0, 0,
734: this,
735: "invalid number (int)");
1.82 parser 736:
737: return result;
1.61 paf 738: }
E-mail: