|
|
| version 1.90, 2001/05/21 16:39:32 | version 1.106, 2001/10/05 16:12:40 |
|---|---|
| Line 2 | Line 2 |
| Parser: string class. @see untasize_t.C. | Parser: string class. @see untasize_t.C. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | $Id$ |
| Line 21 | Line 20 |
| #include "pa_array.h" | #include "pa_array.h" |
| #include "pa_globals.h" | #include "pa_globals.h" |
| #include "pa_table.h" | #include "pa_table.h" |
| #include "pa_dictionary.h" | |
| String::String(Pool& apool, const char *src, size_t src_size, bool tainted) : | String::String(Pool& apool, const char *src, size_t src_size, bool tainted) : |
| Pooled(apool) { | Pooled(apool), |
| forigins_mode(false) { | |
| last_chunk=&head; | last_chunk=&head; |
| head.count=CR_PREALLOCATED_COUNT; | head.count=CR_PREALLOCATED_COUNT; |
| append_here=head.rows; | append_here=head.rows; |
| Line 38 String::String(Pool& apool, const char * | Line 39 String::String(Pool& apool, const char * |
| APPEND_CLEAN(src, src_size, 0, 0); | APPEND_CLEAN(src, src_size, 0, 0); |
| } | } |
| void String::expand() { | String::String(const String& src) : |
| size_t new_chunk_count=last_chunk->count+CR_GROW_COUNT; | Pooled(src.pool()), |
| last_chunk=static_cast<Chunk *>( | forigins_mode(false) { |
| malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *))); | |
| last_chunk->count=new_chunk_count; | |
| link_row->link=last_chunk; | |
| append_here=last_chunk->rows; | |
| link_row=&last_chunk->rows[last_chunk->count]; | |
| link_row->link=0; | |
| } | |
| String::String(const String& src) : Pooled(src.pool()) { | |
| head.count=CR_PREALLOCATED_COUNT; | head.count=CR_PREALLOCATED_COUNT; |
| size_t src_used_rows=src.fused_rows; | size_t src_used_rows=src.fused_rows; |
| if(src_used_rows<=head.count) { | if(src_used_rows<=head.count) { |
| // all new rows fit size_to preallocated area | // all new rows fit size_to preallocated area |
| last_chunk=&head; | |
| size_t curr_chunk_rows=head.count; | size_t curr_chunk_rows=head.count; |
| memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows); | memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows); |
| append_here=&head.rows[src_used_rows]; | append_here=&head.rows[src_used_rows]; |
| Line 73 String::String(const String& src) : Pool | Line 66 String::String(const String& src) : Pool |
| memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count); | memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count); |
| // remaining rows size_to new_chunk | // remaining rows size_to new_chunk |
| size_t curr_chunk_rows=src_used_rows-head.count; | size_t curr_chunk_rows=src_used_rows-head.count; |
| Chunk *new_chunk=static_cast<Chunk *>( | last_chunk=static_cast<Chunk *>( |
| malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); | malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); |
| new_chunk->count=curr_chunk_rows; | last_chunk->count=curr_chunk_rows; |
| head.preallocated_link=new_chunk; | head.preallocated_link=last_chunk; |
| append_here=link_row=&new_chunk->rows[new_chunk->count]; | append_here=link_row=&last_chunk->rows[last_chunk->count]; |
| Chunk *old_chunk=src.head.preallocated_link; | Chunk *old_chunk=src.head.preallocated_link; |
| Chunk::Row *new_rows=new_chunk->rows; | Chunk::Row *new_rows=last_chunk->rows; |
| size_t rows_left_to_copy=new_chunk->count; | size_t rows_left_to_copy=last_chunk->count; |
| while(true) { | while(true) { |
| size_t old_count=old_chunk->count; | size_t old_count=old_chunk->count; |
| Chunk *next_chunk=old_chunk->rows[old_count].link; | Chunk *next_chunk=old_chunk->rows[old_count].link; |
| Line 106 String::String(const String& src) : Pool | Line 99 String::String(const String& src) : Pool |
| fsize=src.fsize; | fsize=src.fsize; |
| } | } |
| void String::expand() { | |
| size_t new_chunk_count=last_chunk->count+CR_GROW_COUNT; | |
| last_chunk=static_cast<Chunk *>( | |
| malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *))); | |
| last_chunk->count=new_chunk_count; | |
| link_row->link=last_chunk; | |
| append_here=last_chunk->rows; | |
| link_row=&last_chunk->rows[last_chunk->count]; | |
| link_row->link=0; | |
| } | |
| String& String::append(const String& src, Untaint_lang lang, bool forced) { | String& String::append(const String& src, Untaint_lang lang, bool forced) { |
| const Chunk *chunk=&src.head; | const Chunk *chunk=&src.head; |
| do { | do { |
| Line 147 String& String::real_append(STRING_APPEN | Line 151 String& String::real_append(STRING_APPEN |
| return *this; | return *this; |
| } | } |
| char String::first_char() const { | |
| if(!fused_rows) | |
| THROW(0, 0, | |
| this, | |
| "getting first char of empty string"); | |
| return *head.rows[0].item.ptr; | |
| } | |
| uint String::hash_code() const { | uint String::hash_code() const { |
| uint result=0; | uint result=0; |
| Line 186 int String::cmp(int& partial, const Stri | Line 199 int String::cmp(int& partial, const Stri |
| size_t pos=0; | size_t pos=0; |
| bool a_break=size()==0; | bool a_break=size()==0; |
| bool b_break=size()==0; | bool b_break=src.size()==0; |
| if(!(a_break || b_break)) while(true) { | if(!(a_break || b_break)) while(true) { |
| if(pos+a_row->item.size > this_offset) { | if(pos+a_row->item.size > this_offset) { |
| if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) | if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) |
| Line 327 int String::cmp(int& partial, const char | Line 340 int String::cmp(int& partial, const char |
| #ifndef NO_STRING_ORIGIN | #ifndef NO_STRING_ORIGIN |
| const Origin& String::origin() const { | const Origin& String::origin() const { |
| if(!fused_rows) | if(!fused_rows) { |
| THROW(0, 0, | static const Origin empty_origin={"empty string"}; |
| 0, | return empty_origin; |
| "String::origin() of empty string called"); | } |
| // determining origin by last appended piece | // determining origin by last appended piece |
| // because first one frequently constant. | // because first one frequently constant. |
| Line 491 bool String::match(const unsigned char * | Line 504 bool String::match(const unsigned char * |
| const String& regexp, | const String& regexp, |
| const String *options, | const String *options, |
| Table **table, | Table **table, |
| Row_action row_action, void *info) const { | Row_action row_action, void *info, |
| bool *was_global) const { | |
| if(!regexp.size()) | if(!regexp.size()) |
| THROW(0, 0, | THROW(0, 0, |
| Line 501 bool String::match(const unsigned char * | Line 515 bool String::match(const unsigned char * |
| const char *errptr; | const char *errptr; |
| int erroffset; | int erroffset; |
| int option_bits[2]; regex_options(options?options->cstr():0, option_bits); | int option_bits[2]; regex_options(options?options->cstr():0, option_bits); |
| if(was_global) | |
| *was_global=option_bits[1]!=0; | |
| pcre *code=pcre_compile(pattern, option_bits[0], | pcre *code=pcre_compile(pattern, option_bits[0], |
| &errptr, &erroffset, | &errptr, &erroffset, |
| pcre_tables); | pcre_tables); |
| Line 512 bool String::match(const unsigned char * | Line 528 bool String::match(const unsigned char * |
| int info_substrings=pcre_info(code, 0, 0); | int info_substrings=pcre_info(code, 0, 0); |
| if(info_substrings<0) { | if(info_substrings<0) { |
| (*pcre_free)(code); | pcre_free(code); |
| THROW(0, 0, | THROW(0, 0, |
| aorigin, | aorigin, |
| "pcre_info error (%d)", | "pcre_info error (%d)", |
| Line 546 bool String::match(const unsigned char * | Line 562 bool String::match(const unsigned char * |
| exec_option_bits, ovector, ovecsize); | exec_option_bits, ovector, ovecsize); |
| if(exec_substrings==PCRE_ERROR_NOMATCH) { | if(exec_substrings==PCRE_ERROR_NOMATCH) { |
| (*pcre_free)(code); | pcre_free(code); |
| (*row_action)(**table, 0/*last time, no row*/, 0, 0, info); | row_action(**table, 0/*last time, no row*/, 0, 0, info); |
| return option_bits[1]!=0; // global=true+table, not global=false | return option_bits[1]!=0; // global=true+table, not global=false |
| } | } |
| if(exec_substrings<0) { | if(exec_substrings<0) { |
| (*pcre_free)(code); | pcre_free(code); |
| THROW(0, 0, | THROW(0, 0, |
| aorigin, | aorigin, |
| "regular expression execute error (%d)", | "regular expression execute error (%d)", |
| Line 569 bool String::match(const unsigned char * | Line 585 bool String::match(const unsigned char * |
| row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value | row+=&mid(ovector[i*2+0], ovector[i*2+1]); // .i column value |
| } | } |
| (*row_action)(**table, &row, startoffset, ovector[0], info); | row_action(**table, &row, startoffset, ovector[0], info); |
| if(!option_bits[1] || !(startoffset=ovector[1])) { // not global | going to hang | if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang |
| (*pcre_free)(code); | pcre_free(code); |
| (*row_action)(**table, 0/*last time, no row*/, 0, 0, info); | row_action(**table, 0/*last time, no row*/, 0, 0, info); |
| return true; | return true; |
| } | } |
| startoffset=ovector[1]; | |
| /* | /* |
| if(option_bits[0] & PCRE_MULTILINE) | if(option_bits[0] & PCRE_MULTILINE) |
| Line 637 break2: | Line 654 break2: |
| return result; | return result; |
| } | } |
| /// @test restructure string to link pieces of same language together prior to this. | |
| String& String::replace(Pool& pool, Dictionary& dict) const { | |
| //_asm int 3; | |
| String& result=*new(pool) String(pool); | |
| const Chunk *chunk=&head; | |
| do { | |
| const Chunk::Row *row=chunk->rows; | |
| for(size_t i=0; i<chunk->count; i++, row++) { | |
| if(row==append_here) | |
| goto break2; | |
| const char *src=row->item.ptr; | |
| size_t src_size=row->item.size; | |
| char *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio())); | |
| char *dest=new_cstr; | |
| while(src_size) { | |
| // there is a row where first column starts 'src' | |
| if(Table::Item *item=dict.first_that_starts(src, src_size)) { | |
| // get a=>b values | |
| const String& a=*static_cast<Array *>(item)->get_string(0); | |
| const String& b=*static_cast<Array *>(item)->get_string(1); | |
| // skip 'a' in 'src' && reduce work size | |
| src+=a.size(); src_size-=a.size(); | |
| // write 'b' to 'dest' && skip 'b' in 'dest' | |
| b.store_to(dest); dest+=b.size(); | |
| } else { | |
| // write a char to b && reduce work size | |
| *dest++=*src++; src_size--; | |
| } | |
| } | |
| result.APPEND(new_cstr, dest-new_cstr, | |
| row->item.lang, | |
| row->item.origin.file, row->item.origin.line); | |
| } | |
| chunk=row->link; | |
| } while(chunk); | |
| break2: | |
| return result; | |
| } | |
| double String::as_double() const { | double String::as_double() const { |
| double result; | double result; |
| const char *cstr=this->cstr(); | const char *cstr=this->cstr(); |
| char *error_pos=0; | char *error_pos; |
| // 0xABC | // 0xABC |
| if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X')) | if(cstr[0]=='0') |
| result=(double)(unsigned long)strtol(cstr, &error_pos, 0); | if(cstr[1]=='x' || cstr[1]=='X') |
| result=(double)(unsigned long)strtol(cstr, &error_pos, 0); | |
| else | |
| result=(double)strtod(cstr+1/*skip leading 0*/, &error_pos); | |
| else | else |
| result=strtod(cstr, &error_pos); | result=(double)strtod(cstr, &error_pos); |
| if(error_pos && *error_pos) | if(*error_pos/*not EOS*/) |
| THROW(0, 0, | THROW(0, 0, |
| this, | this, |
| "invalid number (double)"); | "invalid number (double)"); |
| Line 657 double String::as_double() const { | Line 719 double String::as_double() const { |
| int String::as_int() const { | int String::as_int() const { |
| int result; | int result; |
| const char *cstr=this->cstr(); | const char *cstr=this->cstr(); |
| char *error_pos=0; | char *error_pos; |
| // 0xABC | // 0xABC |
| if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X')) | if(cstr[0]=='0') |
| result=(int)(unsigned long)strtol(cstr, &error_pos, 0); | if(cstr[1]=='x' || cstr[1]=='X') |
| result=(int)(unsigned long)strtol(cstr, &error_pos, 0); | |
| else | |
| result=(int)strtol(cstr+1/*skip leading 0*/, &error_pos, 0); | |
| else | else |
| result=(int)strtol(cstr, &error_pos, 0); | result=(int)strtol(cstr, &error_pos, 0); |
| if(error_pos && *error_pos) | if(*error_pos/*not EOS*/) |
| THROW(0, 0, | THROW(0, 0, |
| this, | this, |
| "invalid number (int)"); | "invalid number (int)"); |