--- parser3/src/main/pa_string.C 2001/05/15 14:31:58 1.83 +++ parser3/src/main/pa_string.C 2001/09/18 16:05:42 1.103 @@ -4,9 +4,8 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: pa_string.C,v 1.83 2001/05/15 14:31:58 parser Exp $ */ +static const char *RCSId="$Id: pa_string.C,v 1.103 2001/09/18 16:05:42 parser Exp $"; #include "pa_config_includes.h" @@ -21,22 +20,11 @@ #include "pa_array.h" #include "pa_globals.h" #include "pa_table.h" -#include "pa_threads.h" - -#include "pa_sapi.h" -#define STRING_STAT_MAX_PIECES 1000 -int string_stat_pieces[STRING_STAT_MAX_PIECES]; -void log_string_stats(Pool& pool) { - for(int i=0; icount+last_chunk->count*CR_GROW_PERCENT/100; - 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_stat_pieces[0]++; +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]; @@ -94,15 +66,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; @@ -127,6 +99,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 { @@ -168,6 +151,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; @@ -207,7 +199,7 @@ int String::cmp(int& partial, const Stri size_t pos=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(pos+a_row->item.size > this_offset) { if(lang!=UL_UNSPECIFIED && a_row->item.lang!=lang) @@ -328,7 +320,8 @@ int String::cmp(int& partial, const char a_row++; a_countdown--; } - if(a_break=a_row==a_end) + a_break=a_row==a_end; + if(a_break || b_break) break; if(!a_countdown) { a_chunk=a_row->link; @@ -347,10 +340,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. @@ -505,12 +498,14 @@ static void regex_options(char *options, } } +/// @todo maybe need speedup: some option to remove pre/match/post string generation bool String::match(const unsigned char *pcre_tables, const String *aorigin, 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, @@ -520,6 +515,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); @@ -531,7 +528,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)", @@ -565,13 +562,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)", @@ -588,13 +585,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) @@ -655,3 +653,89 @@ break2: return result; } + +String& String::replace(Pool& pool, Dictionary& dict) const { + String& result=*new(pool) String(pool); + 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; + + 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)) { + // get a=>b values + const String& a=*static_cast(item)->get_string(0); + const String& b=*static_cast(item)->get_string(1); + // skip 'a' in 'src' + src+=a.size(); + // write 'b' to 'dest' + b.store_to(dest); + // skip 'b' in 'dest' + dest+=b.size(); + // reduce work size + src_size-=a.size(); + } else { + *dest++=*src++; + // reduce work size + 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 result; + const char *cstr=this->cstr(); + char *error_pos; + // 0xABC + if(cstr[0]=='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 + result=(double)strtod(cstr, &error_pos); + + if(*error_pos/*not EOS*/) + THROW(0, 0, + this, + "invalid number (double)"); + + return result; +} +int String::as_int() const { + int result; + const char *cstr=this->cstr(); + char *error_pos; + // 0xABC + if(cstr[0]=='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 + result=(int)strtol(cstr, &error_pos, 0); + + if(*error_pos/*not EOS*/) + THROW(0, 0, + this, + "invalid number (int)"); + + return result; +}