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