--- parser3/src/include/pa_string.h 2003/10/21 05:11:00 1.151 +++ parser3/src/include/pa_string.h 2008/07/17 09:11:14 1.172 @@ -1,17 +1,16 @@ /** @file Parser: string class decl. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_STRING_H #define PA_STRING_H -static const char* IDENT_STRING_H="$Date: 2003/10/21 05:11:00 $"; +static const char * const IDENT_STRING_H="$Date: 2008/07/17 09:11:14 $"; // includes - #include "pa_types.h" #include "pa_array.h" @@ -20,6 +19,8 @@ extern "C" { // cord's author forgot to #include "cord.h" }; +// defines + // 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). */ @@ -36,6 +37,11 @@ class Request_charsets; class String; typedef Array ArrayString; +// generally useful + +int pa_atoi(const char* str, const String* problem_source=0); +double pa_atod(const char* str, const String* problem_source=0); + /// this is result of pos functions which mean that substr were not found #define STRING_NOT_FOUND ((size_t)-1) @@ -71,34 +77,48 @@ public: enum Language { 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='0', ///< clean WARNING: read above warning before changing - L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing + L_CLEAN='0', ///< clean WARNING: read above warning before changing + L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing L_PASS_APPENDED='P', /**< leave lang built into string being appended. just a flag, that value not stored */ - L_TAINTED='T', ///< tainted, untaint lang as assigned later + L_TAINTED='T', ///< tainted, untaint lang as assigned later // untaint langs. assigned by ^untaint[lang]{...} - L_FILE_SPEC='F', ///< file specification - L_HTTP_HEADER='h', ///< text in HTTP response header - L_MAIL_HEADER='m', ///< text in mail header - L_URI='U', ///< text in uri - L_TABLE='L', ///< ^table:set body - L_SQL='Q', ///< ^table:sql body - L_JS='J', ///< JavaScript code + L_FILE_SPEC='F', ///< file specification + L_HTTP_HEADER='h', ///< text in HTTP response header + L_MAIL_HEADER='m', ///< text in mail header + L_URI='U', ///< text in uri + L_SQL='Q', ///< ^table:sql body + L_JS='J', ///< JavaScript code L_XML='X', ///< ^dom:set xml - L_HTML='H', ///< HTML code (for editing) + L_HTML='H', ///< HTML code + L_REGEX='R', ///< RegEx expression + L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode // READ WARNING ABOVE BEFORE ADDING ANYTHING L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization }; + enum Trim_kind { + TRIM_BOTH, + TRIM_START, + TRIM_END + }; + union Languages { struct { +#ifdef PA_LITTLE_ENDIAN Language lang:8; int is_not_just_lang:sizeof(CORD)*8-8; +#elif defined(PA_BIG_ENDIAN) + int is_not_just_lang:sizeof(CORD)*8-8; + Language lang:8; +#else +# error word endianness not determined for some obscure reason +#endif } opt; CORD langs; @@ -115,7 +135,7 @@ public: :CORD_chars((char)opt.lang, alength); } - /// appending when 'langs' already contain something [simple cases hanled elsewhere] + /// appending when 'langs' already contain something [simple cases handled elsewhere] template void append(C current, const CORD to_nonempty_target_langs) { @@ -135,6 +155,7 @@ public: public: const char* v() const; + void dump() const; Languages(): langs(0) {} Languages(Language alang) { @@ -200,7 +221,7 @@ public: if(opt.is_not_just_lang) return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0; else - return opt.lang<=alang; + return (unsigned)opt.lang<=(unsigned)alang; } /// @returns count of blocks @@ -223,7 +244,7 @@ public: callback(opt.lang, get_length(current), info); } - bool invariant(size_t current_length) { + bool invariant(size_t current_length) const { if(!langs) return current_length==0; if(opt.is_not_just_lang) @@ -239,6 +260,7 @@ public: public: const char* v() const; + void dump() const; Body(): body(CORD_EMPTY) {} Body(CORD abody): body(abody) { @@ -310,11 +332,17 @@ public: return CORD_chr(body, offset, c); } - template void for_each( + template int for_each( + int (*f)(char c, I), + I info) const { + return CORD_iter(body, (CORD_iter_fn)f, (void*)info); + } + + template int for_each( int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const { - CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info); + return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info); } void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); } @@ -322,12 +350,17 @@ public: /*Body normalize() const { return Body(CORD_balance(body)); }*/ + + /// @returns this or 0 or mid. if returns this or 0 out_* are not filled + Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, + size_t* out_start=0, size_t* out_length=0) const; }; struct C { const char *str; size_t length; operator const char *() { return str; } + C(): str(0), length(0) {} C(const char *astr, size_t asize): str(astr), length(asize) {} }; @@ -335,18 +368,19 @@ public: char *str; size_t length; //operator char *() { return str; } + Cm(): str(0), length(0) {} Cm(char *astr, size_t asize): str(astr), length(asize) {} }; private: - Languages langs; ///< string characters lang Body body; ///< all characters of string + Languages langs; ///< string characters lang const char* v() const; - -#define ASSERT_STRING_INVARIANT(string) \ - assert((string).langs.invariant((string).body.length())) + void dump() const; + #define ASSERT_STRING_INVARIANT(string) \ + assert((string).langs.invariant((string).body.length())) public: @@ -355,7 +389,6 @@ public: explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); explicit String(const C cstr, bool tainted=false); String(Body 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) { @@ -367,6 +400,7 @@ public: bool is_empty() const { return body.is_empty(); } size_t length() const { return body.length(); } + size_t length(Charset& charset) const; /// convert to CORD. if 'lang' known, forcing 'lang' to it Body cstr_to_string_body(Language lang=L_AS_IS, @@ -429,6 +463,7 @@ public: /// extracts [start, finish) piece of string String& mid(size_t substr_begin, size_t substr_end) const; + String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const; /** ignore lang if it's L_UNSPECIFIED @@ -444,6 +479,9 @@ public: size_t this_offset=0) const { return body.pos(c, this_offset); } + size_t String::pos(Charset& charset, + const String& substr, + size_t this_offset=0, Language lang=L_UNSPECIFIED) const; void split(ArrayString& result, size_t& pos_after, @@ -469,7 +507,7 @@ public: const String& regexp, const String* options, Row_action row_action, void *info, - bool& just_matched) const; + int& matches_count) const; enum Change_case_kind { CC_UPPER, CC_LOWER @@ -477,8 +515,10 @@ public: String& change_case(Charset& source_charset, Change_case_kind kind) const; const String& replace(const Dictionary& dict) const; - double as_double() const; - int as_int() const; + const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0) const; + double as_double() const { return pa_atod(cstr(), this); } + int as_int() const { return pa_atoi(cstr(), this); } + bool as_bool() const { return as_int()!=0; } private: //disabled