|
|
| version 1.151, 2003/10/21 05:11:00 | version 1.185, 2009/05/15 06:57:43 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class decl. | Parser: string class decl. |
| Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| #ifndef PA_STRING_H | #ifndef PA_STRING_H |
| #define PA_STRING_H | #define PA_STRING_H |
| static const char* IDENT_STRING_H="$Date$"; | static const char * const IDENT_STRING_H="$Date$"; |
| // includes | // includes |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_array.h" | #include "pa_array.h" |
| Line 20 extern "C" { // cord's author forgot to | Line 19 extern "C" { // cord's author forgot to |
| #include "cord.h" | #include "cord.h" |
| }; | }; |
| // defines | |
| // cache hash code in String::Body for faster hash access | |
| #define HASH_CODE_CACHING | |
| // cord extension | // cord extension |
| /* Returns true if x does contain */ | /* Returns true if x does contain */ |
| /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x). */ | /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x). */ |
| Line 35 class Dictionary; | Line 39 class Dictionary; |
| class Request_charsets; | class Request_charsets; |
| class String; | class String; |
| typedef Array<const String*> ArrayString; | typedef Array<const String*> ArrayString; |
| class VRegex; | |
| // 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 | /// this is result of pos functions which mean that substr were not found |
| #define STRING_NOT_FOUND ((size_t)-1) | #define STRING_NOT_FOUND ((size_t)-1) |
| Line 51 inline size_t get_length(T current) { | Line 61 inline size_t get_length(T current) { |
| - whether they are tainted or not, | - whether they are tainted or not, |
| and the lang which should be used to detaint them | and the lang which should be used to detaint them |
| */ | */ |
| class String: public PA_Object { | class String: public PA_Object { |
| public: | public: |
| Line 71 public: | Line 83 public: |
| enum Language { | enum Language { |
| L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty | 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 | // these two must go before others, there are checks for >L_AS_IS |
| L_CLEAN='0', ///< clean 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_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing |
| L_PASS_APPENDED='P', | L_PASS_APPENDED='P', |
| /**< | /**< |
| leave lang built into string being appended. | leave lang built into string being appended. |
| just a flag, that value not stored | 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]{...} | // untaint langs. assigned by ^untaint[lang]{...} |
| L_FILE_SPEC='F', ///< file specification | L_FILE_SPEC='F', ///< file specification |
| L_HTTP_HEADER='h', ///< text in HTTP response header | L_HTTP_HEADER='h', ///< text in HTTP response header |
| L_MAIL_HEADER='m', ///< text in mail header | L_MAIL_HEADER='m', ///< text in mail header |
| L_URI='U', ///< text in uri | L_URI='U', ///< text in uri |
| L_TABLE='L', ///< ^table:set body | L_SQL='Q', ///< ^table:sql body |
| L_SQL='Q', ///< ^table:sql body | L_JS='J', ///< JavaScript code |
| L_JS='J', ///< JavaScript code | |
| L_XML='X', ///< ^dom:set xml | 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 | |
| L_FILE_POST='f', ///temporary escaping zero-char | |
| // READ WARNING ABOVE BEFORE ADDING ANYTHING | // READ WARNING ABOVE BEFORE ADDING ANYTHING |
| L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization | L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization |
| }; | }; |
| enum Trim_kind { | |
| TRIM_BOTH, | |
| TRIM_START, | |
| TRIM_END | |
| }; | |
| union Languages { | union Languages { |
| struct { | struct { |
| #ifdef PA_LITTLE_ENDIAN | |
| Language lang:8; | |
| size_t is_not_just_lang:sizeof(CORD)*8-8; | |
| #elif defined(PA_BIG_ENDIAN) | |
| size_t is_not_just_lang:sizeof(CORD)*8-8; | |
| Language lang:8; | Language lang:8; |
| int is_not_just_lang:sizeof(CORD)*8-8; | #else |
| # error word endianness not determined for some obscure reason | |
| #endif | |
| } opt; | } opt; |
| CORD langs; | CORD langs; |
| Line 115 public: | Line 143 public: |
| :CORD_chars((char)opt.lang, alength); | :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<typename C> | template<typename C> |
| void append(C current, | void append(C current, |
| const CORD to_nonempty_target_langs) { | const CORD to_nonempty_target_langs) { |
| Line 135 public: | Line 163 public: |
| public: | public: |
| const char* v() const; | const char* v() const; |
| void dump() const; | |
| Languages(): langs(0) {} | Languages(): langs(0) {} |
| Languages(Language alang) { | Languages(Language alang) { |
| Line 150 public: | Line 179 public: |
| if(!opt.is_not_just_lang) | if(!opt.is_not_just_lang) |
| if(opt.lang) { | if(opt.lang) { |
| if(opt.lang==alang) // same length? ignoring | if(opt.lang==alang) // same language? ignoring |
| return; | return; |
| } else { | } else { |
| opt.lang=alang; // to uninitialized | opt.lang=alang; // to uninitialized |
| Line 160 public: | Line 189 public: |
| append(current, CORD_chars((char)alang, asize)); | append(current, CORD_chars((char)alang, asize)); |
| } | } |
| /// MUST be called exactly prior to modification of current [uses it's length] | |
| template<typename C> | template<typename C> |
| void append(C current, size_t appending_length, | void appendHelper(C current, Language alang, C asize_helper) { |
| const Languages src) { | assert(alang); |
| assert(appending_length); | |
| if(!opt.is_not_just_lang) | |
| if(opt.lang) { | |
| if(opt.lang==alang) // same language? ignoring | |
| return; | |
| } else { | |
| opt.lang=alang; // to uninitialized | |
| return; | |
| } | |
| append(current, CORD_chars((char)alang, asize_helper.length())); | |
| } | |
| template<typename C> | |
| void appendHelper(C current, C length_helper, const Languages src) { | |
| if(!langs) | if(!langs) |
| langs=src.langs; // to uninitialized | langs=src.langs; // to uninitialized |
| else if(!src.opt.is_not_just_lang) | else if(!src.opt.is_not_just_lang) |
| append(current, src.opt.lang, appending_length); // simplifying when simple source | appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source |
| else | else |
| append(current, src.make_langs(appending_length)); | append(current, src.make_langs(length_helper)); |
| } | } |
| /// MUST be called exactly prior to modification of current [uses it's length] | /// MUST be called exactly prior to modification of current [uses it's length] |
| template<typename C> | template<typename C> |
| void append(C current, | void append(C current, |
| Line 200 public: | Line 243 public: |
| if(opt.is_not_just_lang) | if(opt.is_not_just_lang) |
| return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0; | return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0; |
| else | else |
| return opt.lang<=alang; | return (unsigned)opt.lang<=(unsigned)alang; |
| } | } |
| /// @returns count of blocks | /// @returns count of blocks |
| Line 223 public: | Line 266 public: |
| callback(opt.lang, get_length(current), info); | callback(opt.lang, get_length(current), info); |
| } | } |
| bool invariant(size_t current_length) { | bool invariant(size_t current_length) const { |
| if(!langs) | if(!langs) |
| return current_length==0; | return current_length==0; |
| if(opt.is_not_just_lang) | if(opt.is_not_just_lang) |
| Line 236 public: | Line 279 public: |
| CORD body; | 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: | public: |
| const char* v() const; | 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(): body(CORD_EMPTY) {} |
| Body(CORD abody): body(abody) { | Body(CORD abody): body(abody) { |
| #endif | |
| assert(!body // no body | assert(!body // no body |
| || *body // ordinary string | || *body // ordinary string |
| || body[1]==1 // CONCAT_HDR | || body[1]==1 // CONCAT_HDR |
| Line 249 public: | Line 305 public: |
| || body[1]==6 // SUBSTR_HDR | || body[1]==6 // SUBSTR_HDR |
| ); | ); |
| } | } |
| /// WARNING: length is only HELPER length, str in ANY case should be zero-terminated | |
| Body(const char* str, size_t helper_length): body(CORD_EMPTY) { | |
| append_know_length(str, helper_length?helper_length:strlen(str)); | |
| } | |
| static Body Format(int value); | static Body Format(int value); |
| void clear() { body=CORD_EMPTY; } | void clear() { body=CORD_EMPTY; } |
| bool operator! () const { return is_empty(); } | 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); } | const char* cstr() const { return CORD_to_const_char_star(body); } |
| char* cstrm() const { return CORD_to_char_star(body); } | char* cstrm() const { return CORD_to_char_star(body); } |
| Line 296 public: | Line 350 public: |
| Body mid(size_t index, size_t length) const { return CORD_substr(body, index, length); } | Body 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 char* substr, size_t offset=0) const { return CORD_str(body, offset, substr); } |
| size_t pos(const Body substr, size_t offset=0) const { | size_t pos(const Body substr, size_t offset=0) const { |
| if(!substr.length()) | if(substr.is_empty()) |
| return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1] | return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1] |
| // CORD_str checks for bad offset [CORD_chr does not] | // CORD_str checks for bad offset [CORD_chr does not] |
| Line 310 public: | Line 364 public: |
| return CORD_chr(body, offset, c); | return CORD_chr(body, offset, c); |
| } | } |
| template<typename I> void for_each( | template<typename I> int for_each( |
| int (*f)(char c, I), | |
| I info) const { | |
| return CORD_iter(body, (CORD_iter_fn)f, (void*)info); | |
| } | |
| template<typename I> int for_each( | |
| int (*f1)(char c, I), | int (*f1)(char c, I), |
| int (*f2)(const char* s, I), | int (*f2)(const char* s, I), |
| I info) const { | 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); } | void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); } |
| Line 322 public: | Line 382 public: |
| /*Body normalize() const { | /*Body normalize() const { |
| return Body(CORD_balance(body)); | 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 { | struct C { |
| const char *str; | const char *str; |
| size_t length; | size_t length; |
| operator const char *() { return str; } | operator const char *() { return str; } |
| C(): str(0), length(0) {} | |
| C(const char *astr, size_t asize): str(astr), length(asize) {} | C(const char *astr, size_t asize): str(astr), length(asize) {} |
| }; | }; |
| Line 335 public: | Line 400 public: |
| char *str; | char *str; |
| size_t length; | size_t length; |
| //operator char *() { return str; } | //operator char *() { return str; } |
| Cm(): str(0), length(0) {} | |
| Cm(char *astr, size_t asize): str(astr), length(asize) {} | Cm(char *astr, size_t asize): str(astr), length(asize) {} |
| }; | }; |
| private: | private: |
| Languages langs; ///< string characters lang | |
| Body body; ///< all characters of string | Body body; ///< all characters of string |
| Languages langs; ///< string characters lang | |
| const char* v() const; | const char* v() const; |
| void dump() const; | |
| #define ASSERT_STRING_INVARIANT(string) \ | #define ASSERT_STRING_INVARIANT(string) \ |
| assert((string).langs.invariant((string).body.length())) | assert((string).langs.invariant((string).body.length())) |
| public: | public: |
| static const String Empty; | static const String Empty; |
| explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); | explicit String(){}; |
| explicit String(const C cstr, bool tainted=false); | explicit String(const char* cstr, Language alang=L_CLEAN){ |
| if(cstr && *cstr){ | |
| body=cstr; | |
| langs=alang; | |
| } | |
| } | |
| explicit String(const String::C cstr, Language alang=L_CLEAN){ | |
| if(cstr.length){ | |
| body=cstr.str; | |
| langs=alang; | |
| } | |
| } | |
| String(int value, char *format); | |
| String(Body abody, Language alang): body(abody), langs(alang) { | String(Body abody, Language alang): body(abody), langs(alang) { |
| assert(!body.is_empty()); | |
| ASSERT_STRING_INVARIANT(*this); | ASSERT_STRING_INVARIANT(*this); |
| } | } |
| String(const String& src): body(src.body), langs(src.langs) { | String(const String& src): body(src.body), langs(src.langs) { |
| Line 363 public: | Line 440 public: |
| } | } |
| /// for convinient hash lookup | /// for convinient hash lookup |
| #ifdef HASH_CODE_CACHING | |
| operator const Body&() const { return body; } | |
| #else | |
| operator const Body() const { return body; } | operator const Body() const { return body; } |
| #endif | |
| bool is_empty() const { return body.is_empty(); } | bool is_empty() const { return body.is_empty(); } |
| size_t length() const { return body.length(); } | size_t length() const { return body.length(); } |
| size_t length(Charset& charset) const; | |
| /// convert to CORD. if 'lang' known, forcing 'lang' to it | /// convert to CORD. if 'lang' known, forcing 'lang' to it |
| Body cstr_to_string_body(Language lang=L_AS_IS, | Body cstr_to_string_body(Language lang=L_AS_IS, |
| Line 429 public: | Line 511 public: |
| /// extracts [start, finish) piece of string | /// extracts [start, finish) piece of string |
| String& mid(size_t substr_begin, size_t substr_end) const; | 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 | ignore lang if it's L_UNSPECIFIED |
| Line 444 public: | Line 527 public: |
| size_t this_offset=0) const { | size_t this_offset=0) const { |
| return body.pos(c, this_offset); | return body.pos(c, this_offset); |
| } | } |
| size_t pos(Charset& charset, | |
| const String& substr, | |
| size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | |
| void split(ArrayString& result, | void split(ArrayString& result, |
| size_t& pos_after, | size_t& pos_after, |
| Line 465 public: | Line 551 public: |
| prematch/match/postmatch/1/2/3/... | prematch/match/postmatch/1/2/3/... |
| @endverbatim | @endverbatim |
| */ | */ |
| Table* match(Charset& source_charset, | Table* match(VRegex* vregex, |
| const String& regexp, | |
| const String* options, | |
| Row_action row_action, void *info, | Row_action row_action, void *info, |
| bool& just_matched) const; | int& matches_count) const; |
| enum Change_case_kind { | enum Change_case_kind { |
| CC_UPPER, | CC_UPPER, |
| CC_LOWER | CC_LOWER |
| Line 477 public: | Line 561 public: |
| String& change_case(Charset& source_charset, | String& change_case(Charset& source_charset, |
| Change_case_kind kind) const; | Change_case_kind kind) const; |
| const String& replace(const Dictionary& dict) const; | const String& replace(const Dictionary& dict) const; |
| double as_double() const; | const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0) const; |
| int as_int() 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; } | |
| const String& escape(Charset& source_charset) const; | |
| private: //disabled | private: //disabled |
| Line 491 inline size_t get_length<String::Body>(S | Line 578 inline size_t get_length<String::Body>(S |
| return body.length(); | return body.length(); |
| } | } |
| #ifndef HASH_CODE_CACHING | |
| /// simple hash code of string. used by Hash | /// simple hash code of string. used by Hash |
| inline uint hash_code(const String::Body self) { | 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 | /// now that we've declared specialization we can use it |