|
|
| version 1.64, 2001/04/03 09:58:09 | version 1.144.2.28.2.38, 2003/03/26 18:52:49 |
|---|---|
| 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 |
| #include "pa_config_includes.h" | static const char* IDENT_STRING_H="$Date$"; |
| #include <stddef.h> | // includes |
| #include "pa_pool.h" | |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_array.h" | |
| class Table; | extern "C" { // cord's author forgot to do that |
| #define CORD_NO_IO | |
| #include "cord.h" | |
| }; | |
| // forwards | |
| /** | class Charset; |
| $MAIN:html-typo table elements must enlarge string not more that that | class Table; |
| that's a tradeoff - otherwise we'd have to scan string twice: | class SQL_Connection; |
| - first for buffer length | class Dictionary; |
| - second for replacements themselves | 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) | |
| */ | */ |
| #define UNTAINT_TIMES_BIGGER 10 | /// this is result of pos functions which mean that substr were not found |
| #define STRING_NOT_FOUND ((size_t)-1) | |
| #ifndef NO_STRING_ORIGIN | class StringBody { |
| # define STRING_APPEND_PARAMS \ | |
| const char *src, size_t size, \ | CORD body; |
| String::Untaint_lang lang, \ | |
| const char *file, uint line | public: |
| /// appends piece to String @see String::real_append | |
| # define APPEND(src, size, lang, file, line) \ | StringBody(): body(CORD_EMPTY) {} |
| real_append(src, size, lang, file, line) | StringBody(CORD abody): body(abody) { |
| #else | #ifdef _DEBUG |
| # define STRING_APPEND_PARAMS \ | if(body && !*body) |
| const char *src, \ | switch(*(char*)(body+1)) { |
| size_t size, \ | case 1: case 4: case 6: break; |
| String::Untaint_lang lang | default: _asm int 3; break; |
| /// appends piece to String @see String::real_append | } |
| # define APPEND(src, size, lang, file, line) \ | |
| real_append(src, size, lang) | |
| #endif | #endif |
| /// appends clean piece to String @see String::real_append | } |
| #define APPEND_CLEAN(src, size, file, line) \ | StringBody(const char* str, size_t length): body(CORD_EMPTY) { |
| APPEND(src, size, String::UL_CLEAN, file, line) | append(str, length); |
| /// appends tainted piece to String @see String::real_append | } |
| #define APPEND_TAINTED(src, size, file, line) \ | static StringBody Format(int value); |
| APPEND(src, size, String::UL_TAINTED, file, line) | |
| /// handy: appends const char* piece to String @see String::real_append | |
| #define APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0) | |
| class Array; | void clear() { body=CORD_EMPTY; } |
| /** | bool operator! () const { return is_empty(); } |
| Pooled string. | |
| 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(const char *str, size_t length) { |
| ====== ======== | #ifdef _DEBUG |
| head--------------->[ptr, size, ...] | if(!*str) |
| append_here-------->[ptr, size, ...] | _asm int 3; |
| . | bool were_empty=cstr==CORD_EMPTY; |
| . | #endif |
| [ptr, size, ...] | body=CORD_cat_char_star(body, str, length); |
| link_row----------->[link to the next chunk] | #ifdef _DEBUG |
| @endverbatim | if(were_empty && !*str) |
| _asm int 3; | |
| #endif | |
| } | |
| 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, 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); } | |
| }; | |
| /** | |
| 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 { | /** piece is tainted or not. the language to use when detaint |
| CR_PREALLOCATED_COUNT=5, ///< default preallocated item count | remember to change String_Untaint_lang_name @ untaint.C along |
| CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded() | */ |
| }; | 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 | |
| /// piece is tainted or not. the language to use when detaint | L_PASS_APPENDED, |
| enum Untaint_lang { | |
| UL_UNKNOWN=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum | |
| UL_CLEAN, ///< clean | |
| UL_TAINTED, ///< tainted, untaint language as assigned later | |
| // untaint languages. assigned by ^untaint[lang]{...} | |
| UL_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 |
| */ | */ |
| UL_AS_IS, ///< leave all characters intact | L_TAINTED, ///< tainted, untaint language as assigned later |
| UL_FILE_NAME, ///< filename | // untaint languages. assigned by ^untaint[lang]{...} |
| UL_HEADER, ///< text in response header | L_FILE_SPEC, ///< file specification |
| UL_URI, ///< text in uri | L_HTTP_HEADER, ///< text in HTTP response header |
| UL_TABLE, ///< ^table:set body | L_MAIL_HEADER, ///< text in mail header |
| UL_SQL, ///< ^table:sql body | L_URI, ///< text in uri |
| UL_JS, ///< JavaScript code | L_TABLE, ///< ^table:set body |
| UL_HTML, ///< HTML code (for editing) | L_SQL, ///< ^table:sql body |
| UL_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 | |
| }; | }; |
| public: | struct Fragment { |
| Language lang; ///< untaint flag, later untaint language | |
| size_t length; ///< length | |
| Fragment(Language alang, size_t asize): lang(alang), length(asize) { | |
| #ifdef _DEBUG | |
| if(alang==L_UNSPECIFIED) | |
| _asm int 3; // MUST NOT | |
| if(asize==(size_t)-1) | |
| _asm int 3; // MUST NOT | |
| #endif | |
| } | |
| }; | |
| String(Pool& apool, const char *src=0, bool tainted=false); | 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; |
| /// convert to C string | } |
| char *cstr() const { | /// hiding from accidental USE, use append_positions |
| char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1); | void append(const ArrayFragment& src, int offset, int limit) { |
| char *eol=store_to(result); | static_cast<Array<Fragment> *>(this)->append(src, offset, limit); |
| *eol=0; | } |
| return result; | public: |
| } | ArrayFragment& operator += (element_type src) { |
| /** append fragment | if(size_t lcount=count()) { // not empty? |
| @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST | // try to join with last |
| */ | Fragment& last=unchecked_get(lcount-1); |
| String& real_append(STRING_APPEND_PARAMS); | if(last.lang==src.lang) { |
| /// @return <0 ==0 or >0 depending on comparison result | last.length+=src.length; |
| int cmp (int& partial, const String& src, | return *this; |
| size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; | } |
| bool operator < (const String& src) const { int p; return cmp(p, src)<0; } | } |
| bool operator > (const String& src) const { int p; return cmp(p, src)>0; } | append(src); |
| bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; } | return *this; |
| bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; } | } |
| bool operator == (const String& src) const { | void append(const ArrayFragment& src) { append(src, 0, 0); } |
| if(size()!=src.size()) // can speed up in trivial case | void append_positions(const ArrayFragment& src, size_t substr_begin, size_t substr_end); |
| return false; | }; |
| int p; return cmp(p, src)==0; | |
| } | |
| bool operator != (const String& src) const { int p; return cmp(p, src)!=0; } | |
| /** | struct C { |
| @param partial | char *str; |
| returns partial match status. | size_t length; |
| - -1: strings too different | operator char *() { return str; } |
| - 0: full match | C(char *astr, size_t asize): str(astr), length(asize) {} |
| - 1: means @c this starts @c src | }; |
| - 2: means @src starts @this | |
| */ | |
| int cmp(int& partial, const char* src_ptr, size_t src_size=0, | |
| size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; | |
| bool operator == (const char* src_ptr) const { | |
| size_t src_size=src_ptr?strlen(src_ptr):0; | |
| if(size() != src_size) | |
| return false; | |
| int partial; // unused | |
| return cmp(partial, src_ptr, src_size)==0; | |
| } | |
| /** | private: |
| appends other String. | |
| marking all tainted pieces of it with @a lang. | StringBody body; ///< all characters of string |
| or marking ALL pieces of it with a @a lang when @a forced to. | ArrayFragment fragments; ///< fragment language+length info |
| */ | |
| String& append(const String& src, Untaint_lang lang, bool forced=false); | |
| /// simple hash code of string. used by Hash | public: |
| uint hash_code() const; | |
| /// extracts [start, finish) piece of string | /// @todo check all places for length to be only HELPER length, and never be !=strlen(cstr) |
| String& piece(size_t start, size_t finish) const; | explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); |
| String(const String& src); | |
| String(StringBody abody, Language alang): body(abody) { | |
| fragments+=Fragment(alang, abody.length()); | |
| } | |
| /// @return position of substr in string, -1 means "not found" [String version] | /// for convinient hash lookup |
| int pos(const String& substr, | operator const StringBody() const { return body; } |
| size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; | |
| /// @return position of substr in string, -1 means "not found" [const char* version] | |
| int pos(const char *substr, size_t substr_size, | |
| size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; | |
| void split(Array& result, | |
| size_t *pos_after_ref, | |
| const char *delim, size_t delim_size, | |
| Untaint_lang lang, int limit=-1) const; | |
| void split(Array& result, | |
| size_t *pos_after_ref, | |
| const String& delim, | |
| Untaint_lang lang, int limit=-1) const; | |
| bool match(const String *aorigin, | bool is_empty() const { return body.is_empty(); } |
| const String& regexp, | size_t length() const { return body.length(); } |
| const String& options, | |
| Table **table) const; | |
| #ifndef NO_STRING_ORIGIN | /// convert to CORD. if 'lang' known, forcing 'lang' to it |
| /// origin of string. calculated by first row | StringBody cstr_to_string_body(Language lang=L_AS_IS, |
| const Origin& origin() const; | SQL_Connection* connection=0, |
| #endif | 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); | |
| /** 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); | |
| private: | bool operator == (const char* y) const { return body==StringBody(y); } |
| bool operator != (const char* y) const { return body!=StringBody(y); } | |
| struct Chunk { | /// this starts with y |
| // the number of rows in chunk | bool starts_with(const char* y) const { |
| size_t count; | return body.ncmp(0/*x_begin*/, StringBody(y), 0/*y_begin*/, strlen(y))==0; |
| union Row { | } |
| // fragment | /// x starts with this |
| struct { | bool this_starts(const char* x) const { |
| const char *ptr; // pointer to the start | return StringBody(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0; |
| 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: | 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(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(); | |
| char *String::store_to(char *dest) const; | 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 247 private: //disabled | Line 355 private: //disabled |
| }; | }; |
| /// simple hash code of string. used by Hash | |
| inline uint hash_code(const StringBody self) { | |
| return self.hash_code(); | |
| } | |
| #endif | #endif |