--- parser3/src/include/pa_string.h 2002/04/01 09:37:50 1.133 +++ parser3/src/include/pa_string.h 2003/01/22 15:39:07 1.144.2.1 @@ -1,20 +1,18 @@ /** @file Parser: string class decl. - Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: pa_string.h,v 1.133 2002/04/01 09:37:50 paf Exp $ */ #ifndef PA_STRING_H #define PA_STRING_H +static const char* IDENT_STRING_H="$Date: 2003/01/22 15:39:07 $"; + #include "pa_pool.h" #include "pa_types.h" -class Table; - #ifndef NO_STRING_ORIGIN # define STRING_APPEND_PARAMS \ const char *src, size_t size, \ @@ -44,6 +42,7 @@ class Table; /// handy: appends const char* piece to String @see String::real_append #define APPEND_CONST(src) APPEND_AS_IS(src, 0, 0, 0) +class Table; class Array; class SQL_Connection; class Dictionary; @@ -69,7 +68,7 @@ class Dictionary; and the language which should be used to detaint them */ #include "pa_pragma_pack_begin.h" -class String : public Pooled { +class String: public PA_Object { public: enum { @@ -107,10 +106,12 @@ public: public: - String(Pool& apool, const char *src=0, size_t src_size=0, bool tainted=false); + //static String& OnPool(Pool& apool, const char *local_src=0, size_t src_size=0, bool tainted=false); + String(const char *src=0, size_t src_size=0, bool tainted=false); String(const String& src); - bool is_empty() const { return append_here==head.rows; } - size_t size() const; + ~String(); + bool is_empty() const { return append_here==head.chunk.rows; } + size_t size() const; /// @todonow make it field /// convert to C string. if 'lang' known, forcing 'lang' to it char *cstr(Untaint_lang lang=UL_AS_IS, SQL_Connection *connection=0, @@ -154,6 +155,11 @@ public: */ int cmp(int& partial, const char* src_ptr, size_t src_size=0, size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const; + /// this starts with src + bool starts_with(const char* src_ptr, size_t src_size=0) const { + int p; cmp(p, src_ptr, src_size); + return p==0 || p==2; + } bool operator == (const char* src_ptr) const { size_t src_size=src_ptr?strlen(src_ptr):0; if(size() != src_size) @@ -173,8 +179,10 @@ public: /// simple hash code of string. used by Hash uint hash_code() const; - /// extracts first char of a string - char first_char() const; + /// extracts first char of a string, if any + char first_char() const { + return is_empty()?0:*head.chunk.rows[0].item.ptr; + } /// extracts [start, finish) piece of string String& mid(size_t start, size_t finish) const; @@ -195,7 +203,9 @@ public: const String& delim, Untaint_lang lang=UL_UNSPECIFIED, int limit=-1) const; - typedef void (*Row_action)(Table& table, Array *row, int start, int finish, + typedef void (*Row_action)(Table& table, Array *row, + int prestart, int prefinish, + int poststart, int postfinish, void *info); /** @return true if fills table. @@ -204,7 +214,7 @@ public: prematch/match/postmatch/1/2/3/... @endverbatim */ - bool match( + bool match(Charset& source_charset, const String *aorigin, const String& regexp, const String *options, @@ -215,12 +225,14 @@ public: CC_UPPER, CC_LOWER }; - String& change_case(Pool& pool, + String& change_case(Charset& source_charset, Change_case_kind kind) const; - String& replace(Pool& pool, Dictionary& dict) const; + String& replace(Dictionary& dict) const; double as_double() const; int as_int() const; + String& join_chains(char** cstr) const; + #ifndef NO_STRING_ORIGIN /// origin of string. calculated by first row const Origin& origin() const; @@ -229,15 +241,13 @@ public: private: /** several String fragments - - 'mutable' because can write after it's end, after it was appended to somebody - @see String::append */ - mutable struct Chunk { + struct Chunk { typedef uchar count_type; count_type count; ///< the number of rows in chunk + // here could be some padding bytes /// string fragment or a link to next chunk union - union Row { + typedef union Row { typedef uchar item_size_type; /// fragment struct { @@ -248,10 +258,19 @@ private: Origin origin; ///< origin #endif } item; + // we are using the fact that there's no padding before this field! Chunk *link; ///< link to the next chunk in chain - } rows[CR_PREALLOCATED_COUNT+1/*for head.rows[CR_PREALLOCATED_COUNT].link*/]; - } - head; ///< the head chunk of the chunk chain + } rows_type[CR_PREALLOCATED_COUNT]; + rows_type rows; + }; + /** + 'mutable' because can write after it's end, after it was appended to somebody + @see String::append + */ + mutable struct { + Chunk chunk; + Chunk *link_storage; + } head; ///< the head chunk of the chunk chain /// next append would write to this record Chunk::Row *append_here; @@ -279,11 +298,9 @@ private: Charset *store_to_charset=0, const char *store_to_charset_name=0) const; - String& reconstruct(Pool& pool) const; - void join_chain(Pool& pool, + void join_chain( const Chunk*& achunk, const Chunk::Row*& arow, uint& acountdown, uchar& joined_lang, const char *& joined_ptr, size_t& joined_size) const; - String& replace_in_reconstructed(Pool& pool, Dictionary& dict) const; private: //disabled @@ -304,7 +321,7 @@ private: //disabled } #define STRING_PREFIX_FOREACH_ROW(self, body) { \ - const Chunk *chunk=&(self).head; \ + const Chunk *chunk=&(self).head.chunk; \ const Chunk::Row *row=chunk->rows; \ uint countdown=chunk->count; \ STRING_PREPARED_FOREACH_ROW(self, body) \