--- parser3/src/main/pa_string.C 2001/10/19 12:43:30 1.112 +++ parser3/src/main/pa_string.C 2001/10/29 15:15:11 1.115 @@ -4,7 +4,7 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_string.C,v 1.112 2001/10/19 12:43:30 parser Exp $ + $Id: pa_string.C,v 1.115 2001/10/29 15:15:11 paf Exp $ */ #include "pa_config_includes.h" @@ -30,7 +30,7 @@ String::String(Pool& apool, const char * append_here=head.rows; head.preallocated_link=0; link_row=&head.rows[head.count]; - fused_rows=fsize=0; + fsize=0; if(src) if(tainted) @@ -44,7 +44,7 @@ String::String(const String& src) : forigins_mode(false) { head.count=CR_PREALLOCATED_COUNT; - size_t src_used_rows=src.fused_rows; + size_t src_used_rows=src.used_rows(); if(src_used_rows<=head.count) { // all new rows fit size_to preallocated area last_chunk=&head; @@ -67,7 +67,7 @@ String::String(const String& src) : // remaining rows size_to new_chunk size_t curr_chunk_rows=src_used_rows-head.count; last_chunk=static_cast( - malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); + malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *), 9)); last_chunk->count=curr_chunk_rows; head.preallocated_link=last_chunk; append_here=link_row=&last_chunk->rows[last_chunk->count]; @@ -95,14 +95,32 @@ String::String(const String& src) : } } link_row->link=0; - fused_rows=src_used_rows; + src_used_rows; fsize=src.fsize; } +/// @todo not very optimal +uint String::used_rows() const { + uint result=0; + const Chunk *chunk=&head; + do { + const Chunk::Row *row=chunk->rows; + for(size_t i=0; icount; i++, row++) { + if(row==append_here) + goto break2; + + result++; + } + chunk=row->link; + } while(chunk); + +break2: + return result; +} void String::expand() { size_t new_chunk_count=last_chunk->count+CR_GROW_COUNT; last_chunk=static_cast( - malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *))); + malloc(sizeof(size_t)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *), 10)); last_chunk->count=new_chunk_count; link_row->link=last_chunk; append_here=last_chunk->rows; @@ -146,13 +164,13 @@ String& String::real_append(STRING_APPEN append_here->item.origin.file=file; append_here->item.origin.line=line; #endif - append_here++; fused_rows++; + append_here++; return *this; } char String::first_char() const { - if(!fused_rows) + if(!fsize) throw Exception(0, 0, this, "getting first char of empty string"); @@ -340,7 +358,7 @@ int String::cmp(int& partial, const char #ifndef NO_STRING_ORIGIN const Origin& String::origin() const { - if(!fused_rows) { + if(!fsize) { static const Origin empty_origin={"empty string"}; return empty_origin; } @@ -540,7 +558,7 @@ bool String::match(const unsigned char * int length=strlen(subject); int ovecsize; int *ovector=(int *)malloc(sizeof(int)* - (ovecsize=(1/*match*/+info_substrings)*3)); + (ovecsize=(1/*match*/+info_substrings)*3), 11); { // create table Array& columns=*NEW Array(pool()); @@ -632,7 +650,7 @@ String& String::change_case(Pool& pool, if(row==append_here) goto break2; - char *new_cstr=(char *)pool.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++) { @@ -693,7 +711,7 @@ break21:; achunk=arow->link; } else { // join adjacent rows - char *ptr=(char *)pool.malloc(joined_size); + char *ptr=(char *)pool.malloc(joined_size,13); joined_ptr=ptr; size_t start_i=ai; const Chunk::Row *start_row=arow; @@ -767,7 +785,7 @@ String& String::replace_in_reconstructed 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 *new_cstr=(char *)pool.malloc((size_t)ceil(src_size*dict.max_ratio()), 14); char *dest=new_cstr; while(src_size) { // there is a row where first column starts 'src' @@ -802,7 +820,15 @@ String& String::replace(Pool& pool, Dict double String::as_double() const { double result; - const char *cstr=this->cstr(); + const char *cstr; + char buf[MAX_NUMBER]; + if(head.rows+1==append_here) { + int size=min(head.rows[0].item.size, MAX_NUMBER-1); + memcpy(buf, head.rows[0].item.ptr, size); + buf[size]=0; + cstr=buf; + } else + cstr=this->cstr(); char *error_pos; // 0xABC if(cstr[0]=='0') @@ -822,7 +848,15 @@ double String::as_double() const { } int String::as_int() const { int result; - const char *cstr=this->cstr(); + const char *cstr; + char buf[MAX_NUMBER]; + if(head.rows+1==append_here) { + int size=min(head.rows[0].item.size, MAX_NUMBER-1); + memcpy(buf, head.rows[0].item.ptr, size); + buf[size]=0; + cstr=buf; + } else + cstr=this->cstr(); char *error_pos; // 0xABC if(cstr[0]=='0') @@ -840,3 +874,69 @@ int String::as_int() const { return result; } + +/* @todo maybe network order worth spending some effort? + don't bothering myself with network byte order, + am not planning to be able to move resulting file across platforms + for now +*/ +void String::serialize(size_t prolog_size, void *& buf, size_t& buf_size) const { + buf_size= + prolog_size + +used_rows()*(sizeof(Untaint_lang)+sizeof(size_t)) + +size(); + buf=malloc(buf_size,15); + char *cur=(char *)buf+prolog_size; + + const Chunk *chunk=&head; + do { + const Chunk::Row *row=chunk->rows; + for(size_t i=0; icount; i++) { + if(row==append_here) + goto break2; + + // lang + memcpy(cur, &row->item.lang, sizeof(Untaint_lang)); + cur+=sizeof(Untaint_lang); + // size + memcpy(cur, &row->item.size, sizeof(size_t)); + cur+=sizeof(size_t); + // bytes + memcpy(cur, row->item.ptr, row->item.size); + cur+=row->item.size; + + row++; + } + chunk=row->link; + } while(chunk); +break2: + ; +} + +/* @todo maybe network order worth spending some effort? + don't bothering myself with network byte order, + am not planning to be able to move resulting file across platforms + for now +*/ +#ifndef DOXYGEN +struct Serialized_piece { + String::Untaint_lang lang; + size_t size; + char ptr[1]; +}; +#endif + +void String::deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file) { + char *cur=((char *)buf)+prolog_size; + buf_size-=prolog_size; + + while(buf_size) { + Serialized_piece& p=*(Serialized_piece *)cur; + APPEND(p.ptr, p.size, p.lang, file, 0); + + size_t piece_size=sizeof(p.lang)+sizeof(p.size)+p.size; + cur+=piece_size; + buf_size-=piece_size; + } +} +