--- parser3/src/main/pa_string.C 2002/04/22 14:11:28 1.157 +++ parser3/src/main/pa_string.C 2002/08/07 13:44:00 1.166 @@ -3,10 +3,10 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: pa_string.C,v 1.157 2002/04/22 14:11:28 paf Exp $ */ +static const char* IDENT_STRING_C="$Date: 2002/08/07 13:44:00 $"; + #include "pcre.h" #include "pa_pool.h" @@ -27,6 +27,17 @@ ulong string_piece_appends=0; #endif +String& String::OnPool(Pool& apool, const char *local_src, size_t src_size, bool tainted) { + if(local_src && *local_src) { + if(src_size==0) + src_size=strlen(local_src); + + char *pooled_src=(char *)apool.malloc(src_size); + memcpy(pooled_src, local_src, src_size); + return *new(apool) String(apool, pooled_src, src_size, tainted); + } else + return *new(apool) String(apool); +} String::String(Pool& apool, const char *src, size_t src_size, bool tainted) : Pooled(apool) { last_chunk=&head.chunk; @@ -332,8 +343,8 @@ const Origin& String::origin() const { String& String::mid(size_t start, size_t finish) const { String& result=*NEW String(pool()); - start=max(size_t(0), start); - finish=min(size(), finish); + start=min(start, size()); + finish=max(finish, start); if(start==finish) return result; @@ -474,7 +485,7 @@ static void regex_options(const String * } /// @todo make replacement Table stacked -bool String::match(const char *acstr, +bool String::match( const String *aorigin, const String& regexp, const String *options, @@ -512,7 +523,7 @@ bool String::match(const char *acstr, info_substrings); } - const char *subject=acstr?acstr:cstr(); + const char *subject=cstr(); int length=strlen(subject); const int ovecsize=(1/*match*/+MAX_STRING_MATCH_TABLE_COLUMNS)*3; int ovector[ovecsize]; @@ -648,31 +659,6 @@ String& String::replace(Pool& pool, Dict return result; } -/// @test real! -bool String::is_join_chains_profitable() const { - // mimimum actually sizeof(String), - // but one must also consider CPU time optimize would eat - const int minimum_economy=sizeof(String)*2; - - size_t wasted=0; - STRING_FOREACH_ROW( - uchar joined_lang=row->item.lang; - STRING_PREPARED_FOREACH_ROW(*this, - if(row->item.lang==joined_lang) { - wasted+=sizeof(String::Chunk::rows_type); - if(wasted>minimum_economy) - return true; - } else - break; // before non-ours - ); - - // pointers are after joined piece - // & one step back, see STRING_FOREACH_ROW - --row; ++countdown; - ); - return false; -} - String& String::join_chains(Pool& pool, char** acstr) const { char *lcstr=cstr(); const char *current=lcstr; @@ -719,6 +705,11 @@ double String::as_double() const { cstr=buf; } else cstr=this->cstr(); + while(*cstr && isspace(*cstr)) + cstr++; + if(!*cstr) + return 0; + char *error_pos; // 0xABC if(cstr[0]=='0') @@ -729,10 +720,11 @@ double String::as_double() const { else result=(double)strtod(cstr, &error_pos); - if(*error_pos/*not EOS*/) - throw Exception("number.format", - this, - "invalid number (double)"); + while(char c=*error_pos++) + if(!isspace(c)) + throw Exception("number.format", + this, + "invalid number (double)"); return result; } @@ -741,12 +733,17 @@ int String::as_int() const { const char *cstr; char buf[MAX_NUMBER]; if(head.chunk.rows+1==append_here) { - int size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1); + size_t size=min(head.chunk.rows[0].item.size, MAX_NUMBER-1); memcpy(buf, head.chunk.rows[0].item.ptr, size); buf[size]=0; cstr=buf; } else cstr=this->cstr(); + while(*cstr && isspace(*cstr)) + cstr++; + if(!*cstr) + return 0; + char *error_pos; // 0xABC if(cstr[0]=='0') @@ -757,10 +754,11 @@ int String::as_int() const { else result=(int)strtol(cstr, &error_pos, 0); - if(*error_pos/*not EOS*/) - throw Exception("number.format", - this, - "invalid number (int)"); + while(char c=*error_pos++) + if(!isspace(c)) + throw Exception("number.format", + this, + "invalid number (int)"); return result; }