|
|
| version 1.78, 2001/04/09 14:31:40 | 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 |
| #include "pa_config_includes.h" | static const char* IDENT_STRING_H="$Date$"; |
| #include <string.h> | // includes |
| #include <stddef.h> | |
| #include "pa_pool.h" | |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_array.h" | |
| extern "C" { // cord's author forgot to do that | |
| #define CORD_NO_IO | |
| #include "cord.h" | |
| }; | |
| // forwards | |
| class Charset; | |
| class Table; | 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 |
| $MAIN:html-typo table elements must enlarge string not more that that | #define STRING_NOT_FOUND ((size_t)-1) |
| that's a tradeoff - otherwise we'd have to scan string twice: | |
| - first for buffer length | |
| - second for replacements themselves | |
| */ | |
| #define UNTAINT_TIMES_BIGGER 10 | |
| #ifndef NO_STRING_ORIGIN | class StringBody { |
| # define STRING_APPEND_PARAMS \ | |
| const char *src, size_t size, \ | |
| String::Untaint_lang lang, \ | |
| const char *file, uint line | |
| /// appends piece to String @see String::real_append | |
| # define APPEND(src, size, lang, file, line) \ | |
| real_append(src, size, lang, file, line) | |
| #else | |
| # define STRING_APPEND_PARAMS \ | |
| const char *src, \ | |
| size_t size, \ | |
| String::Untaint_lang lang | |
| /// appends piece to String @see String::real_append | |
| # define APPEND(src, size, lang, file, line) \ | |
| real_append(src, size, lang) | |
| #endif | |
| /// appends clean piece to String @see String::real_append | |
| #define APPEND_CLEAN(src, size, file, line) \ | |
| APPEND(src, size, String::UL_CLEAN, file, line) | |
| /// appends tainted piece to String @see String::real_append | |
| #define APPEND_TAINTED(src, size, file, line) \ | |
| 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; | CORD body; |
| class SQL_Connection; | |
| /** | public: |
| Pooled string. | |
| 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); } | |
| bool is_empty() const { return body==CORD_EMPTY; } | |
| void append_know_length(const char *str, size_t known_length) { | |
| if(known_length) | |
| body=CORD_cat_char_star(body, str, known_length); | |
| } | |
| void append_strdup_know_length(const char* str, size_t known_length) { | |
| if(known_length) | |
| append_know_length(pa_strdup(str, known_length), known_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) { 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); | |
| } | |
| Internal structure: | template<typename I> void for_each(int (*callback)(const char* s, I), I info) const { |
| @verbatim | CORD_iter5(body, 0, 0, (CORD_batched_iter_fn)callback, info); |
| String Chunk0 | } |
| ====== ======== | |
| head--------------->[ptr, size, ...] | void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); } |
| append_here-------->[ptr, size, ...] | |
| . | StringBody normalize() const { |
| . | return StringBody(CORD_balance(body)); |
| [ptr, size, ...] | } |
| link_row----------->[link to the next chunk] | |
| @endverbatim | 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 { | /** 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_UNSPECIFIED=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_HTTP_HEADER, ///< text in HTTP response header | L_FILE_SPEC, ///< file specification |
| UL_MAIL_HEADER, ///< text in mail header | L_HTTP_HEADER, ///< text in HTTP response header |
| UL_URI, ///< text in uri | L_MAIL_HEADER, ///< text in mail header |
| UL_TABLE, ///< ^table:set body | L_URI, ///< text in uri |
| UL_SQL, ///< ^table:sql body | L_TABLE, ///< ^table:set body |
| UL_JS, ///< JavaScript code | L_SQL, ///< ^table:sql body |
| UL_HTML, ///< HTML code (for editing) | L_JS, ///< JavaScript code |
| UL_HTML_TYPO ///< HTML code with TYPOgraphic replacements (for showing) | 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) { | |
| assert(alang!=L_UNSPECIFIED); | |
| assert(asize!=0); | |
| assert(asize!=(size_t)-1); | |
| } | |
| }; | |
| String(Pool& apool, const char *src=0, size_t src_size=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, store to 'dest' which must be big enough for proper untaint | } |
| char *store_to(char *dest, | /// hiding from accidental USE, use append_positions |
| Untaint_lang lang=UL_UNSPECIFIED, SQL_Connection *connection=0) const; | void append(const ArrayFragment& src, int offset, int limit) { |
| /// convert to C string. if 'lang' known, forcing 'lang' to it | static_cast<Array<Fragment> *>(this)->append(src, offset, limit); |
| char *cstr(Untaint_lang lang=UL_UNSPECIFIED, SQL_Connection *connection=0) const { | } |
| char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1); | public: |
| char *eol=store_to(result, lang, connection); | ArrayFragment& operator += (element_type src) { |
| *eol=0; | if(size_t lcount=count()) { // not empty? |
| return result; | // try to join with last |
| } | Fragment& last=get_ref(lcount-1); |
| /** append fragment | if(last.lang==src.lang) { |
| @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST | last.length+=src.length; |
| */ | return *this; |
| String& real_append(STRING_APPEND_PARAMS); | } |
| /// @return <0 ==0 or >0 depending on comparison result | } |
| int cmp (int& partial, const String& src, | append(src); |
| size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const; | return *this; |
| 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; } | void append(const ArrayFragment& src) { append(src, 0, ARRAY_OPTION_LIMIT_ALL); } |
| bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; } | void append_positions(const ArrayFragment& src, size_t substr_begin, size_t substr_end); |
| bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; } | |
| bool operator == (const String& src) const { | size_t length() { |
| if(size()!=src.size()) // can speed up in trivial case | size_t result=0; |
| return false; | for(Array_iterator<element_type> i(*this); i.has_next(); ) { |
| int p; return cmp(p, src)==0; | const Fragment fragment=i.next(); |
| } | result+=fragment.length; |
| bool operator != (const String& src) const { int p; return cmp(p, src)!=0; } | } |
| return result; | |
| } | |
| }; | |
| /** | struct C { |
| @param partial | const char *str; |
| returns partial match status. | size_t length; |
| - -1: strings too different | operator const char *() { return str; } |
| - 0: full match | C(const 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_UNSPECIFIED) 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; | |
| } | |
| /** | struct Cm { |
| appends other String. | char *str; |
| size_t length; | |
| //operator char *() { return str; } | |
| Cm(char *astr, size_t asize): str(astr), length(asize) {} | |
| }; | |
| marking all tainted pieces of it with @a lang. | private: |
| 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); | |
| String& operator << (const String& src) { return append(src, UL_PASS_APPENDED); } | |
| String& operator << (const char *src) { return APPEND_CONST(src); } | |
| /// simple hash code of string. used by Hash | StringBody body; ///< all characters of string |
| uint hash_code() const; | ArrayFragment fragments; ///< fragment language+length info |
| /// extracts [start, finish) piece of string | public: |
| String& mid(size_t start, size_t finish) const; | |
| /// @return position of substr in string, -1 means "not found" [String version] | explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); |
| int pos(const String& substr, | explicit String(const C cstr, bool tainted=false); |
| size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const; | String(const String& src); |
| /// @return position of substr in string, -1 means "not found" [const char* version] | String(StringBody abody, Language alang): body(abody) { |
| int pos(const char *substr, size_t substr_size, | fragments+=Fragment(alang, abody.length()); |
| size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) 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; | |
| typedef void (*Row_action)(Table& table, Array *row, int start, int finish, | #define ASSERT_STRING_INVARIANT(string) \ |
| void *info); | assert((string).body.length()==(string).fragments.length()) |
| bool match(const String *aorigin, | |
| const String& regexp, | |
| const String *options, | |
| Table **table, | |
| Row_action row_action, void *info) const; | |
| #ifndef NO_STRING_ORIGIN | |
| /// origin of string. calculated by first row | |
| const Origin& origin() const; | |
| #endif | |
| private: | /// for convinient hash lookup |
| operator const StringBody() const { return body; } | |
| struct Chunk { | bool is_empty() const { return body.is_empty(); } |
| // the number of rows in chunk | size_t length() const { return body.length(); } |
| size_t count; | |
| union Row { | |
| // fragment | |
| 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: | /// convert to CORD. if 'lang' known, forcing 'lang' to it |
| // last chunk | StringBody cstr_to_string_body(Language lang=L_AS_IS, |
| Chunk *last_chunk; | 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 | |
| Cm 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_know_length | |
| String& append_know_length(const char* str, size_t known_length, Language lang); | |
| /// @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 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; | |
| } | |
| // string size | String& append_to(String& dest, Language lang, bool forced) const; |
| size_t fsize; | 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; | |
| } | |
| /// extracts first char of a string, if any | |
| 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 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 257 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 |