|
|
| version 1.221, 2016/11/28 22:42:57 | version 1.243, 2026/10/01 11:48:37 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class decl. | Parser: string class decl. |
| Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://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 12 | Line 12 |
| // includes | // includes |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_int.h" | |
| #include "pa_array.h" | #include "pa_array.h" |
| 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 48 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; |
| // generally useful | #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 | |
| int pa_atoi(const char* str, const String* problem_source=0); | // generally useful |
| double pa_atod(const char* str, const String* problem_source=0); | |
| unsigned int pa_atoui(const char *str, int base, const String* problem_source=0); | |
| unsigned long long int pa_atoul(const char *str, int base, 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 90 public: | Line 105 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_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_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 192 public: |
| public: | public: |
| const char* v() const; | const char* visualize() const; |
| void dump() const; | void dump() const; |
| Languages(): langs(0) {} | Languages(): langs(0) {} |
| Line 350 public: | Line 371 public: |
| public: | public: |
| const char* v() const; | |
| void dump() const; | void dump() const; |
| Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {} | Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {} |
| Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {} | explicit Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {} |
| Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {} | explicit Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {} |
| explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {} | explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {} |
| explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) { | explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) { |
| #ifdef CORD_CAT_OPTIMIZATION | #ifdef CORD_CAT_OPTIMIZATION |
| Line 376 public: | Line 396 public: |
| #endif | #endif |
| } | } |
| static Body uitoa(size_t aindex); | |
| static Body Format(int value); | |
| void clear() { ZERO_LENGTH ZERO_HASH_CODE body=CORD_EMPTY; } | void clear() { ZERO_LENGTH ZERO_HASH_CODE body=CORD_EMPTY; } |
| Line 464 public: | Line 483 public: |
| return CORD_chr(body, offset, c); | return CORD_chr(body, offset, c); |
| } | } |
| size_t strrchr(char c, size_t right) const { | |
| if(right>=length()) // CORD_rchr does not check that [and ABORT's in that case] | |
| return STRING_NOT_FOUND; | |
| return CORD_rchr(body, right, c); | |
| } | |
| size_t strrpbrk(const char* chars, size_t left, size_t right) const; | size_t strrpbrk(const char* chars, size_t left, size_t right) const; |
| Line 491 private: | Line 516 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 565 public: | Line 589 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 627 public: | Line 653 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, size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | /// ignore lang if it's L_UNSPECIFIED, otherwise look for substring that lies in ONE fragment in THAT lang |
| /// String version of @see pos(const char*, int, Language) | size_t pos(const Body substr, size_t this_offset, Language lang) const; |
| size_t pos(const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const; | size_t pos(const String& substr, size_t this_offset, Language lang) 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 pos(Charset& charset, const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const; |
| /// true if the characters from offset to offset+length are of lang or a cleaner one | |
| bool check_lang(Language lang, size_t offset, size_t length) const { return langs.check_lang(lang, offset, length); } | |
| 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 646 public: | Line 671 public: |
| size_t strrpbrk(const char* chars, size_t left, size_t right) const { | size_t strrpbrk(const char* chars, size_t left, size_t right) const { |
| return body.strrpbrk(chars, left, right); | return body.strrpbrk(chars, left, right); |
| } | } |
| size_t strrchr(char c) const { | |
| return (length()) ? body.strrchr(c, length()-1) : STRING_NOT_FOUND; | |
| } | |
| size_t strrchr(char c, size_t right) const { | |
| return body.strrchr(c, right); | |
| } | |
| size_t rskipchars(const char* chars, size_t left=0) const { | size_t rskipchars(const char* chars, size_t left=0) const { |
| return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND; | return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND; |
| Line 661 public: | Line 692 public: |
| /** | /** |
| @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]: |
| @verbatim | @verbatim |
| prematch/match/postmatch/1/2/3/... | prematch/match/postmatch/1/2/3/... |
| @endverbatim | @endverbatim |
| Line 672 public: | Line 703 public: |
| CC_UPPER, | CC_UPPER, |
| CC_LOWER | CC_LOWER |
| }; | }; |
| /// keeps the chars languages | |
| String& change_case(Charset& source_charset, Change_case_kind kind) const; | String& change_case(Charset& source_charset, Change_case_kind kind) const; |
| /// keeps the chars languages | |
| String& change_char(char from, char to) 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; } | pa_wint as_wint() const { return pa_atowi(cstr(), 0, this); } |
| bool as_bool() const { return as_wint()!=0; } | |
| const String& escape(Charset& source_charset) const; | const String& escape(Charset& source_charset) const; |
| private: //disabled | private: //disabled |