--- parser3/src/include/pa_string.h 2009/05/13 07:35:52 1.181 +++ parser3/src/include/pa_string.h 2009/05/23 08:12:47 1.186 @@ -8,7 +8,7 @@ #ifndef PA_STRING_H #define PA_STRING_H -static const char * const IDENT_STRING_H="$Date: 2009/05/13 07:35:52 $"; +static const char * const IDENT_STRING_H="$Date: 2009/05/23 08:12:47 $"; // includes #include "pa_types.h" @@ -17,10 +17,18 @@ static const char * const IDENT_STRING_H extern "C" { // cord's author forgot to do that #define CORD_NO_IO #include "cord.h" + +#ifdef CORD_CAT_OPTIMIZATION +#define CORD_cat(x, y) CORD_cat_optimized(x, y) +#define CORD_cat_char_star(x, y, leny) CORD_cat_char_star_optimized(x, y, leny) +#endif }; // defines +// cache hash code in String::Body for faster hash access +#define HASH_CODE_CACHING + // 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). */ @@ -192,7 +200,7 @@ public: if(!opt.is_not_just_lang) if(opt.lang) { - if(opt.lang==alang) // same length? ignoring + if(opt.lang==alang) // same language? ignoring return; } else { opt.lang=alang; // to uninitialized @@ -203,10 +211,14 @@ public: } template - void appendHelper(C current, C length_helper, const Languages src) { - - if(!langs) + void appendHelper(C current, const Languages src, C length_helper) { + if(!langs){ langs=src.langs; // to uninitialized +#ifdef CORD_CAT_OPTIMIZATION + if(opt.is_not_just_lang && !CORD_IS_STRING(langs)) + CORD_concatenation_protect(langs); +#endif + } else if(!src.opt.is_not_just_lang) appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source else @@ -276,19 +288,41 @@ public: CORD body; +#ifdef HASH_CODE_CACHING + // cached hash code is not reseted on write operations as test shows + // that string body does not change after it is stored as a hash key + mutable uint hash_code; +#endif + public: const char* v() const; void dump() const; +#ifdef HASH_CODE_CACHING + Body(): body(CORD_EMPTY), hash_code(0) {} + Body(CORD abody, uint ahash_code): body(abody), hash_code(ahash_code) {} + Body(CORD abody): body(abody), hash_code(0) { +#else Body(): body(CORD_EMPTY) {} Body(CORD abody): body(abody) { +#endif +#ifdef CORD_CAT_OPTIMIZATION assert(!body // no body || *body // ordinary string || body[1]==1 // CONCAT_HDR + || body[1]==3 // CONCAT_HDR_READ_ONLY || body[1]==4 // FN_HDR || body[1]==6 // SUBSTR_HDR ); +#else + assert(!body // no body + || *body // ordinary string + || body[1]==1 // CONCAT_HDR + || body[1]==4 // FN_HDR + || body[1]==6 // SUBSTR_HDR + ); +#endif } static Body Format(int value); @@ -297,7 +331,8 @@ public: bool operator! () const { return is_empty(); } - uint hash_code() const; + CORD get_cord() const { return body; } + uint get_hash_code() const; const char* cstr() const { return CORD_to_const_char_star(body); } char* cstrm() const { return CORD_to_char_star(body); } @@ -403,18 +438,19 @@ public: static const String Empty; explicit String(){}; - explicit String(const char* cstr, bool tainted=false){ + explicit String(const char* cstr, Language alang=L_CLEAN){ if(cstr && *cstr){ body=cstr; - langs=tainted ? L_TAINTED:L_CLEAN; + langs=alang; } } - explicit String(const String::C cstr, bool tainted=false){ + explicit String(const String::C cstr, Language alang=L_CLEAN){ if(cstr.length){ body=cstr.str; - langs=tainted ? L_TAINTED:L_CLEAN; + langs=alang; } } + String(int value, char *format); String(Body abody, Language alang): body(abody), langs(alang) { ASSERT_STRING_INVARIANT(*this); } @@ -423,7 +459,11 @@ public: } /// for convinient hash lookup +#ifdef HASH_CODE_CACHING + operator const Body&() const { return body; } +#else operator const Body() const { return body; } +#endif bool is_empty() const { return body.is_empty(); } size_t length() const { return body.length(); } @@ -557,10 +597,12 @@ inline size_t get_length(S return body.length(); } +#ifndef HASH_CODE_CACHING /// simple hash code of string. used by Hash inline uint hash_code(const String::Body self) { - return self.hash_code(); + return self.get_hash_code(); } +#endif /// now that we've declared specialization we can use it