--- parser3/src/include/pa_string.h 2003/07/24 11:31:21 1.145 +++ parser3/src/include/pa_string.h 2003/09/24 11:52:05 1.145.4.8 @@ -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/24 11:52:05 $"; // includes @@ -20,6 +20,11 @@ extern "C" { // cord's author forgot to #include "cord.h" }; +// 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; @@ -104,50 +109,51 @@ 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); } }; +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 { - -// 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_CLEAN='1', ///< 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 +163,131 @@ 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: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)); } - }; - 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((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); + + /// 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_append_blocks(langs, to_nonempty_target_langs); + else { // we were "just lang" + size_t current_size=get_length(current); + assert(current_size); + langs=CORD_append_blocks( + 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(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; + + 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)); } - 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 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 CORD_len(langs)==current_length; + return true; // uncheckable, actually } }; @@ -220,20 +307,23 @@ public: private: + Languages langs; ///< string characters lang StringBody body; ///< all characters of string - ArrayFragment fragments; ///< fragment language+length info + +#define ASSERT_STRING_INVARIANT(string) \ + assert((string).langs.invariant((string).body.length())) 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()); + 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); } - -#define ASSERT_STRING_INVARIANT(string) \ - assert((string).body.length()==(string).fragments.length()) /// for convinient hash lookup operator const StringBody() const { return body; } @@ -287,8 +377,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< +inline size_t get_length(StringBody body) { + return body.length(); +} + /// simple hash code of string. used by Hash inline uint hash_code(const StringBody self) { return self.hash_code();