|
|
| version 1.35, 2001/03/14 08:50:01 | version 1.144.2.28.2.19, 2003/03/21 15:00:36 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| Parser | Parser: string class decl. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| */ | */ |
| /* | #ifndef PA_STRING_H |
| #define PA_STRING_H | |
| String Chunk0 | static const char* IDENT_STRING_H="$Date$"; |
| ====== ======== | |
| head--------------->[ptr, size, ...] | |
| append_here-------->[ptr, size, ...] | |
| . | |
| . | |
| [ptr, size, ...] | |
| link_row----------->[link to the next chunk] | |
| // includes | |
| #include "pa_types.h" | |
| #include "pa_array.h" | |
| extern "C" { // cord's author forgot to do that | |
| #include "cord.h" | |
| }; | |
| // forwards | |
| class Charset; | |
| class Table; | |
| class SQL_Connection; | |
| class Dictionary; | |
| class Request_charsets; | |
| class String; | |
| typedef Array<const String*> ArrayString; | |
| // helpers | |
| /* | |
| /// appends clean piece to String @see String::append | |
| #define APPEND_CLEAN(src, length) append(src, length, String::L_CLEAN) | |
| /// appends piece to String as-is @see String::append | |
| #define APPEND_AS_IS(src, length) append(src, length, String::L_AS_IS) | |
| /// appends tainted piece to String @see String::append | |
| #define APPEND_TAINTED(src, length) append(src, length, String::L_TAINTED) | |
| /// handy: appends const char* piece to String @see String::append | |
| #define APPEND_CONST(src) APPEND_AS_IS(src, 0) | |
| */ | */ |
| /// this is result of pos functions which mean that substr were not found | |
| #define STRING_NOT_FOUND ((size_t)-1) | |
| #ifndef PA_STRING_H | class StringBody { |
| #define PA_STRING_H | |
| #ifdef HAVE_CONFIG_H | CORD body; |
| # include "pa_config.h" | |
| #endif | |
| #include <stddef.h> | public: |
| #include "pa_pool.h" | StringBody(): body(CORD_EMPTY) {} |
| #include "pa_types.h" | StringBody(CORD abody): body(abody) {} |
| #define UNTAINT_TIMES_BIGGER 10 | void clear() { body=CORD_EMPTY; } |
| #ifndef NO_STRING_ORIGIN | uint hash_code() const; |
| # define STRING_APPEND_PARAMS \ | |
| const char *src, size_t size, \ | size_t length() const { return CORD_len(body); } |
| bool tainted, \ | |
| const char *file, uint line | bool is_empty() const { return body==CORD_EMPTY; } |
| # define APPEND(src, size, file, line) real_append(src, size, false, file, line) | |
| # define APPEND_TAINTED(src, size, file, line) real_append(src, size, true, file, line) | void append(const char *str, size_t length) { |
| #else | CORD_cat_char_star(body, str, length); |
| # define STRING_APPEND_PARAMS \ | } |
| const char *src, \ | void append(char c) { CORD_cat_char(body, c); } |
| size_t size, \ | void append(StringBody src) { CORD_cat(body, src.body); } |
| bool tainted | |
| # define APPEND(src, size, file, line) real_append(src, size, false) | // could not figure out why this operator is needed [should do this chain: string->simple->==] |
| # define APPEND_TAINTED(src, size, file, line) real_append(src, size, true) | bool operator < (const StringBody& src) const { return CORD_cmp(body, src.body)<0; } |
| #endif | bool operator > (const StringBody& src) const { return CORD_cmp(body, src.body)>0; } |
| #define APPEND_CONST(src) APPEND(src, 0, 0, 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; } | |
| int ncmp(size_t x_begin, const StringBody y, size_t y_begin, size_t size) const { | |
| return CORD_ncmp(body, x_begin, y.body, y_begin, size); | |
| } | |
| char fetch(size_t index) const { return CORD_fetch(body, index); } | |
| }; | |
| /** | |
| String which knows the language of all it's fragments. | |
| All pieces remember | |
| - whether they are tainted or not, | |
| and the language which should be used to detaint them | |
| */ | |
| class String: public PA_Object { | |
| friend class StringBody; | |
| class String : public Pooled { | |
| public: | public: |
| enum { | |
| CR_PREALLOCATED_COUNT=5, | |
| CR_GROW_PERCENT=60 | |
| }; | |
| enum Untaint_lang { | /** piece is tainted or not. the language to use when detaint |
| UNKNOWN=0, // when get by name fails | remember to change String_Untaint_lang_name @ untaint.C along |
| NO, // clean | */ |
| YES, // tainted, untaint language as assigned later | 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]{...} | // untaint languages. assigned by ^untaint[lang]{...} |
| PASS_APPENDED, | L_FILE_SPEC, ///< file specification |
| // leave language built into string being appended | L_HTTP_HEADER, ///< text in HTTP response header |
| // just a flag, that value not stored | L_MAIL_HEADER, ///< text in mail header |
| AS_IS, | L_URI, ///< text in uri |
| TABLE, | L_TABLE, ///< ^table:set body |
| SQL, | L_SQL, ///< ^table:sql body |
| JS, | L_JS, ///< JavaScript code |
| HTML, | L_XML, ///< ^dom:set xml |
| HTML_TYPO | L_HTML, ///< HTML code (for editing) |
| L_OPTIMIZE_BIT = 0x8000 ///< flag, requiring cstr whitespace optimization | |
| }; | }; |
| public: | struct Fragment { |
| Language lang; ///< untaint flag, later untaint language | |
| size_t length; ///< length | |
| Fragment(Language alang, size_t asize): lang(alang), length(asize) {} | |
| }; | |
| String(Pool& apool); | class ArrayFragment: public Array<Fragment> { |
| String(const String& src); | void append(element_type src) { |
| size_t size() const { return fsize; } | *static_cast<Array<Fragment> *>(this)+=src; |
| int used_rows() const { return fused_rows; } | } |
| char *cstr() const; | /// hiding from accidental USE, use append_positions |
| String& real_append(STRING_APPEND_PARAMS); | void append(const ArrayFragment& src, int offset, int limit) { |
| int cmp (const String& src) const; | static_cast<Array<Fragment> *>(this)->append(src, offset, limit); |
| bool operator < (const String& src) const { return cmp(src)<0; } | } |
| bool operator > (const String& src) const { return cmp(src)>0; } | public: |
| bool operator <= (const String& src) const { return cmp(src)<=0; } | ArrayFragment& operator += (element_type src) { |
| bool operator >= (const String& src) const { return cmp(src)>=0; } | if(count()) { // not empty? |
| bool operator == (const String& src) const { | // try to join with last |
| if(size()!=src.size()) // can speed up in trivial case | Fragment& last=get(count()-1); |
| return false; | if(last.lang==src.lang) { |
| return cmp(src)==0; | last.length+=src.length; |
| } | return *this; |
| bool operator != (const String& src) const { return cmp(src)!=0; } | } |
| } | |
| 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); | |
| }; | |
| bool operator == (const char* b_ptr) const; | struct C { |
| String& append(const String& src, Untaint_lang lang); | char *str; |
| size_t length; | |
| operator char *() { return str; } | |
| C(char *astr, size_t asize): str(astr), length(asize) {} | |
| }; | |
| uint hash_code() const; | private: |
| const Origin& origin() const { return head.rows[0].item.origin; } | StringBody body; ///< all characters of string |
| ArrayFragment fragments; ///< fragment language+length info | |
| private: | public: |
| struct Chunk { | /// @todo check all places for length to be only HELPER length, and never be !=strlen(cstr) |
| // the number of rows in chunk | explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); |
| int count; | String(const String& src); |
| union Row { | String(StringBody abody, Language alang): body(abody) { |
| // fragment | fragments+=Fragment(alang, abody.length()); |
| struct { | } |
| const char *ptr; // pointer to the start | |
| size_t size; // length | |
| Untaint_lang lang; // untaint flag, later untaint language | |
| #ifndef NO_STRING_ORIGIN | |
| Origin origin; // origin | |
| #endif | |
| } 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: | /// for convinient hash lookup |
| // last chunk | operator StringBody() const { return body; } |
| Chunk *last_chunk; | |
| // string size | bool is_empty() const { return body.is_empty(); } |
| size_t fsize; | size_t length() const { return body.length(); } |
| // used rows in all chunks | /// convert to CORD. if 'lang' known, forcing 'lang' to it |
| int fused_rows; | CORD cstr_to_cord(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 CORD_to_const_char_star(cstr_to_cord(lang, connection, charsets)); | |
| } | |
| /// 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 CORD_to_char_star(cstr_to_cord(lang, connection, charsets)); | |
| } | |
| /// 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, const char* file); | |
| /** append fragment, | |
| @c length can be zero, it will be autocalced | |
| when length<strlen(str) makes heap copy of @c str piece | |
| */ | |
| String& append(const char* str, size_t length, Language lang); | |
| 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; | |
| } | |
| /// y starts with this | |
| bool this_starts(const char* y) const { | |
| return body.ncmp(0/*x_begin*/, StringBody(y), 0/*y_begin*/, strlen(y))==0; | |
| } | |
| private: | 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(src, 0, L_AS_IS); } | |
| String& operator << (const StringBody& src) { | |
| body.append(src); | |
| fragments+=Fragment(L_AS_IS, src.length()); | |
| return *this; | |
| } | |
| bool chunk_is_full() { | /// extracts first char of a string, if any |
| return append_here == link_row; | char first_char() const { |
| return is_empty()?0:body.fetch(0); | |
| } | } |
| void expand(); | |
| void set_lang(Chunk::Row *row, Untaint_lang lang, size_t size); | 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 | |
| const 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(CORD 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; | |
| void split(ArrayString& result, | |
| size_t& pos_after, | |
| const char* delim, | |
| Language lang=L_UNSPECIFIED, int limit=-1); | |
| 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 String& replace(const Dictionary& dict) const; | |
| double as_double() const; | |
| int as_int() const; | |
| private: //disabled | private: //disabled |
| Line 153 private: //disabled | Line 298 private: //disabled |
| }; | }; |
| /// simple hash code of string. used by Hash | |
| inline uint hash_code(const StringBody& self) { | |
| return self.hash_code(); | |
| } | |
| #endif | #endif |