|
|
| version 1.146, 2003/09/25 09:15:02 | version 1.166, 2005/11/22 15:09:10 |
|---|---|
| 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-2005 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 | |
| // 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). */ |
| int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c); | int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c); |
| size_t CORD_block_count(CORD x); | |
| // forwards | // forwards |
| Line 35 class Request_charsets; | Line 37 class Request_charsets; |
| class String; | class String; |
| typedef Array<const String*> ArrayString; | typedef Array<const String*> 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 | /// 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 42 template<typename T> | Line 49 template<typename T> |
| inline size_t get_length(T current) { | inline size_t get_length(T current) { |
| return current; | return current; |
| } | } |
| /** | /** |
| String which knows the lang of all it's langs. | String which knows the lang of all it's langs. |
| Line 54 public: | Line 62 public: |
| /** piece is tainted or not. the lang to use when detaint | /** piece is tainted or not. the lang to use when detaint |
| remember to change String_Untaint_lang_name @ untaint.C along | remember to change String_Untaint_lang_name @ untaint.C along |
| WARNING WARNING WARNING WARNING WARNING WARNING | |
| pos function compares(<=) languages, that is used in searching | |
| for table column separator being L_CLEAN or L_AS_IS. | |
| they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check]. | |
| letters assigned for debugging, but it's important for no language-letter | |
| come before L_AS_IS other then L_CLEAN | |
| WARNING WARNING WARNING WARNING WARNING WARNING | |
| */ | */ |
| 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='1', ///< clean | L_CLEAN='0', ///< clean WARNING: read above warning before changing |
| L_AS_IS, ///< leave all characters intact | L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing |
| L_PASS_APPENDED, | 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, ///< 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, ///< file specification | L_FILE_SPEC='F', ///< file specification |
| L_HTTP_HEADER, ///< text in HTTP response header | L_HTTP_HEADER='h', ///< text in HTTP response header |
| L_MAIL_HEADER, ///< text in mail header | L_MAIL_HEADER='m', ///< text in mail header |
| L_URI, ///< text in uri | L_URI='U', ///< text in uri |
| L_TABLE, ///< ^table:set body | L_SQL='Q', ///< ^table:sql body |
| L_SQL, ///< ^table:sql body | L_JS='J', ///< JavaScript code |
| L_JS, ///< JavaScript code | L_XML='X', ///< ^dom:set xml |
| L_XML, ///< ^dom:set xml | L_HTML='H', ///< HTML code |
| L_HTML, ///< HTML code (for editing) | L_REGEX='R', ///< RegEx expression |
| // READ WARNING ABOVE BEFORE ADDING ANYTHING | |
| L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization | L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization |
| }; | }; |
| class Languages { | enum Trim_kind { |
| TRIM_BOTH, | |
| TRIM_START, | |
| TRIM_END | |
| }; | |
| union { | union Languages { |
| struct { | |
| Language lang:8; | struct { |
| int is_not_just_lang:sizeof(CORD)*8-8; | #ifdef PA_LITTLE_ENDIAN |
| }; | Language lang:8; |
| CORD langs; | 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; | |
| template<typename C> | template<typename C> |
| CORD make_langs(C current) const { | CORD make_langs(C current) const { |
| return is_not_just_lang? | return opt.is_not_just_lang? |
| langs | langs |
| :CORD_chars((char)lang, get_length(current)); | :CORD_chars((char)opt.lang, get_length(current)); |
| } | } |
| CORD make_langs(size_t aoffset, size_t alength) const { | CORD make_langs(size_t aoffset, size_t alength) const { |
| return is_not_just_lang? | return opt.is_not_just_lang? |
| CORD_substr(langs, aoffset, alength) | CORD_substr(langs, aoffset, alength) |
| :CORD_chars((char)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) { |
| assert(langs); | assert(langs); |
| if(is_not_just_lang) | if(opt.is_not_just_lang) |
| langs=CORD_cat(langs, to_nonempty_target_langs); | langs=CORD_cat(langs, to_nonempty_target_langs); |
| else { // we were "just lang" | else { // we were "just lang" |
| size_t current_size=get_length(current); | size_t current_size=get_length(current); |
| assert(current_size); | assert(current_size); |
| langs=CORD_cat( | langs=CORD_cat( |
| CORD_chars((char)lang, current_size), // first piece [making from just 'lang'] | CORD_chars((char)opt.lang, current_size), // first piece [making from just 'lang'] |
| to_nonempty_target_langs); // new piece | to_nonempty_target_langs); // new piece |
| } | } |
| } | } |
| Line 123 public: | Line 154 public: |
| public: | public: |
| const char* v() const; | const char* v() const; |
| void dump() const; | |
| Languages(): langs(0) {} | Languages(): langs(0) {} |
| Languages(Language alang): lang(alang), is_not_just_lang(0) {} | Languages(Language alang) { |
| opt.lang=alang; | |
| opt.is_not_just_lang=0; | |
| } | |
| /// 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> |
| Line 133 public: | Line 168 public: |
| assert(alang); | assert(alang); |
| assert(asize); | assert(asize); |
| if(!is_not_just_lang) | if(!opt.is_not_just_lang) |
| if(lang) { | if(opt.lang) { |
| if(lang==alang) // same length? ignoring | if(opt.lang==alang) // same length? ignoring |
| return; | return; |
| } else { | } else { |
| lang=alang; // to uninitialized | opt.lang=alang; // to uninitialized |
| return; | return; |
| } | } |
| Line 153 public: | Line 188 public: |
| if(!langs) | if(!langs) |
| langs=src.langs; // to uninitialized | langs=src.langs; // to uninitialized |
| else if(!src.is_not_just_lang) | else if(!src.opt.is_not_just_lang) |
| append(current, src.lang, appending_length); // simplifying when simple source | append(current, src.opt.lang, appending_length); // simplifying when simple source |
| else | else |
| append(current, src.make_langs(appending_length)); | append(current, src.make_langs(appending_length)); |
| } | } |
| Line 166 public: | Line 201 public: |
| assert(alength); | assert(alength); |
| if(!langs) // to uninitialized? | if(!langs) // to uninitialized? |
| if(src.is_not_just_lang) | if(src.opt.is_not_just_lang) |
| langs=CORD_substr(src.langs, aoffset, alength); // to uninitialized complex | langs=CORD_substr(src.langs, aoffset, alength); // to uninitialized complex |
| else | else |
| lang=src.lang; // to uninitialized simple | opt.lang=src.opt.lang; // to uninitialized simple |
| else | else |
| if(!is_not_just_lang && !src.is_not_just_lang && lang==src.lang) // both simple & of same language? | if(!opt.is_not_just_lang && !src.opt.is_not_just_lang && opt.lang==src.opt.lang) // both simple & of same language? |
| return; // ignoring | return; // ignoring |
| else | else |
| append(current, src.make_langs(aoffset, alength)); | append(current, src.make_langs(aoffset, alength)); |
| Line 182 public: | Line 217 public: |
| if(alang==L_UNSPECIFIED) // ignore lang? | if(alang==L_UNSPECIFIED) // ignore lang? |
| return true; | return true; |
| if(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 lang<=alang; | return opt.lang<=alang; |
| } | } |
| /// @returns count of blocks | |
| /// @todo currently there can be adjucent blocks of same language. someday merge them | |
| size_t count() const { | |
| return opt.is_not_just_lang? | |
| CORD_block_count(langs) | |
| : opt.lang? | |
| 1 | |
| : 0; | |
| }; | |
| template<typename C, typename I> | template<typename C, typename I> |
| void for_each(C current, | void for_each(C current, |
| int callback(char, size_t, I), I info) const { | int callback(char, size_t, I), I info) const { |
| if(is_not_just_lang) | if(opt.is_not_just_lang) |
| CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info); | CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info); |
| else | else |
| callback(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(is_not_just_lang) | if(opt.is_not_just_lang) |
| return CORD_len(langs)==current_length; | return CORD_len(langs)==current_length; |
| return true; // uncheckable, actually | return true; // uncheckable, actually |
| } | } |
| Line 214 public: | Line 259 public: |
| public: | public: |
| const char* v() const; | const char* v() const; |
| void dump() const; | |
| Body(): body(CORD_EMPTY) {} | Body(): body(CORD_EMPTY) {} |
| Body(CORD abody): body(abody) { | Body(CORD abody): body(abody) { |
| Line 273 public: | Line 319 public: |
| 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.length()) |
| 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] | |
| return CORD_str(body, offset, substr.body); | return CORD_str(body, offset, substr.body); |
| } | } |
| size_t pos(char c, | size_t pos(char c, |
| size_t offset=0) const { | size_t offset=0) const { |
| if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case] | |
| return STRING_NOT_FOUND; | |
| return CORD_chr(body, offset, c); | return CORD_chr(body, offset, c); |
| } | } |
| /* template<typename I> void for_each(int (*callback)(const char* s, I), I info) const { | template<typename I> int for_each( |
| CORD_iter5(body, 0, 0, (CORD_batched_iter_fn)callback, info); | 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 (*f2)(const char* s, I), | |
| I info) const { | |
| 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); } |
| /*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 303 public: | Line 367 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; | |
| explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); | explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false); |
| explicit String(const C cstr, bool tainted=false); | explicit String(const C cstr, bool tainted=false); |
| 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 379 public: | Line 445 public: |
| } | } |
| String& operator << (const String& src) { return append(src, L_PASS_APPENDED); } | String& operator << (const String& src) { return append(src, L_PASS_APPENDED); } |
| 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.append(body, L_AS_IS, src.length()); | |
| body<<src; | |
| return *this; | |
| } | |
| /// extracts first char of a string, if any | /// extracts first char of a string, if any |
| char first_char() const { | char first_char() const { |
| Line 447 public: | Line 509 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); } | |
| private: //disabled | private: //disabled |
| Line 466 inline uint hash_code(const String::Body | Line 529 inline uint hash_code(const String::Body |
| return self.hash_code(); | return self.hash_code(); |
| } | } |
| /// now that we've declared specialization we can use it | |
| inline String& String::operator << (const String::Body src) { | |
| langs.append(body, L_AS_IS, src.length()); | |
| body<<src; | |
| return *this; | |
| } | |
| #endif | #endif |