|
|
| version 1.47, 2001/03/20 06:45:18 | version 1.145, 2003/07/24 11:31:21 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class decl. | Parser: string class decl. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | |
| */ | */ |
| #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" | |
| #define UNTAINT_TIMES_BIGGER 10 | extern "C" { // cord's author forgot to do that |
| #define CORD_NO_IO | |
| #include "cord.h" | |
| }; | |
| #ifndef NO_STRING_ORIGIN | // forwards |
| # define STRING_APPEND_PARAMS \ | |
| const char *src, size_t size, \ | |
| bool tainted, \ | |
| const char *file, uint line | |
| /// appends clean piece to String @see String::real_append | |
| # define APPEND(src, size, file, line) real_append(src, size, false, file, line) | |
| /// appends tainted piece to String @see String::real_append | |
| # define APPEND_TAINTED(src, size, file, line) real_append(src, size, true, file, line) | |
| #else | |
| # define STRING_APPEND_PARAMS \ | |
| const char *src, \ | |
| size_t size, \ | |
| bool tainted | |
| /// appends clean piece to String @see String::real_append | |
| # define APPEND(src, size, file, line) real_append(src, size, false) | |
| /// appends tainted piece to String @see String::real_append | |
| # define APPEND_TAINTED(src, size, file, line) real_append(src, size, true) | |
| #endif | |
| /// handy: appends const char* piece to String @see String::real_append | |
| #define APPEND_CONST(src) APPEND(src, 0, 0, 0) | |
| /** | class Charset; |
| Pooled string. | class Table; |
| class SQL_Connection; | |
| class Dictionary; | |
| class Request_charsets; | |
| 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: | |
| StringBody(): body(CORD_EMPTY) {} | |
| StringBody(CORD abody): body(abody) { | |
| assert(!body // no body | |
| || *body // ordinary string | |
| || body[1]==1 // CONCAT_HDR | |
| || body[1]==4 // FN_HDR | |
| || body[1]==6 // SUBSTR_HDR | |
| ); | |
| } | |
| /// 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_know_length(str, helper_length?helper_length:strlen(str)); | |
| } | |
| static StringBody Format(int value); | |
| void clear() { body=CORD_EMPTY; } | |
| bool operator! () const { return is_empty(); } | |
| uint hash_code() const; | |
| const char* cstr() const { return CORD_to_const_char_star(body); } | |
| char* cstrm() const { return CORD_to_char_star(body); } | |
| size_t length() const { return CORD_len(body); } | |
| Internal structure: | bool is_empty() const { return body==CORD_EMPTY; } |
| @verbatim | |
| String Chunk0 | void append_know_length(const char *str, size_t known_length) { |
| ====== ======== | if(known_length) |
| head--------------->[ptr, size, ...] | body=CORD_cat_char_star(body, str, known_length); |
| append_here-------->[ptr, size, ...] | } |
| . | void append_strdup_know_length(const char* str, size_t known_length) { |
| . | if(known_length) |
| [ptr, size, ...] | append_know_length(pa_strdup(str, known_length), known_length); |
| link_row----------->[link to the next chunk] | } |
| @endverbatim | 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) { append_know_length(str, strlen(str)); 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; } | |
| 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); } | |
| 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); | |
| } | |
| template<typename I> void for_each(int (*callback)(const char* s, I), I info) const { | |
| CORD_iter5(body, 0, 0, (CORD_batched_iter_fn)callback, info); | |
| } | |
| void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); } | |
| StringBody normalize() const { | |
| return StringBody(CORD_balance(body)); | |
| } | |
| void dump() const { | |
| CORD_dump(body); | |
| } | |
| }; | |
| /** | |
| String which knows the language of all it's fragments. | |
| All pieces remember | All pieces remember |
| - the file and its line they are from [can be turned off by NO_STRING_ORIGIN] | |
| - whether they are tainted or not, | - whether they are tainted or not, |
| and the language which should be used to detaint them | and the language which should be used to detaint them |
| */ | */ |
| class String : public Pooled { | class String: public PA_Object { |
| // friend class StringBody; | |
| public: | public: |
| enum { | |
| CR_PREALLOCATED_COUNT=5, ///< default preallocated item count | |
| CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded() | |
| }; | |
| /// piece is tainted or not. the language to use when detaint | /** piece is tainted or not. the language to use when detaint |
| enum Untaint_lang { | remember to change String_Untaint_lang_name @ untaint.C along |
| UNKNOWN=0, ///< when get by name fails | */ |
| NO, ///< clean | enum Language { |
| YES, ///< tainted, untaint language as assigned later | L_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum |
| // untaint languages. assigned by ^untaint[lang]{...} | // these two must go before others, there are checks for >L_AS_IS |
| PASS_APPENDED, | L_CLEAN, ///< clean |
| L_AS_IS, ///< leave all characters intact | |
| L_PASS_APPENDED, | |
| /**< | /**< |
| leave language built into string being appended. | leave language built into string being appended. |
| just a flag, that value not stored | just a flag, that value not stored |
| */ | */ |
| AS_IS, ///< leave all characters intact | L_TAINTED, ///< tainted, untaint language as assigned later |
| FILE_NAME, ///< filename | // untaint languages. assigned by ^untaint[lang]{...} |
| HEADER, ///< text in response header | L_FILE_SPEC, ///< file specification |
| URI, ///< text in uri | L_HTTP_HEADER, ///< text in HTTP response header |
| TABLE, ///< ^table:set body | L_MAIL_HEADER, ///< text in mail header |
| SQL, ///< ^table:sql body | L_URI, ///< text in uri |
| JS, ///< JavaScript code | L_TABLE, ///< ^table:set body |
| HTML, ///< HTML code (for editing) | L_SQL, ///< ^table:sql body |
| HTML_TYPO ///< HTML code with TYPOgraphic replacements (for showing) | 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!=0); | |
| 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, ARRAY_OPTION_LIMIT_ALL); } | |
| void append_positions(const ArrayFragment& src, size_t substr_begin, size_t substr_end); | |
| size_t length() { | |
| size_t result=0; | |
| for(Array_iterator<element_type> i(*this); i.has_next(); ) { | |
| const Fragment fragment=i.next(); | |
| result+=fragment.length; | |
| } | |
| return result; | |
| } | |
| }; | |
| struct C { | |
| const char *str; | |
| size_t length; | |
| operator const char *() { return str; } | |
| C(const char *astr, size_t asize): str(astr), length(asize) {} | |
| }; | |
| struct Cm { | |
| char *str; | |
| size_t length; | |
| //operator char *() { return str; } | |
| Cm(char *astr, size_t asize): str(astr), length(asize) {} | |
| }; | }; |
| private: | |
| StringBody body; ///< all characters of string | |
| ArrayFragment fragments; ///< fragment language+length info | |
| public: | public: |
| String(Pool& apool, const char *src=0, bool tainted=false); | explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); |
| explicit String(const C cstr, bool tainted=false); | |
| String(const String& src); | String(const String& src); |
| size_t size() const { return fsize; } | String(StringBody abody, Language alang): body(abody) { |
| /// convert to C string | fragments+=Fragment(alang, abody.length()); |
| char *cstr() const; | |
| /** append fragment | |
| @see APPEND, APPEND_TAINTED, APPEND_CONST | |
| */ | |
| String& real_append(STRING_APPEND_PARAMS); | |
| /// @return <0 ==0 or >0 depending on comparison result | |
| int cmp (const String& src) const; | |
| bool operator < (const String& src) const { return cmp(src)<0; } | |
| bool operator > (const String& src) const { return cmp(src)>0; } | |
| bool operator <= (const String& src) const { return cmp(src)<=0; } | |
| bool operator >= (const String& src) const { return cmp(src)>=0; } | |
| bool operator == (const String& src) const { | |
| if(size()!=src.size()) // can speed up in trivial case | |
| return false; | |
| return cmp(src)==0; | |
| } | } |
| bool operator != (const String& src) const { return cmp(src)!=0; } | |
| bool operator == (const char* b_ptr) const; | |
| /** | |
| appends other String. | |
| marking all tainted pieces of it with @a lang. | |
| or marking ALL pieces of it with a @a lang when @a forced to. | |
| */ | |
| String& append(const String& src, Untaint_lang lang, bool forced=false); | |
| /// simple hash code of string. used by Hash | #define ASSERT_STRING_INVARIANT(string) \ |
| uint hash_code() const; | assert((string).body.length()==(string).fragments.length()) |
| #ifndef NO_STRING_ORIGIN | /// for convinient hash lookup |
| /// origin of string. calculated by first row | operator const StringBody() const { return body; } |
| const Origin& origin() const; | |
| #endif | |
| private: | bool is_empty() const { return body.is_empty(); } |
| size_t length() const { return body.length(); } | |
| struct Chunk { | /// convert to CORD. if 'lang' known, forcing 'lang' to it |
| // the number of rows in chunk | StringBody cstr_to_string_body(Language lang=L_AS_IS, |
| int count; | SQL_Connection* connection=0, |
| union Row { | const Request_charsets *charsets=0) const; |
| // fragment | |
| struct { | /// convert to constant C string. if 'lang' known, forcing 'lang' to it |
| const char *ptr; // pointer to the start | const char* cstr(Language lang=L_AS_IS, |
| size_t size; // length | SQL_Connection* connection=0, |
| Untaint_lang lang; // untaint flag, later untaint language | const Request_charsets *charsets=0) const { |
| #ifndef NO_STRING_ORIGIN | return cstr_to_string_body(lang, connection, charsets).cstr(); |
| Origin origin; // origin | } |
| #endif | /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it |
| } item; | char *cstrm(Language lang=L_AS_IS, |
| Chunk *link; // link to the next chunk in chain | SQL_Connection* connection=0, |
| } rows[CR_PREALLOCATED_COUNT]; | const Request_charsets *charsets=0) const { |
| // next rows are here | return cstr_to_string_body(lang, connection, charsets).cstrm(); |
| Chunk *preallocated_link; | } |
| } | /// puts pieces to buf |
| head; // the head chunk of the chunk chain | Cm serialize(size_t prolog_size) const; |
| /// appends pieces from buf to self | |
| // next append would write to this record | bool deserialize(size_t prolog_size, void *buf, size_t buf_size); |
| Chunk::Row *append_here; | /// @see StringBody::append_know_length |
| String& append_know_length(const char* str, size_t known_length, Language lang); | |
| // the address of place where lies address | /// @see StringBody::append_help_length |
| // of the link to the next chunk to allocate | String& append_help_length(const char* str, size_t helper_length, Language lang); |
| Chunk::Row *link_row; | String& append_strdup(const char* str, size_t helper_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; | |
| } | |
| private: | String& append_to(String& dest, Language lang, bool forced) const; |
| // last chunk | String& append(const String& src, Language lang, bool forced=false) { |
| Chunk *last_chunk; | 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; | |
| } | |
| // string size | /// extracts first char of a string, if any |
| size_t fsize; | char first_char() const { |
| return is_empty()?0:body.fetch(0); | |
| } | |
| // used rows in all chunks | bool operator < (const String& src) const { return body<src.body; } |
| int fused_rows; | 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; } | |
| private: | /// extracts [start, finish) piece of string |
| String& mid(size_t substr_begin, size_t substr_end) const; | |
| bool chunk_is_full() { | /** |
| return append_here == link_row; | 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 expand(); | |
| void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size); | 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 | private: //disabled |
| Line 187 private: //disabled | Line 363 private: //disabled |
| }; | }; |
| /// simple hash code of string. used by Hash | |
| inline uint hash_code(const StringBody self) { | |
| return self.hash_code(); | |
| } | |
| #endif | #endif |