|
|
| version 1.16, 2001/02/12 13:26:54 | version 1.144.2.28.2.45, 2003/04/07 11:57:29 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| $Id$ | Parser: string class decl. |
| */ | |
| /* | |
| String Chunk0 | |
| ====== ======== | |
| head--------------->[ptr, size, ...] | |
| append_here-------->[ptr, size, ...] | |
| . | |
| . | |
| [ptr, size, ...] | |
| link_row----------->[link to the next chunk] | |
| Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| */ | */ |
| #ifndef PA_STRING_H | #ifndef PA_STRING_H |
| #define PA_STRING_H | #define PA_STRING_H |
| #ifdef HAVE_CONFIG_H | static const char* IDENT_STRING_H="$Date$"; |
| #include "pa_config.h" | |
| #endif | |
| #include <stddef.h> | // includes |
| #include "pa_pool.h" | |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_array.h" | |
| #ifndef NO_STRING_ORIGIN | extern "C" { // cord's author forgot to do that |
| # define STRING_APPEND_PARAMS const char *src, char *file, uint line | #define CORD_NO_IO |
| # define APPEND(src, file, line) real_append(src, file, line) | #include "cord.h" |
| #else | }; |
| # define STRING_APPEND_PARAMS const char *src | |
| # define APPEND(src, file, line) real_append(src) | |
| #endif | |
| class String_iterator; | // forwards |
| class String : public Pooled { | class Charset; |
| friend String_iterator; | class Table; |
| public: | class SQL_Connection; |
| enum { | class Dictionary; |
| CR_PREALLOCATED_COUNT=5, | class Request_charsets; |
| CR_GROW_PERCENT=60 | class String; |
| }; | typedef Array<const String*> ArrayString; |
| /// this is result of pos functions which mean that substr were not found | |
| #define STRING_NOT_FOUND ((size_t)-1) | |
| class StringBody { | |
| CORD body; | |
| public: | public: |
| String(Pool& apool); | StringBody(): body(CORD_EMPTY) {} |
| String(const String& src); | StringBody(CORD abody): body(abody) { |
| size_t size() const { return fsize; } | assert(!body // no body |
| int used_rows() const { return fused_rows; } | || *body // ordinary string |
| char *cstr() const; | || body[1]==1 // CONCAT_HDR |
| String& real_append(STRING_APPEND_PARAMS); | || body[1]==4 // FN_HDR |
| bool operator == (const String& src) const; | || body[1]==6 // SUBSTR_HDR |
| String& append(const String_iterator& begin, const String_iterator& end); | ); |
| } | |
| /// WARNING: length is only HELPER length, str in ANY case should be zero-terminated | |
| StringBody(const char* str, size_t helper_length): body(CORD_EMPTY) { | |
| append_help_length(str, helper_length); | |
| } | |
| static StringBody Format(int value); | |
| void clear() { body=CORD_EMPTY; } | |
| bool operator! () const { return is_empty(); } | |
| uint hash_code() const; | uint hash_code() const; |
| const Origin& origin() const { return head.rows[0].item.origin; } | const char* cstr() const { return CORD_to_const_char_star(body); } |
| char* cstrm() const { return CORD_to_char_star(body); } | |
| private: | size_t length() const { return CORD_len(body); } |
| struct Chunk { | bool is_empty() const { return body==CORD_EMPTY; } |
| // the number of rows in chunk | |
| int count; | |
| union Row { | |
| // chunk item | |
| struct { | |
| const char *ptr; // pointer to the start of string fragment | |
| size_t size; // length of the fragment | |
| Origin origin; // origin of this fragment | |
| } item; | |
| Chunk *link; // link to the next chunk in chain | |
| } rows[CR_PREALLOCATED_COUNT]; | |
| // next rows are here | |
| Chunk *preallocated_link; | |
| } | |
| head; // the head chunk of the chunk chain | |
| // next append would write to this record | |
| Chunk::Row *append_here; | |
| // the address of place where lies address | |
| // of the link to the next chunk to allocate | |
| Chunk::Row *link_row; | |
| private: | void append_help_length(const char *str, size_t help_length) { |
| // last chank allocated count | #ifndef NDEBUG |
| int curr_chunk_rows; | bool were_empty=is_empty(); |
| #endif | |
| body=CORD_cat_char_star(body, str, help_length); | |
| // string size | assert(!were_empty || body[0] /*append to empty must produce simple string*/); |
| size_t fsize; | } |
| void append_strdup(const char* str, size_t length) { | |
| append_help_length(pa_strdup(str, length), length); | |
| } | |
| void append(char c) { body=CORD_cat_char(body, c); } | |
| StringBody& operator << (const StringBody src) { body=CORD_cat(body, src.body); return *this; } | |
| StringBody& operator << (const char* str) { body=CORD_cat_char_star(body, str, 0); return *this; } | |
| // could not figure out why this operator is needed [should do this chain: string->simple->==] | |
| bool operator < (const StringBody src) const { return CORD_cmp(body, src.body)<0; } | |
| bool operator > (const StringBody src) const { return CORD_cmp(body, src.body)>0; } | |
| bool operator <= (const StringBody src) const { return CORD_cmp(body, src.body)<=0; } | |
| bool operator >= (const StringBody src) const { return CORD_cmp(body, src.body)>=0; } | |
| bool operator != (const StringBody src) const { return CORD_cmp(body, src.body)!=0; } | |
| bool operator == (const StringBody src) const { return CORD_cmp(body, src.body)==0; } | |
| // used rows in all chunks | int ncmp(size_t x_begin, const StringBody y, size_t y_begin, size_t size) const { |
| int fused_rows; | return CORD_ncmp(body, x_begin, y.body, y_begin, size); |
| } | |
| private: | char fetch(size_t index) const { return CORD_fetch(body, index); } |
| StringBody mid(size_t index, size_t length) const { return CORD_substr(body, index, length); } | |
| size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr); } | |
| size_t pos(const StringBody substr, size_t offset=0) const { | |
| if(!substr.length()) | |
| return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1] | |
| return CORD_str(body, offset, substr.body); | |
| } | |
| size_t pos(char c, | |
| size_t offset=0) const { | |
| return CORD_chr(body, offset, c); | |
| } | |
| bool chunk_is_full() { | template<typename I> void for_each(int (*callback)(const char* s, I), I info) const { |
| return append_here == link_row; | CORD_iter5(body, 0, 0, (CORD_batched_iter_fn)callback, info); |
| } | } |
| void expand(); | |
| private: //disabled | void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); } |
| String& operator = (const String&) { return *this; } | StringBody normalize() const { |
| return StringBody(CORD_balance(body)); | |
| } | |
| void dump() const { | |
| CORD_dump(body); | |
| } | |
| }; | }; |
| /** | |
| String which knows the language of all it's fragments. | |
| class Char_set { | All pieces remember |
| public: | - whether they are tainted or not, |
| Char_set(); | and the language which should be used to detaint them |
| void include(char c) { bools[static_cast<unsigned int>(c)]=true; } | */ |
| bool in(char c) { return bools[static_cast<unsigned int>(c)]; } | class String: public PA_Object { |
| private: | |
| bool bools[0x100]; | // friend class StringBody; |
| }; | |
| class String_iterator { | |
| public: | public: |
| String_iterator(String& astring); | |
| void operator ++() { skip(); } | /** piece is tainted or not. the language to use when detaint |
| void operator ++(int) { skip(); } | remember to change String_Untaint_lang_name @ untaint.C along |
| */ | |
| enum Language { | |
| L_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum | |
| // these two must go before others, there are checks for >L_AS_IS | |
| L_CLEAN, ///< clean | |
| L_AS_IS, ///< leave all characters intact | |
| L_PASS_APPENDED, | |
| /**< | |
| leave language built into string being appended. | |
| just a flag, that value not stored | |
| */ | |
| L_TAINTED, ///< tainted, untaint language as assigned later | |
| // untaint languages. assigned by ^untaint[lang]{...} | |
| L_FILE_SPEC, ///< file specification | |
| L_HTTP_HEADER, ///< text in HTTP response header | |
| L_MAIL_HEADER, ///< text in mail header | |
| L_URI, ///< text in uri | |
| L_TABLE, ///< ^table:set body | |
| L_SQL, ///< ^table:sql body | |
| L_JS, ///< JavaScript code | |
| L_XML, ///< ^dom:set xml | |
| L_HTML, ///< HTML code (for editing) | |
| L_OPTIMIZE_BIT = 0x8000 ///< flag, requiring cstr whitespace optimization | |
| }; | |
| struct Fragment { | |
| Language lang; ///< untaint flag, later untaint language | |
| size_t length; ///< length | |
| Fragment(Language alang, size_t asize): lang(alang), length(asize) { | |
| assert(alang!=L_UNSPECIFIED); | |
| assert(asize!=(size_t)-1); | |
| } | |
| }; | |
| class ArrayFragment: public Array<Fragment> { | |
| void append(element_type src) { | |
| *static_cast<Array<Fragment> *>(this)+=src; | |
| } | |
| /// hiding from accidental USE, use append_positions | |
| void append(const ArrayFragment& src, int offset, int limit) { | |
| static_cast<Array<Fragment> *>(this)->append(src, offset, limit); | |
| } | |
| public: | |
| ArrayFragment& operator += (element_type src) { | |
| if(size_t lcount=count()) { // not empty? | |
| // try to join with last | |
| Fragment& last=get_ref(lcount-1); | |
| if(last.lang==src.lang) { | |
| last.length+=src.length; | |
| return *this; | |
| } | |
| } | |
| append(src); | |
| return *this; | |
| } | |
| void append(const ArrayFragment& src) { append(src, 0, 0); } | |
| void append_positions(const ArrayFragment& src, size_t substr_begin, size_t substr_end); | |
| }; | |
| void skip_to(Char_set& set); | struct C { |
| void skip_to(char c); | char *str; |
| size_t length; | |
| operator char *() { return str; } | |
| C(char *astr, size_t asize): str(astr), length(asize) {} | |
| }; | |
| bool eof() { return feof; } | private: |
| StringBody body; ///< all characters of string | |
| ArrayFragment fragments; ///< fragment language+length info | |
| public: | |
| explicit String(const char* cstr=0, size_t help_length=0, bool tainted=false); | |
| String(const String& src); | |
| String(StringBody abody, Language alang): body(abody) { | |
| fragments+=Fragment(alang, abody.length()); | |
| } | |
| // current char | /// for convinient hash lookup |
| char operator() const; | operator const StringBody() const { return body; } |
| protected: | bool is_empty() const { return body.is_empty(); } |
| // home string | size_t length() const { return body.length(); } |
| String& string; | |
| // the row in which we are | |
| Chunk::Row *read_here; | |
| // position in that row's string fragment | |
| int offset; | |
| // when read_here reaches this row, move to the next chunk | |
| Chunk::Row *link_row; | |
| bool feof; | /// convert to CORD. if 'lang' known, forcing 'lang' to it |
| StringBody cstr_to_string_body(Language lang=L_AS_IS, | |
| SQL_Connection* connection=0, | |
| const Request_charsets *charsets=0) const; | |
| /// convert to constant C string. if 'lang' known, forcing 'lang' to it | |
| const char* cstr(Language lang=L_AS_IS, | |
| SQL_Connection* connection=0, | |
| const Request_charsets *charsets=0) const { | |
| return cstr_to_string_body(lang, connection, charsets).cstr(); | |
| } | |
| /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it | |
| char *cstrm(Language lang=L_AS_IS, | |
| SQL_Connection* connection=0, | |
| const Request_charsets *charsets=0) const { | |
| return cstr_to_string_body(lang, connection, charsets).cstrm(); | |
| } | |
| /// puts pieces to buf | |
| C serialize(size_t prolog_size) const; | |
| /// appends pieces from buf to self | |
| bool deserialize(size_t prolog_size, void *buf, size_t buf_size); | |
| /// @see StringBody::append_help_length | |
| String& append_help_length(const char* str, size_t helper_length, Language lang); | |
| String& append_strdup(const char* str, size_t length, Language lang); | |
| bool operator == (const char* y) const { return body==StringBody(y); } | |
| bool operator != (const char* y) const { return body!=StringBody(y); } | |
| /// this starts with y | |
| bool starts_with(const char* y) const { | |
| return body.ncmp(0/*x_begin*/, StringBody(y), 0/*y_begin*/, strlen(y))==0; | |
| } | |
| /// x starts with this | |
| bool this_starts(const char* x) const { | |
| return StringBody(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0; | |
| } | |
| protected: | String& append_to(String& dest, Language lang, bool forced) const; |
| String& append(const String& src, Language lang, bool forced=false) { | |
| return src.append_to(*this, lang, forced); | |
| } | |
| String& operator << (const String& src) { return append(src, L_PASS_APPENDED); } | |
| String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); } | |
| String& operator << (const StringBody src) { | |
| body<<src; | |
| fragments+=Fragment(L_AS_IS, src.length()); | |
| return *this; | |
| } | |
| // advances position on one char | /// extracts first char of a string, if any |
| void skip() { | char first_char() const { |
| return is_empty()?0:body.fetch(0); | |
| } | |
| bool operator < (const String& src) const { return body<src.body; } | |
| bool operator > (const String& src) const { return body>src.body; } | |
| bool operator <= (const String& src) const { return body<=src.body; } | |
| bool operator >= (const String& src) const { return body>=src.body; } | |
| bool operator != (const String& src) const { return body!=src.body; } | |
| bool operator == (const String& src) const { return body==src.body; } | |
| /// extracts [start, finish) piece of string | |
| String& mid(size_t substr_begin, size_t substr_end) const; | |
| /** | |
| ignore lang if it's L_UNSPECIFIED | |
| but when specified: look for substring that lies in ONE fragment in THAT lang | |
| @return position of substr in string, -1 means "not found" [const char* version] | |
| */ | |
| size_t pos(const StringBody substr, | |
| size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | |
| /// String version of @see pos(const char*, int, Language) | |
| size_t pos(const String& substr, | |
| size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | |
| size_t pos(char c, | |
| size_t this_offset=0) const { | |
| return body.pos(c, this_offset); | |
| } | } |
| void split(ArrayString& result, | |
| size_t& pos_after, | |
| const char* delim, | |
| Language lang=L_UNSPECIFIED, int limit=-1) const; | |
| void split(ArrayString& result, | |
| size_t& pos_after, | |
| const String& delim, | |
| Language lang=L_UNSPECIFIED, int limit=-1) const; | |
| typedef void (*Row_action)(Table& table, ArrayString* row, | |
| int prestart, int prefinish, | |
| int poststart, int postfinish, | |
| void *info); | |
| /** | |
| @return table of found items, if any. | |
| table format is defined and fixed[can be used by others]: | |
| @verbatim | |
| prematch/match/postmatch/1/2/3/... | |
| @endverbatim | |
| */ | |
| Table* match(Charset& source_charset, | |
| const String& regexp, | |
| const String* options, | |
| Row_action row_action, void *info, | |
| bool& just_matched) const; | |
| enum Change_case_kind { | |
| CC_UPPER, | |
| CC_LOWER | |
| }; | |
| String& change_case(Charset& source_charset, | |
| Change_case_kind kind) const; | |
| const String& replace(const Dictionary& dict) const; | |
| double as_double() const; | |
| int as_int() const; | |
| private: //disabled | |
| String& operator = (const String&) { return *this; } | |
| }; | }; |
| /// simple hash code of string. used by Hash | |
| inline uint hash_code(const StringBody self) { | |
| return self.hash_code(); | |
| } | |
| #endif | #endif |