|
|
| version 1.209, 2015/05/30 22:55:28 | version 1.236, 2025/05/25 17:44:27 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class decl. | Parser: string class decl. |
| Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| #ifndef PA_STRING_H | #ifndef PA_STRING_H |
| Line 16 | Line 16 |
| extern "C" { // cord's author forgot to do that | extern "C" { // cord's author forgot to do that |
| #define CORD_NO_IO | #define CORD_NO_IO |
| #include "cord.h" | #include "../lib/cord/include/cord.h" |
| #ifdef CORD_CAT_OPTIMIZATION | #ifdef CORD_CAT_OPTIMIZATION |
| #define CORD_cat(x, y) CORD_cat_optimized(x, y) | #define CORD_cat(x, y) CORD_cat_optimized(x, y) |
| Line 47 class SQL_Connection; | Line 47 class SQL_Connection; |
| class Dictionary; | class Dictionary; |
| class Request_charsets; | class Request_charsets; |
| class String; | class String; |
| typedef Array<const String*> ArrayString; | |
| class VRegex; | class VRegex; |
| #ifdef NDEBUG | |
| typedef Array<const String*> ArrayString; | |
| #else | |
| class ArrayString : public Array<const String*> { | |
| public: | |
| inline ArrayString(size_t initial=0) : Array(initial){ | |
| } | |
| inline Array& operator+=(element_type src) { | |
| assert(src != NULL); | |
| return Array::operator+=(src); | |
| } | |
| inline element_type get(size_t index) const { | |
| element_type result=Array::get(index); | |
| assert(result != NULL); | |
| return result; | |
| } | |
| }; | |
| #endif | |
| // generally useful | // generally useful |
| int pa_atoi(const char* str, const String* problem_source=0); | double pa_atod(const char* str, const String* problem_source); |
| double pa_atod(const char* str, const String* problem_source=0); | int pa_atoi(const char* str, int base=10, const String* problem_source=0); |
| unsigned int pa_atoui(const char *str, int base, const String* problem_source=0); | unsigned int pa_atoui(const char *str, int base=10, const String* problem_source=0); |
| uint64_t pa_atoul(const char *str, int base=10, 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) |
| /// CORD can't be empty string, thus checking it in assigment | |
| #define AS_CORD(v) ((v) && *(v) ? (CORD)(v):0) | |
| /** | /** |
| String which knows the lang of all it's langs. | String which knows the lang of all it's langs. |
| Line 69 unsigned int pa_atoui(const char *str, i | Line 92 unsigned int pa_atoui(const char *str, i |
| class String: public PA_Object { | class String: public PA_Object { |
| friend class StringSplitHelper; | |
| public: | public: |
| /** piece is tainted or not. the lang to use when detaint | /** piece is tainted or not. the lang to use when detaint |
| Line 85 public: | Line 109 public: |
| WARNING WARNING WARNING WARNING WARNING WARNING | WARNING WARNING WARNING WARNING WARNING WARNING |
| */ | */ |
| #if _MSC_VER >= 1900 | |
| /// required for VS2015+ to make sizeof(Languages::opt) == sizeof(CORD), will be 16 byte under x64 without it | |
| enum Language : size_t { | |
| #else | |
| 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 | #endif |
| 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_TAINTED='T', ///< tainted, untaint lang as assigned later |
| /**< | |
| leave lang built into string being appended. | |
| just a flag, that value not stored | |
| */ | |
| 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_SQL='Q', ///< ^table:sql body | L_SQL='Q', ///< ^table:sql body |
| L_JS='J', ///< JavaScript code | L_JS='J', ///< JavaScript code |
| L_XML='X', ///< ^xdoc:create xml | L_XML='X', ///< ^xdoc:create xml |
| L_HTML='H', ///< HTML code | L_HTML='H', ///< HTML code |
| L_REGEX='R', ///< RegExp | L_REGEX='R', ///< RegExp |
| L_JSON='S', ///< JSON code | L_JSON='S', ///< JSON code |
| L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode | L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode |
| L_PARSER_CODE='p', ///< ^process body | L_PARSER_CODE='p', ///< ^process body |
| // READ WARNING ABOVE BEFORE ADDING ANYTHING | // READ WARNING ABOVE BEFORE ADDING ANYTHING |
| Line 171 public: | Line 196 public: |
| public: | public: |
| const char* v() const; | const char* visualize() const; |
| void dump() const; | void dump() const; |
| Languages(): langs(0) {} | Languages(): langs(0) {} |
| Line 307 public: | Line 332 public: |
| } | } |
| }; | }; |
| struct C { | |
| const char *str; | |
| size_t length; | |
| C(): str(0), length(0) {} | |
| C(const char *astr, size_t asize): str(astr), length(asize) {} | |
| explicit C(Body abody): str(abody.cstr()), length(abody.length()) {} | |
| }; | |
| struct Cm { | |
| char *str; | |
| size_t length; | |
| Cm(): str(0), length(0) {} | |
| Cm(char *astr, size_t asize): str(astr), length(asize) {} | |
| explicit Cm(Body abody): str(abody.cstrm()), length(abody.length()) {} | |
| }; | |
| class Body { | class Body { |
| CORD body; | CORD body; |
| Line 315 public: | Line 356 public: |
| // cached hash code is not reseted on write operations as test shows | // 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 | // that string body does not change after it is stored as a hash key |
| mutable uint hash_code; | mutable uint hash_code; |
| #define INIT_HASH_CODE(c) ,hash_code(c) | |
| #define ZERO_HASH_CODE hash_code=0; | |
| #else | |
| #define INIT_HASH_CODE(c) | |
| #define ZERO_HASH_CODE | |
| #endif | #endif |
| #ifdef STRING_LENGTH_CACHING | #ifdef STRING_LENGTH_CACHING |
| // cached length is reseted on modification, used only for char*, not CORD | // cached length is reseted on modification, used only for char*, not CORD |
| mutable size_t string_length; | mutable size_t string_length; |
| #define INIT_LENGTH ,string_length(0) | #define INIT_LENGTH(l) ,string_length(l) |
| #define ZERO_LENGTH string_length=0; | #define ZERO_LENGTH string_length=0; |
| #else | #else |
| #define INIT_LENGTH | #define INIT_LENGTH(l) |
| #define ZERO_LENGTH | #define ZERO_LENGTH |
| #endif | #endif |
| public: | public: |
| const char* v() const; | |
| void dump() const; | void dump() const; |
| #ifdef HASH_CODE_CACHING | Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {} |
| Body(): body(CORD_EMPTY), hash_code(0) INIT_LENGTH {} | Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {} |
| Body(CORD abody, uint ahash_code): body(abody), hash_code(ahash_code) INIT_LENGTH {} | Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {} |
| Body(CORD abody): body(abody), hash_code(0) INIT_LENGTH { | explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {} |
| #else | explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) { |
| Body(): body(CORD_EMPTY) INIT_LENGTH {} | |
| Body(CORD abody): body(abody) INIT_LENGTH { | |
| #endif | |
| #ifdef CORD_CAT_OPTIMIZATION | #ifdef CORD_CAT_OPTIMIZATION |
| assert(!body // no body | assert(!body // no body |
| || *body // ordinary string | || *body // ordinary string |
| Line 358 public: | Line 400 public: |
| #endif | #endif |
| } | } |
| static Body Format(int value); | static Body uitoa(size_t aindex); |
| void clear() { ZERO_LENGTH body=CORD_EMPTY; } | void clear() { ZERO_LENGTH ZERO_HASH_CODE body=CORD_EMPTY; } |
| bool operator! () const { return is_empty(); } | bool operator! () const { return is_empty(); } |
| CORD get_cord() const { return body; } | inline CORD get_cord() const { return body; } |
| uint get_hash_code() const; | uint get_hash_code() const; |
| // never null | |
| const char* cstr() const { | const char* cstr() const { |
| #ifdef STRING_LENGTH_CACHING | #ifdef STRING_LENGTH_CACHING |
| string_length = length(); | string_length = length(); |
| if(string_length) | if(string_length){ |
| return const_cast<Body*>(this)->body=CORD_to_const_char_star(body, string_length); | const char *result=CORD_to_const_char_star(body, string_length); |
| const_cast<Body*>(this)->body=(CORD)result; | |
| return result; | |
| } | |
| #endif | #endif |
| return CORD_to_const_char_star(body, length()); | return CORD_to_const_char_star(body, length()); |
| } | } |
| // never null | |
| char* cstrm() const { return CORD_to_char_star(body, length()); } | char* cstrm() const { return CORD_to_char_star(body, length()); } |
| #ifdef STRING_LENGTH_CACHING | #ifdef STRING_LENGTH_CACHING |
| void set_length(size_t alength){ string_length = alength; } | void set_length(size_t alength){ string_length = alength; } |
| size_t length() const { return body ? CORD_IS_STRING(body) ? string_length ? string_length : (string_length=strlen(body)) : CORD_len(body) : 0; } | size_t length() const { return body ? CORD_IS_STRING(body) ? string_length ? string_length : (string_length=strlen((const char *)body)) : CORD_len(body) : 0; } |
| #else | #else |
| size_t length() const { return CORD_len(body); } | size_t length() const { return CORD_len(body); } |
| #endif | #endif |
| bool is_empty() const { return body==CORD_EMPTY; } | inline bool is_empty() const { return body==CORD_EMPTY; } |
| void append_know_length(const char *str, size_t known_length) { | void append_know_length(const char *str, size_t known_length) { |
| if(known_length){ | if(known_length){ |
| Line 393 public: | Line 440 public: |
| body = CORD_cat_char_star(body, str, known_length); | body = CORD_cat_char_star(body, str, known_length); |
| ZERO_LENGTH | ZERO_LENGTH |
| } else { | } else { |
| body=str; | body=(CORD)str; |
| #ifdef STRING_LENGTH_CACHING | #ifdef STRING_LENGTH_CACHING |
| string_length=known_length; | string_length=known_length; |
| #endif | #endif |
| Line 417 public: | Line 464 public: |
| bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; } | bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; } |
| bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; } | bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; } |
| bool operator != (const char *src) const { return CORD_cmp(body, src)!=0; } | bool operator != (const char *src) const { return CORD_cmp(body, AS_CORD(src))!=0; } |
| bool operator == (const char *src) const { return CORD_cmp(body, src)==0; } | bool operator == (const char *src) const { return CORD_cmp(body, AS_CORD(src))==0; } |
| int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const { | int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const { |
| return CORD_ncmp(body, x_begin, y.body, y_begin, size); | return CORD_ncmp(body, x_begin, y.body, y_begin, size); |
| } | } |
| char fetch(size_t index) const { return CORD_fetch(body, index); } | char fetch(size_t index) const { return CORD_fetch(body, index); } |
| Body mid(size_t aindex, size_t alength) const { return CORD_substr(body, aindex, alength, length()); } | Body mid(size_t aindex, size_t alength) const { return Body(CORD_substr(body, aindex, alength, length())); } |
| size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr, length()); } | size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, AS_CORD(substr), length()); } |
| size_t pos(const Body substr, size_t offset=0) const { | size_t pos(const Body substr, size_t offset=0) const { |
| if(substr.is_empty()) | 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] |
| Line 462 public: | Line 509 public: |
| size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const; | size_t* out_start=0, size_t* out_length=0, Charset* source_charset=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) {} | |
| }; | |
| struct Cm { | |
| 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: | private: |
| Body body; ///< all characters of string | Body body; ///< all characters of string |
| Languages langs; ///< string characters lang | Languages langs; ///< string characters lang |
| const char* v() const; | |
| void dump() 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())) |
| Line 493 public: | Line 523 public: |
| static const String Empty; | static const String Empty; |
| explicit String(){}; | explicit String(){}; |
| explicit String(const char* cstr, Language alang=L_CLEAN){ | explicit String(const char* cstr, Language alang=L_CLEAN) : body(cstr){ |
| if(cstr && *cstr){ | if(body.get_cord()){ |
| body=cstr; | |
| langs=alang; | langs=alang; |
| } | } |
| } | } |
| explicit String(const char* cstr, Language alang, size_t alength){ | explicit String(C ac, Language alang=L_CLEAN) : body(ac){ |
| if(cstr && *cstr){ | if(body.get_cord()){ |
| body=cstr; | |
| #ifdef STRING_LENGTH_CACHING | |
| body.set_length(alength); | |
| #endif | |
| langs=alang; | langs=alang; |
| } | } |
| } | } |
| String(int value, const char *format); | |
| String(Body abody, Language alang): body(abody), langs(alang) { | String(Body abody, Language alang): body(abody), langs(alang) { |
| 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) { |
| ASSERT_STRING_INVARIANT(*this); | ASSERT_STRING_INVARIANT(*this); |
| } | } |
| String(int value, const char *format); | |
| /// for convinient hash lookup | /// for convinient hash lookup |
| #ifdef HASH_CODE_CACHING | #ifdef HASH_CODE_CACHING |
| Line 533 public: | Line 557 public: |
| /// convert to CORD with tainting dirty to lang | /// convert to CORD with tainting dirty to lang |
| Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const; | Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const; |
| /// | /// from body |
| const char* cstr() const { | const char* cstr() const { return body.cstr(); } |
| return body.cstr(); | char* cstrm() const { return body.cstrm(); } |
| } | |
| /// | |
| char* cstrm() const { | |
| return body.cstrm(); | |
| } | |
| /// convert to constant C string forcing lang tainting | /// convert to constant C string forcing lang tainting |
| const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const { | const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const { |
| Line 568 public: | Line 587 public: |
| return langs.opt.lang; | return langs.opt.lang; |
| } | } |
| char* visualize_langs() const; | |
| /// puts pieces to buf | /// puts pieces to buf |
| Cm serialize(size_t prolog_size) const; | Cm serialize(size_t prolog_size) const; |
| /// appends pieces from buf to self | /// appends pieces from buf to self |
| Line 590 public: | Line 611 public: |
| return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0; | return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0; |
| } | } |
| String& append_to(String& dest) const; | |
| String& append_to(String& dest, Language lang, bool forced=false) const; | String& append_to(String& dest, Language lang, bool forced=false) const; |
| String& append(const String& src, Language lang, bool forced=false) { | String& append(const String& src, Language lang, bool forced=false) { |
| return src.append_to(*this, lang, forced); | return src.append_to(*this, lang, forced); |
| Line 602 public: | Line 624 public: |
| return *this; | return *this; |
| } | } |
| String& operator << (const String& src) { return append(src, L_PASS_APPENDED); } | String& operator << (const String& src) { return src.append_to(*this); } |
| String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); } | String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); } |
| String& operator << (const Body& src){ | String& operator << (const Body& src){ |
| langs.appendHelper(body, L_AS_IS, src); | langs.appendHelper(body, L_AS_IS, src); |
| Line 629 public: | Line 651 public: |
| 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; | String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const; |
| /** | /// return position of substr in string, -1 means "not found" [const char* version] |
| ignore lang if it's L_UNSPECIFIED | size_t pos(const char* substr, size_t this_offset=0) const { return body.pos(substr, this_offset); } |
| but when specified: look for substring that lies in ONE fragment in THAT lang | size_t pos(const Body substr, size_t this_offset=0) const { return body.pos(substr, this_offset); } |
| @return position of substr in string, -1 means "not found" [const char* version] | size_t pos(const String& substr, size_t this_offset=0) const { return body.pos(substr.body, this_offset); } |
| */ | size_t pos(char c, size_t this_offset=0) const { return body.pos(c, this_offset); } |
| size_t pos(const Body substr, | /// ignore lang if it's L_UNSPECIFIED, otherwise look for substring that lies in ONE fragment in THAT lang |
| size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | size_t pos(const Body substr, size_t this_offset, Language lang) const; |
| /// String version of @see pos(const char*, int, Language) | size_t pos(const String& substr, size_t this_offset, Language lang) const; |
| size_t pos(const String& substr, | size_t pos(Charset& charset, const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const; |
| size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | |
| size_t pos(char c, | |
| size_t this_offset=0) const { | |
| return body.pos(c, this_offset); | |
| } | |
| size_t pos(Charset& charset, | |
| const String& substr, | |
| size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | |
| size_t strrpbrk(const char* chars, size_t left=0) const { | size_t strrpbrk(const char* chars, size_t left=0) const { |
| return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND; | return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND; |
| Line 661 public: | Line 675 public: |
| return body.rskipchars(chars, left, right); | return body.rskipchars(chars, left, right); |
| } | } |
| void split(ArrayString& result, | void split(ArrayString& result, size_t pos_after, const char* delim, Language lang=L_UNSPECIFIED) const; |
| size_t& pos_after, | void split(ArrayString& result, size_t pos_after, const String& delim, Language lang=L_UNSPECIFIED) const; |
| const char* delim, | |
| Language lang=L_UNSPECIFIED, int limit=-1) const; | typedef void (*Row_action)(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info); |
| void split(ArrayString& result, | |
| size_t& pos_after, | |
| const String& delim, | |
| Language lang=L_UNSPECIFIED, int limit=-1) const; | |
| typedef void (*Row_action)(Table& table, ArrayString* row, | |
| int prestart, int prefinish, | |
| int poststart, int postfinish, | |
| void *info); | |
| /** | /** |
| @return table of found items, if any. | @return table of found items, if any. |
| table format is defined and fixed[can be used by others]: | table format is defined and fixed[can be used by others]: |
| Line 681 public: | Line 687 public: |
| prematch/match/postmatch/1/2/3/... | prematch/match/postmatch/1/2/3/... |
| @endverbatim | @endverbatim |
| */ | */ |
| Table* match(VRegex* vregex, | Table* match(VRegex* vregex, Row_action row_action, void *info, int& matches_count) const; |
| Row_action row_action, void *info, | |
| int& matches_count) const; | |
| enum Change_case_kind { | enum Change_case_kind { |
| CC_UPPER, | CC_UPPER, |
| CC_LOWER | CC_LOWER |
| }; | }; |
| 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; |
| const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const; | const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const; |
| double as_double() const { return pa_atod(cstr(), this); } | double as_double() const { return pa_atod(cstr(), this); } |
| int as_int() const { return pa_atoi(cstr(), this); } | int as_int() const { return pa_atoi(cstr(), 0, this); } |
| bool as_bool() const { return as_int()!=0; } | bool as_bool() const { return as_int()!=0; } |
| const String& escape(Charset& source_charset) const; | const String& escape(Charset& source_charset) const; |