--- parser3/src/include/pa_string.h 2003/03/24 13:16:12 1.144.2.28.2.27 +++ parser3/src/include/pa_string.h 2003/03/27 14:51:27 1.144.2.28.2.41 @@ -8,7 +8,7 @@ #ifndef PA_STRING_H #define PA_STRING_H -static const char* IDENT_STRING_H="$Date: 2003/03/24 13:16:12 $"; +static const char* IDENT_STRING_H="$Date: 2003/03/27 14:51:27 $"; // includes @@ -16,6 +16,7 @@ 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" }; @@ -29,17 +30,6 @@ class Request_charsets; class String; typedef Array ArrayString; -// helpers -/* -/// appends clean piece to String @see String::append -#define APPEND_CLEAN(src, length) append(src, length, String::L_CLEAN) -/// appends piece to String as-is @see String::append -#define APPEND_AS_IS(src, length) append(src, length, String::L_AS_IS) -/// appends tainted piece to String @see String::append -#define APPEND_TAINTED(src, length) append(src, length, String::L_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) @@ -50,10 +40,20 @@ class StringBody { public: StringBody(): body(CORD_EMPTY) {} - StringBody(CORD abody): body(abody) {} - explicit StringBody(const char* str, size_t length): body(CORD_EMPTY) { - append(str, length); + StringBody(CORD abody): body(abody) { +#ifdef _DEBUG + if(body && !*body) + switch(*(char*)(body+1)) { + case 1: case 4: case 6: break; + default: _asm int 3; break; + } +#endif + } + /// 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_help_length(str, helper_length); } + static StringBody Format(int value); void clear() { body=CORD_EMPTY; } @@ -68,19 +68,32 @@ public: bool is_empty() const { return body==CORD_EMPTY; } - void append(const char *str, size_t length) { - body=CORD_cat_char_star(body, str, length); + void append_help_length(const char *str, size_t help_length) { +#ifdef _DEBUG + if(!*str) + _asm int 3; + bool were_empty=cstr==CORD_EMPTY; +#endif + body=CORD_cat_char_star(body, str, help_length); +#ifdef _DEBUG + if(were_empty && !*str) + _asm int 3; +#endif + } + String& append_strdup(const char* str, size_t length) { + append_help_length(pa_strdup(str, length), length); } void append(char c) { body=CORD_cat_char(body, c); } - void append(StringBody src) { body=CORD_cat(body, src.body); } + StringBody& operator << (const StringBody src) { body=CORD_cat(body, src.body); return *this; } + StringBody& operator << (const char* str) { body=CORD_cat_char_star(body, str, 0); 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; } + 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); @@ -89,7 +102,15 @@ public: 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 { return CORD_str(body, offset, substr.body); } + 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); @@ -143,7 +164,14 @@ public: struct Fragment { Language lang; ///< untaint flag, later untaint language size_t length; ///< length - Fragment(Language alang, size_t asize): lang(alang), length(asize) {} + Fragment(Language alang, size_t asize): lang(alang), length(asize) { +#ifdef _DEBUG + if(alang==L_UNSPECIFIED) + _asm int 3; // MUST NOT + if(asize==(size_t)-1) + _asm int 3; // MUST NOT +#endif + } }; class ArrayFragment: public Array { @@ -156,9 +184,9 @@ public: } public: ArrayFragment& operator += (element_type src) { - if(count()) { // not empty? + if(size_t lcount=count()) { // not empty? // try to join with last - Fragment& last=get(count()-1); + Fragment& last=unchecked_get_ref(lcount-1); if(last.lang==src.lang) { last.length+=src.length; return *this; @@ -185,8 +213,7 @@ private: 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); + explicit String(const char* cstr=0, size_t help_length=0, bool tainted=false); String(const String& src); String(StringBody abody, Language alang): body(abody) { fragments+=Fragment(alang, abody.length()); @@ -219,11 +246,9 @@ public: C serialize(size_t prolog_size) const; /// appends pieces from buf to self bool deserialize(size_t prolog_size, void *buf, size_t buf_size); - /** append fragment, - @c length can be zero, it will be autocalced - when length