--- parser3/src/main/pa_string.C 2003/01/23 15:38:06 1.172.2.2 +++ parser3/src/main/pa_string.C 2003/04/11 15:00:05 1.173 @@ -5,47 +5,45 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_STRING_C="$Date: 2003/01/23 15:38:06 $"; +static const char* IDENT_STRING_C="$Date: 2003/04/11 15:00:05 $"; #include "pcre.h" #include "pa_pool.h" #include "pa_string.h" -//#include "pa_hash.h" +#include "pa_hash.h" #include "pa_exception.h" -//#include "pa_common.h" +#include "pa_common.h" #include "pa_array.h" -//#include "pa_globals.h" +#include "pa_globals.h" #include "pa_table.h" #include "pa_dictionary.h" #include "pa_charset.h" -// helpers +#define DEBUG_STRING_APPENDS_VS_EXPANDS -/// String::match uses this as replace & global search table columns -const int MAX_STRING_MATCH_TABLE_COLUMNS=100; -class String_match_table_template_columns: public Array> { - char cn_cstr[MAX_STRING_MATCH_TABLE_COLUMNS][3/*strlen("100")*/+1/*terminating 0*/]; - String cn[MAX_STRING_MATCH_TABLE_COLUMNS]; -public: - string_match_table_template_columns_class() { - *this+=smart_ptr(new String("prematch")); - *this+=smart_ptr(new String("match")); - *this+=smart_ptr(new String("postmatch")); - for(int i=0; iitem.size; + ); + return result; +} + +/// @todo not very optimal +uint String::used_rows() const { + uint result=0; + STRING_FOREACH_ROW( + result++; + ); + return result; +} +void String::expand() { + uint new_chunk_count=last_chunk->count+CR_GROW_COUNT; + if(new_chunk_count>max_integral(Chunk::count_type)) + new_chunk_count=max_integral(Chunk::count_type); + + Chunk *new_chunk=static_cast(malloc( + sizeof(Chunk)// count+interpadding(?)+rows[CR_PREALLOCATED_COUNT]+tailpadding(??) + -sizeof(Chunk::rows_type) // PREALLOCATED rows + +sizeof(Chunk::Row)*new_chunk_count // neaded rows + +sizeof(Chunk *) // link size + , 10)); + new_chunk->rows[new_chunk->count=new_chunk_count].link=0; + last_chunk->rows[last_chunk->count].link=new_chunk; + + last_chunk=new_chunk; + append_here=last_chunk->rows; +} + String& String::real_append(STRING_APPEND_PARAMS) { + if(!last_chunk) // growth stopped [we're appended as string to somebody] + throw Exception(0, + this, + "string growth stopped (append cstr)"); + if(!src) return *this; if(!size) @@ -65,17 +107,39 @@ String& String::real_append(STRING_APPEN if(!size) return *this; - if(is_full()) - expand(fdelta); +#ifdef DEBUG_STRING_APPENDS_VS_EXPANDS + string_piece_appends++; +#endif + + // manually unrolled to avoid extra check + while(size>max_integral(Chunk::Row::item_size_type)) { + if(chunk_is_full()) + expand(); + + append_here->item.ptr=src; + append_here->item.size=max_integral(Chunk::Row::item_size_type); + append_here->item.lang=lang; +#ifndef NO_STRING_ORIGIN + append_here->item.origin.file=file; + append_here->item.origin.line=line; +#endif + append_here++; + + src+=max_integral(Chunk::Row::item_size_type); + size-=max_integral(Chunk::Row::item_size_type); + } + + if(chunk_is_full()) + expand(); - String_fragment *fragment=felements[fused++]; - fragment->item.ptr=src; - fragment->item.size=size; - fragment->item.lang=lang; + append_here->item.ptr=src; + append_here->item.size=size; + append_here->item.lang=lang; #ifndef NO_STRING_ORIGIN - fragment->item.origin.file=file; - fragment->item.origin.line=line; + append_here->item.origin.file=file; + append_here->item.origin.line=line; #endif + append_here++; return *this; } @@ -268,7 +332,7 @@ const Origin& String::origin() const { #endif String& String::mid(size_t start, size_t finish) const { - String& result=*new String(); + String& result=*NEW String(pool()); start=min(start, size()); finish=max(start, finish); @@ -412,13 +476,13 @@ static void regex_options(const String * } /// @todo make replacement Table stacked -bool String::match(Charset& source_charset, - const String *aorigin, - const String& regexp, - const String *options, - Table **table, - Row_action row_action, void *info, - bool *was_global) const { +bool String::match( + const String *aorigin, + const String& regexp, + const String *options, + Table **table, + Row_action row_action, void *info, + bool *was_global) const { if(regexp.is_empty()) throw Exception(0, @@ -428,13 +492,13 @@ bool String::match(Charset& source_chars const char *pattern=regexp.cstr(); const char *errptr; int erroffset; - bool need_pre_post_match=false; + bool need_pre_post_match=false; int option_bits[2]; regex_options(options, option_bits, need_pre_post_match); if(was_global) *was_global=option_bits[1]!=0; pcre *code=pcre_compile(pattern, option_bits[0], &errptr, &erroffset, - source_charset.pcre_tables); + pool().get_source_charset().pcre_tables); if(!code) throw Exception(0, @@ -456,7 +520,8 @@ bool String::match(Charset& source_chars int ovector[ovecsize]; // create table - *table=new Table(*string_match_table_template); + Table::Action_options table_options; + *table=NEW Table(pool(), *string_match_table_template, table_options); int exec_option_bits=0; int prestart=0; @@ -483,7 +548,7 @@ bool String::match(Charset& source_chars int prefinish=ovector[0]; poststart=ovector[1]; - Array& row=*new Array(); + Array& row=*NEW Array(pool()); row+=need_pre_post_match?&mid(0, prefinish):0; // .prematch column value row+=need_pre_post_match?&mid(prefinish, poststart):0; // .match row+=need_pre_post_match?&mid(poststart, postfinish):0; // .postmatch @@ -509,9 +574,10 @@ bool String::match(Charset& source_chars } } -String& String::change_case(Charset& source_charset, Change_case_kind kind) const { - const unsigned char *tables=source_charset.pcre_tables; - String& result=*new String(); +String& String::change_case(Pool& pool, + Change_case_kind kind) const { + const unsigned char *tables=pool.get_source_charset().pcre_tables; + String& result=*new(pool) String(pool); const unsigned char *a; const unsigned char *b; @@ -534,7 +600,7 @@ String& String::change_case(Charset& sou } STRING_FOREACH_ROW( - char *new_cstr=(char *)pa_malloc(row->item.size); + char *new_cstr=(char *)pool.malloc(row->item.size, 12); char *dest=new_cstr; const char *src=row->item.ptr; for(int size=row->item.size; size--; src++) { @@ -554,11 +620,11 @@ String& String::change_case(Charset& sou } /// @test if in some piece were found no dict words, append it, not it's duplicate -String& String::replace(Dictionary& dict) const { +String& String::replace(Pool& pool, Dictionary& dict) const { char *lcstr=cstr(); const char *current=lcstr; - String& result=*new String(); + String& result=*new(pool) String(pool); STRING_FOREACH_ROW( IFNDEF_NO_STRING_ORIGIN( const char *joined_origin_file=row->item.origin.file; @@ -580,7 +646,7 @@ IFNDEF_NO_STRING_ORIGIN( // & one step back, see STRING_FOREACH_ROW --row; ++countdown; - char *new_cstr=(char *)pa_malloc((size_t)ceil(joined_size*dict.max_ratio())); + char *new_cstr=(char *)pool.malloc((size_t)ceil(joined_size*dict.max_ratio()), 14); char *dest=new_cstr; while(joined_size) { // there is a row where first column starts 'joined_ptr' @@ -605,11 +671,11 @@ IFNDEF_NO_STRING_ORIGIN( return result; } -String& String::join_chains(char** acstr) const { +String& String::join_chains(Pool& pool, char** acstr) const { char *lcstr=cstr(); const char *current=lcstr; - String& result=*new String(); + String& result=*new(pool) String(pool); STRING_FOREACH_ROW( IFNDEF_NO_STRING_ORIGIN( const char *joined_origin_file=row->item.origin.file; @@ -726,7 +792,7 @@ void String::serialize(size_t prolog_siz prolog_size +used_rows()*(sizeof(uchar)+sizeof(ushort)) +size(); - buf=pa_malloc(buf_size); + buf=malloc(buf_size,15); char *cur=(char *)buf+prolog_size; STRING_FOREACH_ROW(