|
|
| version 1.151, 2003/10/21 05:11:00 | version 1.161, 2004/05/24 10:45:08 |
|---|---|
| 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-2004 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 |
| Line 36 class Request_charsets; | Line 36 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 94 public: | Line 99 public: |
| 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; | Language lang:8; |
| int is_not_just_lang:sizeof(CORD)*8-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; | } opt; |
| CORD langs; | CORD langs; |
| Line 106 public: | Line 124 public: |
| CORD make_langs(C current) const { | CORD make_langs(C current) const { |
| return opt.is_not_just_lang? | return opt.is_not_just_lang? |
| langs | langs |
| :CORD_chars((char)opt.lang, get_length(current)); | :CORD_chars_block((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 opt.is_not_just_lang? | return opt.is_not_just_lang? |
| CORD_substr(langs, aoffset, alength) | CORD_substr(langs, aoffset, alength) |
| :CORD_chars((char)opt.lang, alength); | :CORD_chars_block((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 127 public: | Line 145 public: |
| 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)opt.lang, current_size), // first piece [making from just 'lang'] | CORD_chars_block((char)opt.lang, current_size), // first piece [making from just 'lang'] |
| to_nonempty_target_langs); // new piece | to_nonempty_target_langs); // new piece |
| } | } |
| } | } |
| Line 157 public: | Line 175 public: |
| return; | return; |
| } | } |
| append(current, CORD_chars((char)alang, asize)); | append(current, CORD_chars_block((char)alang, asize)); |
| } | } |
| /// 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] |
| Line 310 public: | Line 328 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 346 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 364 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; |
| Line 355 public: | Line 385 public: |
| 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 477 public: | Line 506 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 |