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