--- parser3/src/include/pa_string.h 2003/07/24 11:31:21 1.145 +++ parser3/src/include/pa_string.h 2003/09/23 10:37:05 1.145.4.2 @@ -8,7 +8,7 @@ #ifndef PA_STRING_H #define PA_STRING_H -static const char* IDENT_STRING_H="$Date: 2003/07/24 11:31:21 $"; +static const char* IDENT_STRING_H="$Date: 2003/09/23 10:37:05 $"; // includes @@ -20,6 +20,13 @@ extern "C" { // cord's author forgot to #include "cord.h" }; +CORD CORD_chars_gen(char c, size_t 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_nnchr(CORD x, size_t i, size_t n, int not_c); + // forwards class Charset; @@ -104,15 +111,15 @@ public: return CORD_chr(body, offset, c); } - template void for_each(int (*callback)(const char* s, I), I info) const { +/* 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 { + /*StringBody normalize() const { return StringBody(CORD_balance(body)); - } + }*/ void dump() const { CORD_dump(body); @@ -120,34 +127,31 @@ public: }; /** - 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 { - -// friend class StringBody; - 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 Language { - L_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum + 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 L_PASS_APPENDED, /**< - leave language built into string being appended. + leave lang 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]{...} + 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 @@ -157,50 +161,112 @@ public: L_JS, ///< JavaScript code L_XML, ///< ^dom:set xml L_HTML, ///< HTML code (for editing) - L_OPTIMIZE_BIT = 0x8000 ///< flag, requiring cstr whitespace optimization + L_OPTIMIZE_BIT = 0x80 ///< 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 Languages { + + union { + struct { + Language lang:8; + int is_not_just_lang:16-8; + }; + CORD langs; + }; + + CORD make_langs(const StringBody body) const { + return is_not_just_lang? + langs + :CORD_chars_gen((char)lang, body.length()); } - }; - class ArrayFragment: public Array { - void append(element_type src) { - *static_cast *>(this)+=src; + CORD make_langs(size_t aoffset, size_t alength) const { + return is_not_just_lang? + CORD_substr(langs, aoffset, alength) + :CORD_chars_gen((char)lang, alength); } - /// hiding from accidental USE, use append_positions - void append(const ArrayFragment& src, int offset, int limit) { - static_cast *>(this)->append(src, offset, limit); + + /// @returns true if appended, false if not: + /// when appending can be simplified by just assigning lang/langs + bool append(const StringBody current_body, + const CORD to_nonempty_target_langs) { + if(!lang) + return false; + + if(is_not_just_lang) + langs=CORD_cat(langs, to_nonempty_target_langs); + else { // we were "just lang" + size_t current_size=current_body.length(); + assert(current_size); + langs=CORD_cat( + CORD_chars_gen((char)lang, current_size), // first piece [making from just 'lang'] + to_nonempty_target_langs); // new piece + } + + return true; } + 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; + + Languages(): langs(0) {} + Languages(Language alang): lang(alang), is_not_just_lang(0) {} + + /// MUST be called prior to modification of current_body [uses it's original length] + void append(const StringBody current_body, Language alang, size_t asize) { + assert(alang); + assert(asize); + + if(!is_not_just_lang && lang==alang) + return; + + if(!append(current_body, CORD_chars((char)alang, asize))) + lang=alang; } - 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 i(*this); i.has_next(); ) { - const Fragment fragment=i.next(); - result+=fragment.length; - } - return result; + /// MUST be called prior to modification of current_body [uses it's original length] + void append(const StringBody current_body, const StringBody appending_body, + const Languages src) { + assert(appending_body.length()); + + if(!is_not_just_lang && !src.is_not_just_lang && lang==src.lang) + return; + + if(!append(current_body, src.make_langs(appending_body))) + langs=src.langs; + } + + /// MUST be called prior to modification of current_body [uses it's original length] + void append(const StringBody current_body, + const Languages src, size_t aoffset, size_t alength) { + assert(alength); + if(!is_not_just_lang && !src.is_not_just_lang && lang==src.lang) + return; + + if(!append(current_body, src.make_langs(aoffset, alength))) + if(src.is_not_just_lang) + langs=CORD_substr(src.langs, aoffset, alength); + else + lang=src.lang; + } + + /// checks if we have 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_nnchr(langs, aoffset, alength, (unsigned)alang)!=0; + else + return lang==alang; + } + + template void for_each(const StringBody current_body, + 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, current_body.length(), info); } }; @@ -220,20 +286,15 @@ public: private: + Languages langs; ///< string characters lang StringBody body; ///< all characters of string - ArrayFragment fragments; ///< fragment language+length info public: 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(StringBody abody, Language alang): body(abody) { - fragments+=Fragment(alang, abody.length()); - } - -#define ASSERT_STRING_INVARIANT(string) \ - assert((string).body.length()==(string).fragments.length()) + String(StringBody abody, Language alang): body(abody), langs(alang) {} + String(const String& src): body(src.body), langs(src.langs) {} /// for convinient hash lookup operator const StringBody() const { return body; } @@ -287,8 +348,8 @@ public: 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<