Annotation of parser3/src/main/pa_string.C, revision 1.92
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.46 paf 5:
1.37 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.36 paf 7:
1.92 ! parser 8: $Id: pa_string.C,v 1.91 2001/05/23 08:57:39 parser Exp $
1.4 paf 9: */
1.92 ! parser 10: static char *RCSId="$Id$";
1.4 paf 11:
1.48 paf 12: #include "pa_config_includes.h"
1.1 paf 13:
1.70 paf 14: #include "pcre.h"
1.82 parser 15: #include "internal.h"
1.70 paf 16:
1.13 paf 17: #include "pa_pool.h"
1.12 paf 18: #include "pa_string.h"
1.5 paf 19: #include "pa_hash.h"
1.22 paf 20: #include "pa_exception.h"
1.53 paf 21: #include "pa_common.h"
1.60 paf 22: #include "pa_array.h"
23: #include "pa_globals.h"
1.61 paf 24: #include "pa_table.h"
1.60 paf 25:
1.75 paf 26: String::String(Pool& apool, const char *src, size_t src_size, bool tainted) :
1.84 parser 27: Pooled(apool) {
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:
42: void String::expand() {
1.85 parser 43: size_t new_chunk_count=last_chunk->count+CR_GROW_COUNT;
1.28 paf 44: last_chunk=static_cast<Chunk *>(
1.55 paf 45: malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
1.28 paf 46: last_chunk->count=new_chunk_count;
47: link_row->link=last_chunk;
48: append_here=last_chunk->rows;
49: link_row=&last_chunk->rows[last_chunk->count];
1.8 paf 50: link_row->link=0;
1.1 paf 51: }
52:
1.40 paf 53: String::String(const String& src) : Pooled(src.pool()) {
1.8 paf 54: head.count=CR_PREALLOCATED_COUNT;
55:
1.55 paf 56: size_t src_used_rows=src.fused_rows;
1.8 paf 57: if(src_used_rows<=head.count) {
1.55 paf 58: // all new rows fit size_to preallocated area
59: size_t curr_chunk_rows=head.count;
1.8 paf 60: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
61: append_here=&head.rows[src_used_rows];
62: link_row=&head.rows[curr_chunk_rows];
63: } else {
64: // warning:
1.10 paf 65: // heavily relies on the fact
66: // "preallocated area is the same for all strings"
1.8 paf 67: //
68: // info:
69: // allocating only enough mem to fit src string rows
70: // next append would allocate a new chunk
71: //
1.55 paf 72: // new rows don't fit size_to preallocated area: splitting size_to two chunks
1.8 paf 73: // preallocated chunk src to constructing head
74: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
1.55 paf 75: // remaining rows size_to new_chunk
76: size_t curr_chunk_rows=src_used_rows-head.count;
1.8 paf 77: Chunk *new_chunk=static_cast<Chunk *>(
1.55 paf 78: malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.8 paf 79: new_chunk->count=curr_chunk_rows;
80: head.preallocated_link=new_chunk;
1.28 paf 81: append_here=link_row=&new_chunk->rows[new_chunk->count];
1.8 paf 82:
83: Chunk *old_chunk=src.head.preallocated_link;
84: Chunk::Row *new_rows=new_chunk->rows;
1.55 paf 85: size_t rows_left_to_copy=new_chunk->count;
1.8 paf 86: while(true) {
1.55 paf 87: size_t old_count=old_chunk->count;
1.8 paf 88: Chunk *next_chunk=old_chunk->rows[old_count].link;
89: if(next_chunk) {
90: // not last source chunk
91: // taking it all
92: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
93: new_rows+=old_count;
94: rows_left_to_copy-=old_count;
95:
96: old_chunk=next_chunk;
97: } else {
98: // the last source chunk
99: // taking only those rows of chunk that _left_to_copy
100: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
101: break;
102: }
103: }
1.5 paf 104: }
1.8 paf 105: link_row->link=0;
106: fused_rows=src_used_rows;
107: fsize=src.fsize;
1.5 paf 108: }
1.28 paf 109:
1.42 paf 110: String& String::append(const String& src, Untaint_lang lang, bool forced) {
1.60 paf 111: const Chunk *chunk=&src.head;
1.40 paf 112: do {
1.60 paf 113: const Chunk::Row *row=chunk->rows;
114: for(size_t i=0; i<chunk->count; i++, row++) {
115: if(row==src.append_here)
1.40 paf 116: goto break2;
1.60 paf 117:
118: APPEND(row->item.ptr, row->item.size,
119: (lang!=UL_PASS_APPENDED && (row->item.lang==UL_TAINTED || forced))?lang:row->item.lang,
120: row->item.origin.file, row->item.origin.line);
1.40 paf 121: }
122: chunk=row->link;
123: } while(chunk);
124: break2:
1.60 paf 125: return *this;
1.34 paf 126: }
1.60 paf 127:
1.13 paf 128: String& String::real_append(STRING_APPEND_PARAMS) {
1.9 paf 129: if(!src)
130: return *this;
1.26 paf 131: if(!size)
132: size=strlen(src);
133: if(!size)
1.9 paf 134: return *this;
135:
1.1 paf 136: if(chunk_is_full())
137: expand();
138:
139: append_here->item.ptr=src;
1.26 paf 140: fsize+=append_here->item.size=size;
1.52 paf 141: append_here->item.lang=lang;
1.13 paf 142: #ifndef NO_STRING_ORIGIN
1.14 paf 143: append_here->item.origin.file=file;
144: append_here->item.origin.line=line;
1.13 paf 145: #endif
1.8 paf 146: append_here++; fused_rows++;
1.1 paf 147:
148: return *this;
149: }
150:
1.16 paf 151: uint String::hash_code() const {
1.7 paf 152: uint result=0;
1.5 paf 153:
1.16 paf 154: const Chunk *chunk=&head;
1.5 paf 155: do {
1.16 paf 156: const Chunk::Row *row=chunk->rows;
1.55 paf 157: for(size_t i=0; i<chunk->count; i++) {
1.5 paf 158: if(row==append_here)
159: goto break2;
160:
1.6 paf 161: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 162: row++;
163: }
164: chunk=row->link;
165: } while(chunk);
166: break2:
167: return result;
168: }
169:
1.60 paf 170: /// @todo move 'lang' skipping to pos
171: int String::cmp(int& partial, const String& src,
172: size_t this_offset, Untaint_lang lang) const {
1.59 paf 173: partial=-1;
1.55 paf 174: this_offset=min(this_offset, size()-1);
175:
1.16 paf 176: const Chunk *a_chunk=&head;
177: const Chunk *b_chunk=&src.head;
178: const Chunk::Row *a_row=a_chunk->rows;
179: const Chunk::Row *b_row=b_chunk->rows;
1.55 paf 180: size_t a_offset=this_offset;
181: size_t b_offset=0;
1.9 paf 182: Chunk::Row *a_end=append_here;
183: Chunk::Row *b_end=src.append_here;
1.55 paf 184: size_t a_countdown=a_chunk->count;
185: size_t b_countdown=b_chunk->count;
186: size_t result;
1.60 paf 187: size_t pos=0;
1.33 paf 188:
1.83 parser 189: bool a_break=size()==0;
1.91 parser 190: bool b_break=src.size()==0;
1.83 parser 191: if(!(a_break || b_break)) while(true) {
1.55 paf 192: if(pos+a_row->item.size > this_offset) {
1.71 paf 193: if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang)
1.60 paf 194: return -1; // wrong lang -- bail out
195:
1.55 paf 196: int size_diff=
197: (a_row->item.size-a_offset)-
198: (b_row->item.size-b_offset);
199:
200: if(size_diff==0) { // a has same size as b
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;
1.60 paf 205: pos+=a_row->item.size;
1.55 paf 206: a_row++; a_countdown--; a_offset=0;
207: b_row++; b_countdown--; b_offset=0;
208: } else if (size_diff>0) { // a longer
1.60 paf 209: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset,
210: b_row->item.size-b_offset);
1.55 paf 211: if(result)
212: return result;
213: a_offset+=b_row->item.size-b_offset;
214: b_row++; b_countdown--; b_offset=0;
215: } else { // b longer
1.60 paf 216: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset,
217: a_row->item.size-a_offset);
1.55 paf 218: if(result)
219: return result;
220: b_offset+=a_row->item.size-a_offset;
1.60 paf 221: pos+=a_row->item.size;
1.55 paf 222: a_row++; a_countdown--; a_offset=0;
223: }
1.83 parser 224: if(b_break=b_row==b_end) {
225: a_break=a_row==a_end;
226: break;
227: }
1.55 paf 228: if(!b_countdown) {
229: b_chunk=b_row->link;
230: b_row=b_chunk->rows;
231: b_countdown=b_chunk->count;
232: }
233: } else {
1.60 paf 234: a_offset-=a_row->item.size;
235: pos+=a_row->item.size;
236: a_row++; a_countdown--;
1.9 paf 237: }
238:
1.83 parser 239: if(a_break=a_row==a_end) {
240: b_break=b_row==b_end;
241: break;
242: }
1.11 paf 243: if(!a_countdown) {
1.9 paf 244: a_chunk=a_row->link;
245: a_row=a_chunk->rows;
1.11 paf 246: a_countdown=a_chunk->count;
1.9 paf 247: }
1.27 paf 248: }
1.55 paf 249: if(a_break==b_break) { // ended simultaneously
250: partial=0; return 0;
251: } else if(a_break) { // first bytes equal, but a ended before b
252: partial=1; return -1;
253: } else {
254: partial=2; return +1;
255: }
1.27 paf 256: }
257:
1.60 paf 258: /// @todo move 'lang' skipping to pos
1.59 paf 259: int String::cmp(int& partial, const char* b_ptr, size_t src_size,
1.60 paf 260: size_t this_offset, Untaint_lang lang) const {
1.59 paf 261: partial=-1;
1.50 paf 262: size_t b_size=src_size?src_size:b_ptr?strlen(b_ptr):0;
1.59 paf 263: this_offset=min(this_offset, size()-1);
1.27 paf 264:
265: const Chunk *a_chunk=&head;
266: const Chunk::Row *a_row=a_chunk->rows;
1.59 paf 267: size_t a_offset=this_offset;
1.55 paf 268: size_t b_offset=0;
1.27 paf 269: Chunk::Row *a_end=append_here;
1.55 paf 270: size_t a_countdown=a_chunk->count;
1.60 paf 271: size_t pos=0;
1.52 paf 272:
1.83 parser 273: bool a_break=size()==0;
274: bool b_break=b_size==0;
275: if(!(a_break || b_break)) while(true) {
1.59 paf 276: if(pos+a_row->item.size > this_offset) {
1.71 paf 277: if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang)
1.60 paf 278: return -1; // wrong lang -- bail out
279:
1.59 paf 280: int size_diff=
281: (a_row->item.size-a_offset)-
282: (b_size-b_offset);
283:
284: if(size_diff==0) { // a has same size as b
285: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
286: a_row->item.size-a_offset)!=0)
287: return result;
1.60 paf 288: pos+=a_row->item.size;
1.59 paf 289: a_row++; a_countdown--; a_offset=0;
290: b_break=true;
291: } else if (size_diff>0) { // a longer
292: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
293: b_size-b_offset)!=0)
294: return result;
295: a_offset+=b_size-b_offset;
296: b_break=true;
297: } else { // b longer
298: if(size_t result=memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset,
299: a_row->item.size-a_offset)!=0)
300: return result;
301: b_offset+=a_row->item.size-a_offset;
1.60 paf 302: pos+=a_row->item.size;
1.59 paf 303: a_row++; a_countdown--; a_offset=0;
304: }
305: } else {
1.60 paf 306: a_offset-=a_row->item.size;
307: pos+=a_row->item.size;
308: a_row++; a_countdown--;
1.27 paf 309: }
310:
1.86 parser 311: a_break=a_row==a_end;
312: if(a_break || b_break)
1.83 parser 313: break;
1.27 paf 314: if(!a_countdown) {
315: a_chunk=a_row->link;
316: a_row=a_chunk->rows;
317: a_countdown=a_chunk->count;
1.9 paf 318: }
319: }
1.55 paf 320: if(a_break==b_break) { // ended simultaneously
321: partial=0; return 0;
322: } else if(a_break) { // first bytes equal, but a ended before b
323: partial=1; return -1;
324: } else {
325: partial=2; return +1;
326: }
1.5 paf 327: }
1.46 paf 328:
329: #ifndef NO_STRING_ORIGIN
330: const Origin& String::origin() const {
331: if(!fused_rows)
332: THROW(0, 0,
1.50 paf 333: 0,
334: "String::origin() of empty string called");
1.46 paf 335:
1.49 paf 336: // determining origin by last appended piece
1.50 paf 337: // because first one frequently constant.
338: // ex: ^load[/file] "document_root" + "/file"
1.80 paf 339: // when last peice is constant,
340: // ex: parser_root_auto_path{dynamic} / auto.p{const}
341: // using first piece
342: Origin& last_origin=append_here[-1].item.origin;
343: return last_origin.file ? last_origin : head.rows[0].item.origin;
1.46 paf 344: }
345: #endif
1.53 paf 346:
1.69 paf 347: String& String::mid(size_t start, size_t finish) const {
1.53 paf 348: start=max(0, start);
349: finish=min(size(), finish);
1.60 paf 350: if(start==finish)
351: return *empty_string;
1.53 paf 352:
353: String& result=*NEW String(pool());
354:
355: size_t pos=0;
356: const Chunk *chunk=&head;
357: do {
358: const Chunk::Row *row=chunk->rows;
1.55 paf 359: for(size_t i=0; i<chunk->count; pos+=row->item.size, i++, row++) {
1.53 paf 360: if(row==append_here)
361: goto break2;
362:
1.60 paf 363: size_t item_finish=pos+row->item.size;
364: if(item_finish > start) { // started now or already?
365: bool started=result.size()==0; // started now?
366: bool finished=finish <= item_finish; // finished now?
1.53 paf 367: size_t offset=started?start-pos:0;
368: size_t size=finished?finish-pos:row->item.size;
369: result.APPEND(
370: row->item.ptr+offset, size-offset,
371: row->item.lang,
372: row->item.origin.file, row->item.origin.line);
373: if(finished)
374: goto break2;
375: }
376: }
377: chunk=row->link;
378: } while(chunk);
379: break2:
1.60 paf 380: // SAPI::log(pool(), "piece of '%s' from %d to %d is '%s'",
381: //cstr(), start, finish, result.cstr());
1.53 paf 382: return result;
1.54 paf 383: }
384:
1.60 paf 385: int String::pos(const String& substr,
386: size_t result, Untaint_lang lang) const {
1.58 paf 387: for(; result<size(); result++) {
1.60 paf 388: int partial; cmp(partial, substr, result, lang);
1.58 paf 389: if(
390: partial==0 || // full match
391: partial==2) // 'substr' starts 'this'+'result'
392: return result;
393: }
394:
395: return -1;
396: }
397:
1.60 paf 398: int String::pos(const char *substr, size_t substr_size,
399: size_t result, Untaint_lang lang) const {
1.57 paf 400: for(; result<size(); result++) {
1.60 paf 401: int partial; cmp(partial, substr, substr_size, result, lang);
1.55 paf 402: if(
403: partial==0 || // full match
404: partial==2) // 'substr' starts 'this'+'result'
405: return result;
406: }
407:
408: return -1;
1.60 paf 409: }
410:
411: void String::split(Array& result,
412: size_t* pos_after_ref,
413: const char *delim, size_t delim_size,
414: Untaint_lang lang, int limit) const {
415: if(delim_size) {
416: size_t pos_after=pos_after_ref?*pos_after_ref:0;
417: int pos_before;
418: // while we have 'delim'...
419: for(; (pos_before=pos(delim, delim_size, pos_after, lang))>=0 && limit; limit--) {
1.69 paf 420: result+=&mid(pos_after, pos_before);
1.60 paf 421: pos_after=pos_before+delim_size;
422: }
423: // last piece
424: if(pos_after<size() && limit) {
1.69 paf 425: result+=&mid(pos_after, size());
1.60 paf 426: pos_after=size();
427: }
428: if(pos_after_ref)
429: *pos_after_ref=pos_after;
430: } else { // empty delim
431: result+=this;
432: if(pos_after_ref)
433: *pos_after_ref+=size();
434: }
435: }
436:
437: void String::split(Array& result,
438: size_t* pos_after_ref,
439: const String& delim, Untaint_lang lang,
440: int limit) const {
441: if(delim.size()) {
442: size_t pos_after=pos_after_ref?*pos_after_ref:0;
443: int pos_before;
444: // while we have 'delim'...
445: for(; (pos_before=pos(delim, pos_after, lang))>=0 && limit; limit--) {
1.69 paf 446: result+=&mid(pos_after, pos_before);
1.60 paf 447: pos_after=pos_before+delim.size();
448: }
449: // last piece
450: if(pos_after<size() && limit) {
1.69 paf 451: result+=&mid(pos_after, size());
1.60 paf 452: pos_after=size();
453: }
454: if(pos_after_ref)
455: *pos_after_ref=pos_after;
456: } else { // empty delim
457: result+=this;
458: if(pos_after_ref)
459: *pos_after_ref+=size();
460: }
1.61 paf 461: }
462:
1.63 paf 463: static void regex_options(char *options, int *result){
464: struct Regex_option {
465: char key;
466: int clear, set;
467: int *result;
468: } regex_option[]={
469: {'i', 0, PCRE_CASELESS, result}, // a=A
1.79 paf 470: {'s', 0, PCRE_DOTALL, result}, // \n\n$ [default]
1.63 paf 471: {'x', 0, PCRE_EXTENDED, result}, // whitespace in regex ignored
472: {'m', PCRE_DOTALL, PCRE_MULTILINE, result}, // ^aaa\n$^bbb\n$
473: {'g', 0, true, result+1}, // many rows
474: {0},
475: };
476: result[0]=PCRE_EXTRA | PCRE_DOTALL;
477: result[1]=0;
478:
479: if(options)
480: for(Regex_option *o=regex_option; o->key; o++)
481: if(
482: strchr(options, o->key) ||
483: strchr(options, toupper(o->key))) {
484: *(o->result)&=~o->clear;
485: *(o->result)|=o->set;
486: }
487: }
488:
1.88 parser 489: /// @todo maybe need speedup: some option to remove pre/match/post string generation
1.77 paf 490: bool String::match(const unsigned char *pcre_tables,
491: const String *aorigin,
1.62 paf 492: const String& regexp,
1.63 paf 493: const String *options,
1.64 paf 494: Table **table,
495: Row_action row_action, void *info) const {
496:
1.73 paf 497: if(!regexp.size())
498: THROW(0, 0,
499: aorigin,
500: "regexp is empty");
1.68 paf 501: const char *pattern=regexp.cstr(UL_AS_IS);
1.62 paf 502: const char *errptr;
503: int erroffset;
1.63 paf 504: int option_bits[2]; regex_options(options?options->cstr():0, option_bits);
505: pcre *code=pcre_compile(pattern, option_bits[0],
1.62 paf 506: &errptr, &erroffset,
1.74 paf 507: pcre_tables);
1.62 paf 508:
1.67 paf 509: if(!code)
1.62 paf 510: THROW(0, 0,
1.69 paf 511: ®exp.mid(erroffset, regexp.size()),
1.74 paf 512: "regular expression syntax error - %s", errptr);
1.62 paf 513:
1.63 paf 514: int info_substrings=pcre_info(code, 0, 0);
515: if(info_substrings<0) {
516: (*pcre_free)(code);
517: THROW(0, 0,
1.73 paf 518: aorigin,
1.76 paf 519: "pcre_info error (%d)",
1.73 paf 520: info_substrings);
1.63 paf 521: }
522:
523: int startoffset=0;
1.68 paf 524: const char *subject=cstr(UL_AS_IS);
1.62 paf 525: int length=strlen(subject);
1.63 paf 526: int ovecsize;
527: int *ovector=(int *)malloc(sizeof(int)*
1.65 paf 528: (ovecsize=(1/*match*/+info_substrings)*3));
1.62 paf 529:
1.64 paf 530: { // create table
531: Array& columns=*NEW Array(pool());
532: columns+=string_pre_match_name;
533: columns+=string_match_name;
534: columns+=string_post_match_name;
535: for(int i=1; i<=info_substrings; i++) {
536: char *column=(char *)malloc(MAX_NUMBER);
537: snprintf(column, MAX_NUMBER, "%d", i);
538: columns+=NEW String(pool(), column); // .i column name
539: }
540: *table=NEW Table(pool(), aorigin, &columns);
1.62 paf 541: }
1.63 paf 542:
1.64 paf 543: int exec_option_bits=0;
1.63 paf 544: while(true) {
545: int exec_substrings=pcre_exec(code, 0,
546: subject, length, startoffset,
1.64 paf 547: exec_option_bits, ovector, ovecsize);
1.63 paf 548:
549: if(exec_substrings==PCRE_ERROR_NOMATCH) {
550: (*pcre_free)(code);
1.67 paf 551: (*row_action)(**table, 0/*last time, no row*/, 0, 0, info);
1.63 paf 552: return option_bits[1]!=0; // global=true+table, not global=false
553: }
554:
555: if(exec_substrings<0) {
556: (*pcre_free)(code);
557: THROW(0, 0,
558: aorigin,
1.76 paf 559: "regular expression execute error (%d)",
1.63 paf 560: exec_substrings);
561: }
562:
563: Array& row=*NEW Array(pool());
1.81 paf 564: row+=&mid(0, ovector[0]); // .prematch column value
1.69 paf 565: row+=&mid(ovector[0], ovector[1]); // .match
1.81 paf 566: row+=&mid(ovector[1], size()); // .postmatch
1.63 paf 567:
568: for(int i=1; i<exec_substrings; i++) {
1.69 paf 569: // -1:-1 case handled peacefully by mid() itself
570: row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value
1.63 paf 571: }
572:
1.67 paf 573: (*row_action)(**table, &row, startoffset, ovector[0], info);
1.63 paf 574:
1.67 paf 575: if(!option_bits[1] || !(startoffset=ovector[1])) { // not global | going to hang
1.63 paf 576: (*pcre_free)(code);
1.67 paf 577: (*row_action)(**table, 0/*last time, no row*/, 0, 0, info);
1.63 paf 578: return true;
579: }
580:
581: /*
582: if(option_bits[0] & PCRE_MULTILINE)
1.64 paf 583: exec_option_bits|=PCRE_NOTBOL; // start of subject+startoffset not BOL
1.63 paf 584: */
585: }
1.82 parser 586: }
587:
588: String& String::change_case(Pool& pool, const unsigned char *tables,
589: Change_case_kind kind) const {
590: String& result=*new(pool) String(pool);
591:
592: const unsigned char *a;
593: const unsigned char *b;
594: switch(kind) {
595: case CC_UPPER:
596: a=tables+lcc_offset;
597: b=tables+fcc_offset;
598: break;
599: case CC_LOWER:
600: a=tables+lcc_offset;
601: b=0;
602: break;
603: default:
604: PTHROW(0, 0,
605: this,
606: "unknown change case kind #%d",
607: static_cast<int>(kind)); // never
608: a=b=0; // calm, compiler
609: break; // never
610: }
611:
612: const Chunk *chunk=&head;
613: do {
614: const Chunk::Row *row=chunk->rows;
615: for(size_t i=0; i<chunk->count; i++, row++) {
616: if(row==append_here)
617: goto break2;
618:
619: char *new_cstr=(char *)pool.malloc(row->item.size);
620: char *dest=new_cstr;
621: const char *src=row->item.ptr;
622: for(int size=row->item.size; size--; src++) {
623: unsigned char c=a[(unsigned char)*src];
624: if(b)
625: c=b[c];
626:
627: *dest++=(char)c;
628: }
629:
630: result.APPEND(new_cstr, row->item.size,
631: row->item.lang,
632: row->item.origin.file, row->item.origin.line);
633: }
634: chunk=row->link;
635: } while(chunk);
636: break2:
1.89 parser 637:
638: return result;
639: }
640:
1.90 parser 641: double String::as_double() const {
1.89 parser 642: double result;
643: const char *cstr=this->cstr();
644: char *error_pos=0;
645: // 0xABC
646: if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X'))
647: result=(double)(unsigned long)strtol(cstr, &error_pos, 0);
648: else
649: result=strtod(cstr, &error_pos);
650:
651: if(error_pos && *error_pos)
652: THROW(0, 0,
653: this,
654: "invalid number (double)");
655:
656: return result;
657: }
1.90 parser 658: int String::as_int() const {
1.89 parser 659: int result;
660: const char *cstr=this->cstr();
661: char *error_pos=0;
662: // 0xABC
663: if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X'))
664: result=(int)(unsigned long)strtol(cstr, &error_pos, 0);
665: else
666: result=(int)strtol(cstr, &error_pos, 0);
667:
668: if(error_pos && *error_pos)
669: THROW(0, 0,
670: this,
671: "invalid number (int)");
1.82 parser 672:
673: return result;
1.61 paf 674: }
E-mail: