--- parser3/src/include/pa_string.h 2001/03/25 08:52:34 1.52 +++ parser3/src/include/pa_string.h 2001/04/03 05:23:40 1.62 @@ -5,7 +5,7 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_string.h,v 1.52 2001/03/25 08:52:34 paf Exp $ + $Id: pa_string.h,v 1.62 2001/04/03 05:23:40 paf Exp $ */ #ifndef PA_STRING_H @@ -31,30 +31,28 @@ const char *src, size_t size, \ String::Untaint_lang lang, \ const char *file, uint line -/// appends clean piece to String @see String::real_append -# define APPEND(src, size, file, line) \ - real_append(src, size, String::UL_NO, file, line) -/// appends tainted piece to String @see String::real_append -# define APPEND_TAINTED(src, size, file, line) \ - real_append(src, size, String::UL_YES, file, line) -# define APPEND_SPECIFIC_TAINTED(src, size, lang, file, line) \ - real_append(src, size, lang, file, line) +/// appends piece to String @see String::real_append +# define APPEND(src, size, lang, file, line) \ + real_append(src, size, lang, file, line) #else # define STRING_APPEND_PARAMS \ const char *src, \ size_t size, \ String::Untaint_lang lang +/// appends piece to String @see String::real_append +# define APPEND(src, size, lang, file, line) \ + real_append(src, size, lang) +#endif /// appends clean piece to String @see String::real_append -# define APPEND(src, size, file, line) \ - real_append(src, size, String::UL_NO) +#define APPEND_CLEAN(src, size, file, line) \ + APPEND(src, size, String::UL_CLEAN, file, line) /// appends tainted piece to String @see String::real_append -# define APPEND_TAINTED(src, size, file, line) \ - real_append(src, size, String::UL_YES) -# define APPEND_SPECIFIC_TAINTED(src, size, lang, file, line) \ - real_append(src, size, lang) -#endif +#define APPEND_TAINTED(src, size, file, line) \ + APPEND(src, size, String::UL_TAINTED, file, line) /// handy: appends const char* piece to String @see String::real_append -#define APPEND_CONST(src) APPEND(src, 0, 0, 0) +#define APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0) + +class Array; /** Pooled string. @@ -86,9 +84,9 @@ public: /// piece is tainted or not. the language to use when detaint enum Untaint_lang { - UL_UNKNOWN=0, ///< when get by name fails - UL_NO, ///< clean - UL_YES, ///< tainted, untaint language as assigned later + UL_UNKNOWN=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum + UL_CLEAN, ///< clean + UL_TAINTED, ///< tainted, untaint language as assigned later // untaint languages. assigned by ^untaint[lang]{...} UL_PASS_APPENDED, /**< @@ -119,21 +117,22 @@ public: return result; } /** append fragment - @see APPEND, APPEND_TAINTED, APPEND_CONST + @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST */ String& real_append(STRING_APPEND_PARAMS); /// @return <0 ==0 or >0 depending on comparison result - int cmp (const String& src) const; - bool operator < (const String& src) const { return cmp(src)<0; } - bool operator > (const String& src) const { return cmp(src)>0; } - bool operator <= (const String& src) const { return cmp(src)<=0; } - bool operator >= (const String& src) const { return cmp(src)>=0; } + int cmp (int& partial, const String& src, + size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; + bool operator < (const String& src) const { int p; return cmp(p, src)<0; } + bool operator > (const String& src) const { int p; return cmp(p, src)>0; } + bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; } + bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; } bool operator == (const String& src) const { if(size()!=src.size()) // can speed up in trivial case return false; - return cmp(src)==0; + int p; return cmp(p, src)==0; } - bool operator != (const String& src) const { return cmp(src)!=0; } + bool operator != (const String& src) const { int p; return cmp(p, src)!=0; } /** @param partial @@ -143,13 +142,14 @@ public: - 1: means @c this starts @c src - 2: means @src starts @this */ - int cmp(const char* src_ptr, int& partial, size_t src_size=0) const; + int cmp(int& partial, const char* src_ptr, size_t src_size=0, + size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; bool operator == (const char* src_ptr) const { size_t src_size=src_ptr?strlen(src_ptr):0; if(size() != src_size) return false; int partial; // unused - return cmp(src_ptr, partial, src_size)==0; + return cmp(partial, src_ptr, src_size)==0; } /** @@ -163,6 +163,26 @@ public: /// simple hash code of string. used by Hash uint hash_code() const; + /// extracts [start, finish) piece of string + String& piece(size_t start, size_t finish) const; + + /// @return position of substr in string, -1 means "not found" [String version] + int pos(const String& substr, + size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; + /// @return position of substr in string, -1 means "not found" [const char* version] + int pos(const char *substr, size_t substr_size, + size_t this_offset=0, Untaint_lang lang=UL_UNKNOWN) const; + + void split(Array& result, + size_t *pos_after_ref, + const char *delim, size_t delim_size, + Untaint_lang lang, int limit=-1) const; + void split(Array& result, + size_t *pos_after_ref, + const String& delim, + Untaint_lang lang, int limit=-1) const; + + #ifndef NO_STRING_ORIGIN /// origin of string. calculated by first row const Origin& origin() const; @@ -172,7 +192,7 @@ private: struct Chunk { // the number of rows in chunk - int count; + size_t count; union Row { // fragment struct { @@ -213,7 +233,6 @@ private: return append_here == link_row; } void expand(); - void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size); char *String::store_to(char *dest) const; private: //disabled