--- parser3/src/main/pa_string.C 2001/06/28 07:41:59 1.92 +++ parser3/src/main/pa_string.C 2001/08/10 08:10:13 1.100 @@ -4,10 +4,8 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: pa_string.C,v 1.92 2001/06/28 07:41:59 parser Exp $ */ -static char *RCSId="$Id: pa_string.C,v 1.92 2001/06/28 07:41:59 parser Exp $"; +static const char *RCSId="$Id: pa_string.C,v 1.100 2001/08/10 08:10:13 parser Exp $"; #include "pa_config_includes.h" @@ -24,7 +22,8 @@ static char *RCSId="$Id: pa_string.C,v 1 #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; @@ -39,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]; @@ -74,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; @@ -107,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 { @@ -148,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; @@ -328,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. @@ -492,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, @@ -502,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); @@ -513,7 +527,7 @@ bool String::match(const unsigned char * int info_substrings=pcre_info(code, 0, 0); if(info_substrings<0) { - (*pcre_free)(code); + pcre_free(code); THROW(0, 0, aorigin, "pcre_info error (%d)", @@ -547,13 +561,13 @@ bool String::match(const unsigned char * exec_option_bits, ovector, ovecsize); if(exec_substrings==PCRE_ERROR_NOMATCH) { - (*pcre_free)(code); - (*row_action)(**table, 0/*last time, no row*/, 0, 0, info); + pcre_free(code); + row_action(**table, 0/*last time, no row*/, 0, 0, info); return option_bits[1]!=0; // global=true+table, not global=false } if(exec_substrings<0) { - (*pcre_free)(code); + pcre_free(code); THROW(0, 0, aorigin, "regular expression execute error (%d)", @@ -570,13 +584,14 @@ bool String::match(const unsigned char * 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 - (*pcre_free)(code); - (*row_action)(**table, 0/*last time, no row*/, 0, 0, info); + if(!option_bits[1] || startoffset==ovector[1]) { // not global | going to hang + pcre_free(code); + row_action(**table, 0/*last time, no row*/, 0, 0, info); return true; } + startoffset=ovector[1]; /* if(option_bits[0] & PCRE_MULTILINE) @@ -643,10 +658,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, @@ -660,8 +678,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);