--- parser3/src/include/pa_string.h 2003/03/20 16:57:43 1.144.2.28.2.16 +++ parser3/src/include/pa_string.h 2003/09/24 08:02:09 1.145.4.6 @@ -8,7 +8,7 @@ #ifndef PA_STRING_H #define PA_STRING_H -static const char* IDENT_STRING_H="$Date: 2003/03/20 16:57:43 $"; +static const char* IDENT_STRING_H="$Date: 2003/09/24 08:02:09 $"; // includes @@ -16,9 +16,19 @@ static const char* IDENT_STRING_H="$Date #include "pa_array.h" extern "C" { // cord's author forgot to do that +#define CORD_NO_IO #include "cord.h" }; +/// must use simple version, without optimization for short pieces +/// it's much more convinient for iterating. we rely on this here: CORD_block_iter +#define CORD_chars(c, i) CORD_chars_block((c), (i)) + +// cord extension +/* Returns true if x does contain */ +/* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x). */ +int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c); + // forwards class Charset; @@ -27,197 +37,395 @@ class SQL_Connection; class Dictionary; class Request_charsets; class String; -typedef Array ArrayString; +typedef Array ArrayString; -// helpers -/* -/// appends clean piece to String @see String::append -#define APPEND_CLEAN(src, length) append(src, length, String::UL_CLEAN) -/// appends piece to String as-is @see String::append -#define APPEND_AS_IS(src, length) append(src, length, String::UL_AS_IS) -/// appends tainted piece to String @see String::append -#define APPEND_TAINTED(src, length) append(src, length, String::UL_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) +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); } + + 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); + } + +/* template 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); + } +}; + +template +inline size_t get_length(T current) { + return current; +} /** - String which knows the language of all it's fragments. + String which knows the lang of all it's langs. All pieces remember - whether they are tainted or not, - and the language which should be used to detaint them + and the lang which should be used to detaint them */ class String: public PA_Object { - public: - /** piece is tainted or not. the language to use when detaint + /** piece is tainted or not. the lang to use when detaint remember to change String_Untaint_lang_name @ untaint.C along */ - enum Untaint_lang { - UL_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum - // these two must go before others, there are checks for >UL_AS_IS - UL_CLEAN, ///< clean - UL_AS_IS, ///< leave all characters intact + enum Language { + L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty + // these two must go before others, there are checks for >L_AS_IS + L_CLEAN, ///< clean + L_AS_IS, ///< leave all characters intact - UL_PASS_APPENDED, + L_PASS_APPENDED, /**< - leave language built into string being appended. + leave lang built into string being appended. just a flag, that value not stored */ - UL_TAINTED, ///< tainted, untaint language as assigned later - // untaint languages. assigned by ^untaint[lang]{...} - UL_FILE_SPEC, ///< file specification - UL_HTTP_HEADER, ///< text in HTTP response header - UL_MAIL_HEADER, ///< text in mail header - UL_URI, ///< text in uri - UL_TABLE, ///< ^table:set body - UL_SQL, ///< ^table:sql body - UL_JS, ///< JavaScript code - UL_XML, ///< ^dom:set xml - UL_HTML, ///< HTML code (for editing) - UL_OPTIMIZE_BIT = 0x8000 ///< flag, requiring cstr whitespace optimization + L_TAINTED, ///< tainted, untaint lang as assigned later + // untaint langs. 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 = 0x80 ///< flag, requiring cstr whitespace optimization }; - struct Fragment { - Untaint_lang lang; ///< untaint flag, later untaint language - size_t length; ///< length - Fragment(Untaint_lang alang, size_t asize): lang(alang), length(asize) {} - }; + class Languages { + + union { + struct { + Language lang:8; + int is_not_just_lang:sizeof(CORD)*8-8; + }; + CORD langs; + }; + + template + CORD make_langs(C current) const { + return is_not_just_lang? + langs + :CORD_chars((char)lang, get_length(current)); + } + + CORD make_langs(size_t aoffset, size_t alength) const { + return is_not_just_lang? + CORD_substr(langs, aoffset, alength) + :CORD_chars((char)lang, alength); + } - class ArrayFragment: public Array { - void append(element_type src) { - *static_cast *>(this)+=src; - } - /// hiding from accidental USE, use append_positions - void append(const ArrayFragment& src, int offset, int limit) { - static_cast *>(this)->append(src, offset, limit); + /// appending when 'langs' already contain something [simple cases hanled elsewhere] + template + void append(C current, + const CORD to_nonempty_target_langs) { + assert(langs); + + if(is_not_just_lang) + langs=CORD_cat(langs, to_nonempty_target_langs); + else { // we were "just lang" + size_t current_size=get_length(current); + assert(current_size); + langs=CORD_cat( + CORD_chars((char)lang, current_size), // first piece [making from just 'lang'] + to_nonempty_target_langs); // new piece + } } + public: - ArrayFragment& operator += (element_type src) { - if(count()) { // not empty? - // try to join with last - Fragment& last=get(count()-1); - if(last.lang==src.lang) { - last.length+=src.length; - return *this; + + Languages(): langs(0) {} + Languages(Language alang): lang(alang), is_not_just_lang(0) {} + + /// MUST be called exactly prior to modification of current [uses it's length] + template + void append(C current, Language alang, size_t asize) { + assert(alang); + assert(asize); + + if(!is_not_just_lang) + if(lang) { + if(lang==alang) // same length? ignoring + return; + } else { + lang=alang; // to uninitialized + return; } - } - append(src); - return *this; + + append(current, CORD_chars((char)alang, asize)); + } + + /// MUST be called exactly prior to modification of current [uses it's length] + template + void append(C current, size_t appending_length, + const Languages src) { + assert(appending_length); + + if(!langs) + langs=src.langs; // to uninitialized + else if(!src.is_not_just_lang) + append(current, src.lang, appending_length); // simplifying when simple source + else + append(current, src.make_langs(appending_length)); + } + + /// MUST be called exactly prior to modification of current [uses it's length] + template + void append(C current, + const Languages src, size_t aoffset, size_t alength) { + assert(alength); + + if(!langs) // to uninitialized? + if(src.is_not_just_lang) + langs=CORD_substr(src.langs, aoffset, alength); // to uninitialized complex + else + lang=src.lang; // to uninitialized simple + else + if(!is_not_just_lang && !src.is_not_just_lang && lang==src.lang) // both simple & of same language? + return; // ignoring + else + append(current, src.make_langs(aoffset, alength)); + } + + /// checks if we have lang<=alang all from aoffset to aoffset+alength + bool check_lang(Language alang, size_t aoffset, size_t alength) const { + if(alang==L_UNSPECIFIED) // ignore lang? + return true; + + if(is_not_just_lang) + return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0; + else + return lang<=alang; + } + + template + void for_each(C current, + int callback(char, size_t, I), I info) const { + + if(is_not_just_lang) + CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info); + else + callback(lang, get_length(current), info); + } + + bool invariant(size_t current_length) { + if(!langs) + return current_length==0; + if(is_not_just_lang) + return !*langs && CORD_len(langs)==current_length; + return true; // uncheckable, actually } - void append(const ArrayFragment& src) { append(src, 0, 0); } - void append_positions(const ArrayFragment& src, size_t substr_begin, size_t substr_end); }; 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; } - C(char *astr, size_t asize): str(astr), length(asize) {} + //operator char *() { return str; } + Cm(char *astr, size_t asize): str(astr), length(asize) {} }; private: - CORD body; ///< all characters of string - ArrayFragment fragments; ///< fragment language+length info + Languages langs; ///< string characters lang + StringBody body; ///< all characters of string + +#define ASSERT_STRING_INVARIANT(string) \ + assert((string).langs.invariant((string).body.length())) public: - /// @todo check all places for length to be only HELPER length, and never be !=strlen(cstr) explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); - String(const String& src); - operator bool() const { return body!=CORD_EMPTY; } - size_t length() const { return CORD_len(body); } + explicit String(const C cstr, bool tainted=false); + String(StringBody abody, Language alang): body(abody), langs(alang) { + assert(!body.is_empty()); + ASSERT_STRING_INVARIANT(*this); + } + String(const String& src): body(src.body), langs(src.langs) { + ASSERT_STRING_INVARIANT(*this); + } + + /// for convinient hash lookup + operator const StringBody() const { return body; } + + bool is_empty() const { return body.is_empty(); } + size_t length() const { return body.length(); } /// convert to CORD. if 'lang' known, forcing 'lang' to it - CORD cstr_to_cord(Untaint_lang lang=UL_AS_IS, - SQL_Connection *connection=0, + 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(Untaint_lang lang=UL_AS_IS, - SQL_Connection *connection=0, + 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)); + return cstr_to_string_body(lang, connection, charsets).cstr(); } /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it - char *cstrm(Untaint_lang lang=UL_AS_IS, - SQL_Connection *connection=0, + 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)); + return cstr_to_string_body(lang, connection, charsets).cstrm(); } /// puts pieces to buf - C serialize(size_t prolog_size) const; + 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, const char* file); - /** append fragment, - @c helper_length can be zero, it will be autocalced - when length (const String& src) const { return CORD_cmp(body, src.body)>0; } - bool operator <= (const String& src) const { return CORD_cmp(body, src.body)<=0; } - bool operator >= (const String& src) const { return CORD_cmp(body, src.body)>=0; } - bool operator == (const String& src) const { return CORD_cmp(body, src.body)==0; } - bool operator != (const String& src) const { return CORD_cmp(body, src.body)!=0; } + 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(CORD y) const { - return CORD_ncmp(body, 0/*x_begin*/, y, 0/*y_begin*/, CORD_len(y))==0; + 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(CORD y) const { - return CORD_ncmp(body, 0/*x_begin*/, y, 0/*y_begin*/, CORD_len(body))==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; } - bool operator == (CORD y) const { return CORD_cmp(body, y)==0; } - bool operator != (CORD y) const { return CORD_cmp(body, y)!=0; } - String& append_to(String& dest, Untaint_lang lang, bool forced) const; - String& append(const String& src, Untaint_lang lang, bool forced=false) { + 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, UL_PASS_APPENDED); } - String& operator << (const char* src) { return append(src, 0, UL_AS_IS); } - - /// simple hash code of string - uint hash_code() const; + 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) { + langs.append(body, L_AS_IS, src.length()); + body< (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; + String& mid(size_t substr_begin, size_t substr_end) const; /** - ignore lang if it's UL_UNSPECIFIED + 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, Untaint_lang lang=UL_UNSPECIFIED) const; - /// String version of @see pos(const char*, int, Untaint_lang) + 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, Untaint_lang lang=UL_UNSPECIFIED) const; + 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, + size_t& pos_after, const char* delim, - Untaint_lang lang=UL_UNSPECIFIED, int limit=-1); + Language lang=L_UNSPECIFIED, int limit=-1) const; void split(ArrayString& result, size_t& pos_after, const String& delim, - Untaint_lang lang=UL_UNSPECIFIED, int limit=-1) const; + Language lang=L_UNSPECIFIED, int limit=-1) const; - typedef void (*Row_action)(Table* table, ArrayString* row, + typedef void (*Row_action)(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info); @@ -230,7 +438,7 @@ public: */ Table* match(Charset& source_charset, const String& regexp, - const String& options, + const String* options, Row_action row_action, void *info, bool& just_matched) const; enum Change_case_kind { @@ -238,7 +446,7 @@ public: CC_LOWER }; String& change_case(Charset& source_charset, - Change_case_kind kind); + Change_case_kind kind) const; const String& replace(const Dictionary& dict) const; double as_double() const; int as_int() const; @@ -249,8 +457,13 @@ private: //disabled }; +template<> +inline size_t get_length(StringBody body) { + return body.length(); +} + /// simple hash code of string. used by Hash -inline uint hash_code(const String& self) { +inline uint hash_code(const StringBody self) { return self.hash_code(); }