Annotation of parser3/src/main/pa_string.C, revision 1.157
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.157 ! paf 7: $Id: pa_string.C,v 1.156 2002/04/19 11:59:44 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.155 paf 476: /// @todo make replacement Table stacked
1.156 paf 477: bool String::match(const char *acstr,
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.156 paf 515: const char *subject=acstr?acstr:cstr();
1.62 paf 516: int length=strlen(subject);
1.155 paf 517: const int ovecsize=(1/*match*/+MAX_STRING_MATCH_TABLE_COLUMNS)*3;
518: int ovector[ovecsize];
519:
520: // create table
1.157 ! paf 521: *table=NEW Table(pool(), *string_match_table_template);
1.63 paf 522:
1.64 paf 523: int exec_option_bits=0;
1.154 paf 524: int prestart=0;
525: int poststart=0;
526: int postfinish=size();
1.63 paf 527: while(true) {
528: int exec_substrings=pcre_exec(code, 0,
1.154 paf 529: subject, length, prestart,
1.64 paf 530: exec_option_bits, ovector, ovecsize);
1.63 paf 531:
532: if(exec_substrings==PCRE_ERROR_NOMATCH) {
1.100 parser 533: pcre_free(code);
1.154 paf 534: row_action(**table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
1.63 paf 535: return option_bits[1]!=0; // global=true+table, not global=false
536: }
537:
538: if(exec_substrings<0) {
1.100 parser 539: pcre_free(code);
1.149 paf 540: throw Exception(0,
1.63 paf 541: aorigin,
1.76 paf 542: "regular expression execute error (%d)",
1.63 paf 543: exec_substrings);
544: }
545:
1.154 paf 546: int prefinish=ovector[0];
547: poststart=ovector[1];
1.63 paf 548: Array& row=*NEW Array(pool());
1.154 paf 549: row+=need_pre_post_match?&mid(0, prefinish):0; // .prematch column value
550: row+=need_pre_post_match?&mid(prefinish, poststart):0; // .match
551: row+=need_pre_post_match?&mid(poststart, postfinish):0; // .postmatch
1.63 paf 552:
553: for(int i=1; i<exec_substrings; i++) {
1.69 paf 554: // -1:-1 case handled peacefully by mid() itself
555: row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63 paf 556: }
557:
1.154 paf 558: row_action(**table, &row, prestart, prefinish, poststart, postfinish, info);
1.63 paf 559:
1.154 paf 560: if(!option_bits[1] || prestart==poststart) { // not global | going to hang
1.100 parser 561: pcre_free(code);
1.154 paf 562: row_action(**table, 0/*last time, no row*/, 0, 0, poststart, postfinish, info);
1.63 paf 563: return true;
564: }
1.154 paf 565: prestart=poststart;
1.63 paf 566:
567: /*
568: if(option_bits[0] & PCRE_MULTILINE)
1.64 paf 569: exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63 paf 570: */
571: }
1.82 parser 572: }
573:
1.132 paf 574: String& String::change_case(Pool& pool,
1.82 parser 575: Change_case_kind kind) const {
1.132 paf 576: const unsigned char *tables=pool.get_source_charset().pcre_tables;
1.82 parser 577: String& result=*new(pool) String(pool);
578:
579: const unsigned char *a;
580: const unsigned char *b;
581: switch(kind) {
582: case CC_UPPER:
583: a=tables+lcc_offset;
584: b=tables+fcc_offset;
585: break;
586: case CC_LOWER:
587: a=tables+lcc_offset;
588: b=0;
589: break;
590: default:
1.149 paf 591: throw Exception(0,
1.82 parser 592: this,
593: "unknown change case kind #%d",
594: static_cast<int>(kind)); // never
595: a=b=0; // calm, compiler
596: break; // never
597: }
598:
1.143 paf 599: STRING_FOREACH_ROW(
600: char *new_cstr=(char *)pool.malloc(row->item.size, 12);
601: char *dest=new_cstr;
602: const char *src=row->item.ptr;
603: for(int size=row->item.size; size--; src++) {
604: unsigned char c=a[(unsigned char)*src];
605: if(b)
606: c=b[c];
1.82 parser 607:
1.143 paf 608: *dest++=(char)c;
1.82 parser 609: }
1.143 paf 610:
611: result.APPEND(new_cstr, row->item.size,
612: row->item.lang,
613: row->item.origin.file, row->item.origin.line);
614: );
1.89 parser 615:
1.101 parser 616: return result;
617: }
618:
1.150 paf 619: /// @test if in some piece were found no dict words, append it, not it's duplicate
620: String& String::replace(Pool& pool, Dictionary& dict) const {
621: // return reconstruct(pool).replace_in_reconstructed(pool, dict);
1.108 parser 622: String& result=*new(pool) String(pool);
1.150 paf 623:
1.143 paf 624: STRING_FOREACH_ROW(
1.156 paf 625: const char *src=row->item.ptr;
626: size_t src_size=row->item.size;
1.123 paf 627: char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14);
628: char *dest=new_cstr;
629: while(src_size) {
630: // there is a row where first column starts 'src'
631: if(Table::Item *item=dict.first_that_starts(src, src_size)) {
632: // get a=>b values
633: const String& a=*static_cast<Array *>(item)->get_string(0);
634: const String& b=*static_cast<Array *>(item)->get_string(1);
635: // skip 'a' in 'src' && reduce work size
636: src+=a.size(); src_size-=a.size();
637: // write 'b' to 'dest' && skip 'b' in 'dest'
638: b.store_to(dest); dest+=b.size();
639: } else {
640: // write a char to b && reduce work size
641: *dest++=*src++; src_size--;
1.101 parser 642: }
643: }
644:
1.156 paf 645: result.APPEND(new_cstr, dest-new_cstr, row->item.lang,
646: row->item.origin.file, row->item.origin.line);
647: );
648: return result;
649: }
650:
651: /// @test real!
652: bool String::is_join_chains_profitable() const {
653: // mimimum actually sizeof(String),
654: // but one must also consider CPU time optimize would eat
655: const int minimum_economy=sizeof(String)*2;
656:
657: size_t wasted=0;
658: STRING_FOREACH_ROW(
659: uchar joined_lang=row->item.lang;
660: STRING_PREPARED_FOREACH_ROW(*this,
661: if(row->item.lang==joined_lang) {
662: wasted+=sizeof(String::Chunk::rows_type);
663: if(wasted>minimum_economy)
664: return true;
665: } else
666: break; // before non-ours
667: );
668:
669: // pointers are after joined piece
670: // & one step back, see STRING_FOREACH_ROW
671: --row; ++countdown;
672: );
673: return false;
674: }
675:
676: String& String::join_chains(Pool& pool, char** acstr) const {
677: char *lcstr=cstr();
678: const char *current=lcstr;
679:
680: String& result=*new(pool) String(pool);
681: STRING_FOREACH_ROW(
682: IFNDEF_NO_STRING_ORIGIN(
683: const char *joined_origin_file=row->item.origin.file;
684: const size_t joined_origin_line=row->item.origin.line;
685: );
686: uchar joined_lang=row->item.lang;
687: const char *joined_ptr=current;
688: // calc size
689: size_t joined_size=0;
690: STRING_PREPARED_FOREACH_ROW(*this,
691: if(row->item.lang==joined_lang)
692: joined_size+=row->item.size;
693: else
694: break; // before non-ours
695: );
696: current+=joined_size;
697:
698: // pointers are after joined piece
699: // & one step back, see STRING_FOREACH_ROW
700: --row; ++countdown;
701:
702: result.APPEND(joined_ptr, joined_size, joined_lang,
1.150 paf 703: joined_origin_file, joined_origin_line);
1.123 paf 704: );
1.156 paf 705:
706: if(acstr)
707: *acstr=lcstr;
1.89 parser 708: return result;
709: }
710:
1.90 parser 711: double String::as_double() const {
1.89 parser 712: double result;
1.114 paf 713: const char *cstr;
714: char buf[MAX_NUMBER];
1.151 paf 715: if(head.chunk.rows+1==append_here) {
716: int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
717: memcpy(buf, head.chunk.rows[0].item.ptr, size);
1.114 paf 718: buf[size]=0;
719: cstr=buf;
720: } else
721: 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=(double)(unsigned long)strtol(cstr, &error_pos, 0);
727: else
1.102 parser 728: result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos);
1.89 parser 729: else
1.99 parser 730: result=(double)strtod(cstr, &error_pos);
1.89 parser 731:
1.103 parser 732: if(*error_pos/*not EOS*/)
1.149 paf 733: throw Exception("number.format",
1.89 parser 734: this,
735: "invalid number (double)");
736:
737: return result;
738: }
1.90 parser 739: int String::as_int() const {
1.89 parser 740: int result;
1.114 paf 741: const char *cstr;
742: char buf[MAX_NUMBER];
1.151 paf 743: if(head.chunk.rows+1==append_here) {
744: int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1);
745: memcpy(buf, head.chunk.rows[0].item.ptr, size);
1.114 paf 746: buf[size]=0;
747: cstr=buf;
748: } else
749: cstr=this->cstr();
1.102 parser 750: char *error_pos;
1.89 parser 751: // 0xABC
1.99 parser 752: if(cstr[0]=='0')
753: if(cstr[1]=='x' || cstr[1]=='X')
754: result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
755: else
1.102 parser 756: result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0);
1.89 parser 757: else
758: result=(int)strtol(cstr, &error_pos, 0);
759:
1.103 parser 760: if(*error_pos/*not EOS*/)
1.149 paf 761: throw Exception("number.format",
1.89 parser 762: this,
763: "invalid number (int)");
1.82 parser 764:
765: return result;
1.61 paf 766: }
1.113 parser 767:
1.128 paf 768: inline void ushort2uchars(ushort word, uchar& byte1, uchar& byte2) {
769: byte1=word&0xFF;
770: byte2=word>>8;
771: }
772: inline ushort uchars2ushort(uchar byte1, uchar byte2) {
773: return (byte2<<8) | byte1;
774: }
1.113 parser 775: /* @todo maybe network order worth spending some effort?
776: don't bothering myself with network byte order,
777: am not planning to be able to move resulting file across platforms
778: for now
779: */
780: void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const {
781: buf_size=
782: prolog_size
1.126 paf 783: +used_rows()*(sizeof(uchar)+sizeof(ushort))
1.113 parser 784: +size();
1.114 paf 785: buf=malloc(buf_size,15);
1.113 parser 786: char *cur=(char *)buf+prolog_size;
787:
1.123 paf 788: STRING_FOREACH_ROW(
789: // lang
1.126 paf 790: memcpy(cur, &row->item.lang, sizeof(uchar));
791: cur+=sizeof(uchar);
1.123 paf 792: // size
1.128 paf 793: uchar byte1; uchar byte2;
794: ushort2uchars(row->item.size, byte1, byte2);
795: memcpy(cur, &byte1, sizeof(uchar)); cur+=sizeof(uchar);
796: memcpy(cur, &byte2, sizeof(uchar)); cur+=sizeof(uchar);
1.123 paf 797: // bytes
798: memcpy(cur, row->item.ptr, row->item.size);
799: cur+=row->item.size;
800: );
1.113 parser 801: }
1.148 paf 802: bool String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) {
1.135 paf 803: if(buf_size<=prolog_size)
1.148 paf 804: return false;
1.135 paf 805:
1.126 paf 806: char *cur=(char *)buf+prolog_size;
1.113 parser 807: buf_size-=prolog_size;
808:
809: while(buf_size) {
1.148 paf 810: if(sizeof(uchar)+sizeof(ushort)>buf_size) // lang+size
811: return false;
812:
813: uchar lang=*(uchar *)(cur);
1.128 paf 814: ushort size=uchars2ushort(
815: *(uchar*)(cur+sizeof(uchar)*1),
816: *(uchar*)(cur+sizeof(uchar)*2)
817: );
818:
1.148 paf 819: size_t piece_size=sizeof(uchar)+sizeof(ushort)+size;
820: if(piece_size>buf_size) // buffer overrun, can be on incomplete cache files
821: return false;
822:
1.128 paf 823: const char *ptr=(const char*)(cur+sizeof(uchar)*3);
1.126 paf 824: APPEND(ptr, size, lang, file, 0);
1.113 parser 825:
826: cur+=piece_size;
827: buf_size-=piece_size;
828: }
1.148 paf 829: return true;
1.113 parser 830: }
E-mail: