--- parser3/src/main/pa_string.C 2001/06/28 07:44:17 1.93 +++ parser3/src/main/pa_string.C 2001/08/09 16:53:27 1.99 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan (http://design.ru/paf) */ -static const char *RCSId="$Id: pa_string.C,v 1.93 2001/06/28 07:44:17 parser Exp $"; +static const char *RCSId="$Id: pa_string.C,v 1.99 2001/08/09 16:53:27 parser Exp $"; #include "pa_config_includes.h" @@ -22,7 +22,8 @@ static const char *RCSId="$Id: pa_string #include "pa_table.h" String::String(Pool& apool, const char *src, size_t src_size, bool tainted) : - Pooled(apool) { + Pooled(apool), + forigins_mode(false) { last_chunk=&head; head.count=CR_PREALLOCATED_COUNT; append_here=head.rows; @@ -37,23 +38,15 @@ String::String(Pool& apool, const char * APPEND_CLEAN(src, src_size, 0, 0); } -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 *))); - 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()) { +String::String(const String& src) : + Pooled(src.pool()), + forigins_mode(false) { head.count=CR_PREALLOCATED_COUNT; size_t src_used_rows=src.fused_rows; if(src_used_rows<=head.count) { // all new rows fit size_to preallocated area + last_chunk=&head; size_t curr_chunk_rows=head.count; memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows); append_here=&head.rows[src_used_rows]; @@ -72,15 +65,15 @@ String::String(const String& src) : Pool memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count); // remaining rows size_to new_chunk size_t curr_chunk_rows=src_used_rows-head.count; - Chunk *new_chunk=static_cast( + last_chunk=static_cast( malloc(sizeof(size_t)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); - new_chunk->count=curr_chunk_rows; - head.preallocated_link=new_chunk; - append_here=link_row=&new_chunk->rows[new_chunk->count]; + last_chunk->count=curr_chunk_rows; + head.preallocated_link=last_chunk; + append_here=link_row=&last_chunk->rows[last_chunk->count]; Chunk *old_chunk=src.head.preallocated_link; - Chunk::Row *new_rows=new_chunk->rows; - size_t rows_left_to_copy=new_chunk->count; + Chunk::Row *new_rows=last_chunk->rows; + size_t rows_left_to_copy=last_chunk->count; while(true) { size_t old_count=old_chunk->count; Chunk *next_chunk=old_chunk->rows[old_count].link; @@ -105,6 +98,17 @@ String::String(const String& src) : Pool fsize=src.fsize; } +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 *))); + 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) { const Chunk *chunk=&src.head; do { @@ -146,6 +150,15 @@ String& String::real_append(STRING_APPEN 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 result=0; @@ -326,10 +339,10 @@ int String::cmp(int& partial, const char #ifndef NO_STRING_ORIGIN const Origin& String::origin() const { - if(!fused_rows) - THROW(0, 0, - 0, - "String::origin() of empty string called"); + if(!fused_rows) { + static const Origin empty_origin={"empty string"}; + return empty_origin; + } // determining origin by last appended piece // because first one frequently constant. @@ -490,7 +503,8 @@ bool String::match(const unsigned char * const String& regexp, const String *options, Table **table, - Row_action row_action, void *info) const { + Row_action row_action, void *info, + bool *was_global) const { if(!regexp.size()) THROW(0, 0, @@ -500,6 +514,8 @@ bool String::match(const unsigned char * const char *errptr; int erroffset; 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], &errptr, &erroffset, pcre_tables); @@ -641,10 +657,13 @@ double String::as_double() const { const char *cstr=this->cstr(); char *error_pos=0; // 0xABC - if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X')) - result=(double)(unsigned long)strtol(cstr, &error_pos, 0); + if(cstr[0]=='0') + if(cstr[1]=='x' || cstr[1]=='X') + result=(double)(unsigned long)strtol(cstr, &error_pos, 0); + else + result=(double)strtod(/*skip leading 0*/++cstr, &error_pos); else - result=strtod(cstr, &error_pos); + result=(double)strtod(cstr, &error_pos); if(error_pos && *error_pos) THROW(0, 0, @@ -658,8 +677,11 @@ int String::as_int() const { const char *cstr=this->cstr(); char *error_pos=0; // 0xABC - if(cstr[0]=='0' && (cstr[1]=='x' || cstr[1]=='X')) - result=(int)(unsigned long)strtol(cstr, &error_pos, 0); + if(cstr[0]=='0') + if(cstr[1]=='x' || cstr[1]=='X') + result=(int)(unsigned long)strtol(cstr, &error_pos, 0); + else + result=(int)strtol(/*skip leading 0*/++cstr, &error_pos, 0); else result=(int)strtol(cstr, &error_pos, 0);