|
|
| version 1.175, 2009/01/25 01:59:42 | version 1.187, 2009/06/20 00:45:46 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: string class decl. | Parser: string class decl. |
| Copyright (c) 2001-2005 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) |
| */ | */ |
| Line 17 static const char * const IDENT_STRING_H | Line 17 static const char * const IDENT_STRING_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 "cord.h" |
| #ifdef CORD_CAT_OPTIMIZATION | |
| #define CORD_cat(x, y) CORD_cat_optimized(x, y) | |
| #define CORD_cat_char_star(x, y, leny) CORD_cat_char_star_optimized(x, y, leny) | |
| #endif | |
| }; | }; |
| // defines | // defines |
| // cache hash code in String::Body for faster hash access | |
| #define HASH_CODE_CACHING | |
| // cache String::Body.length() for char* strings only, CORDs have own length | |
| #define STRING_LENGTH_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 36 class Dictionary; | Line 48 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 | // generally useful |
| Line 45 double pa_atod(const char* str, const St | Line 58 double pa_atod(const char* str, const St |
| /// 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) |
| template<typename T> | |
| inline size_t get_length(T current) { | |
| return current; | |
| } | |
| /** | /** |
| String which knows the lang of all it's langs. | String which knows the lang of all it's langs. |
| Line 57 inline size_t get_length(T current) { | Line 65 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 108 public: | Line 118 public: |
| TRIM_END | TRIM_END |
| }; | }; |
| class Body; | |
| union Languages { | union Languages { |
| struct { | struct { |
| #ifdef PA_LITTLE_ENDIAN | #ifdef PA_LITTLE_ENDIAN |
| Language lang:8; | Language lang:8; |
| int is_not_just_lang:sizeof(CORD)*8-8; | size_t is_not_just_lang:sizeof(CORD)*8-8; |
| #elif defined(PA_BIG_ENDIAN) | #elif defined(PA_BIG_ENDIAN) |
| int is_not_just_lang:sizeof(CORD)*8-8; | size_t is_not_just_lang:sizeof(CORD)*8-8; |
| Language lang:8; | Language lang:8; |
| #else | #else |
| # error word endianness not determined for some obscure reason | # error word endianness not determined for some obscure reason |
| Line 123 public: | Line 135 public: |
| } opt; | } opt; |
| CORD langs; | CORD langs; |
| template<typename C> | CORD make_langs(const Body& current) const { |
| CORD make_langs(C current) const { | return opt.is_not_just_lang?langs:CORD_chars((char)opt.lang, current.length()); |
| return opt.is_not_just_lang? | |
| langs | |
| :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 { |
| Line 137 public: | Line 146 public: |
| } | } |
| /// appending when 'langs' already contain something [simple cases handled elsewhere] | /// appending when 'langs' already contain something [simple cases handled elsewhere] |
| template<typename C> | void append(size_t current, const CORD to_nonempty_target_langs) { |
| void append(C current, | |
| const CORD to_nonempty_target_langs) { | |
| assert(langs); | assert(langs); |
| if(opt.is_not_just_lang) | |
| langs=CORD_cat(langs, to_nonempty_target_langs); | |
| else { | |
| assert(current); | |
| langs=CORD_cat(CORD_chars((char)opt.lang, current), to_nonempty_target_langs); | |
| } | |
| } | |
| void append(const Body& current, const CORD to_nonempty_target_langs) { | |
| assert(langs); | |
| if(opt.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 { |
| size_t current_size=get_length(current); | size_t current_size=current.length(); |
| assert(current_size); | assert(current_size); |
| langs=CORD_cat( | langs=CORD_cat(CORD_chars((char)opt.lang, current_size), to_nonempty_target_langs); |
| CORD_chars((char)opt.lang, current_size), // first piece [making from just 'lang'] | |
| to_nonempty_target_langs); // new piece | |
| } | } |
| } | } |
| Line 165 public: | Line 179 public: |
| } | } |
| /// 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> | void append(size_t current, Language alang, size_t length) { |
| void append(C current, Language alang, size_t asize) { | |
| assert(alang); | assert(alang); |
| assert(asize); | assert(length); |
| 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 |
| return; | return; |
| } | } |
| append(current, CORD_chars((char)alang, asize)); | append(current, CORD_chars((char)alang, length)); |
| } | } |
| /// MUST be called exactly prior to modification of current [uses it's length] | void append(const Body ¤t, Language alang, size_t length) { |
| template<typename C> | assert(alang); |
| void append(C current, size_t appending_length, | assert(length); |
| const Languages src) { | |
| assert(appending_length); | |
| if(!langs) | 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, length)); | |
| } | |
| void appendHelper(const Body& current, Language alang, const Body &length_helper) { | |
| assert(alang); | |
| 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, length_helper.length())); | |
| } | |
| void appendHelper(const Body& current, const Languages &src, const Body& length_helper) { | |
| if(!langs){ | |
| langs=src.langs; // to uninitialized | langs=src.langs; // to uninitialized |
| #ifdef CORD_CAT_OPTIMIZATION | |
| if(opt.is_not_just_lang && !CORD_IS_STRING(langs)) | |
| CORD_concatenation_protect(langs); | |
| #endif | |
| } | |
| 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> | void append(const Body& current, const Languages src, size_t aoffset, size_t alength) { |
| void append(C current, | |
| const Languages src, size_t aoffset, size_t alength) { | |
| assert(alength); | assert(alength); |
| if(!langs) // to uninitialized? | if(!langs) // to uninitialized? |
| Line 235 public: | Line 277 public: |
| : 0; | : 0; |
| }; | }; |
| template<typename C, typename I> | template<typename I> |
| void for_each(C current, | void for_each(size_t current, int callback(char, size_t, I), I info) const { |
| int callback(char, size_t, I), I info) const { | |
| if(opt.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(opt.lang, get_length(current), info); | callback(opt.lang, current, info); |
| } | |
| template<typename I> | |
| void for_each(const Body& current, int callback(char, size_t, I), I info) const { | |
| if(opt.is_not_just_lang) | |
| CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info); | |
| else | |
| callback(opt.lang, current.length(), info); | |
| } | } |
| bool invariant(size_t current_length) const { | bool invariant(size_t current_length) const { |
| Line 258 public: | Line 306 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 | |
| #ifdef STRING_LENGTH_CACHING | |
| // cached length is reseted on modification, used only for char*, not CORD | |
| mutable size_t string_length; | |
| #define INIT_LENGTH ,string_length(0) | |
| #define ZERO_LENGTH string_length=0; | |
| #else | |
| #define INIT_LENGTH | |
| #define ZERO_LENGTH | |
| #endif | |
| public: | public: |
| const char* v() const; | const char* v() const; |
| void dump() const; | void dump() const; |
| Body(): body(CORD_EMPTY) {} | #ifdef HASH_CODE_CACHING |
| Body(CORD abody): body(abody) { | Body(): body(CORD_EMPTY), hash_code(0) INIT_LENGTH {} |
| Body(CORD abody, uint ahash_code): body(abody), hash_code(ahash_code) INIT_LENGTH {} | |
| Body(CORD abody): body(abody), hash_code(0) INIT_LENGTH { | |
| #else | |
| Body(): body(CORD_EMPTY) INIT_LENGTH {} | |
| Body(CORD abody): body(abody) INIT_LENGTH { | |
| #endif | |
| #ifdef CORD_CAT_OPTIMIZATION | |
| assert(!body // no body | assert(!body // no body |
| || *body // ordinary string | || *body // ordinary string |
| || body[1]==1 // CONCAT_HDR | || body[1]==1 // CONCAT_HDR |
| || body[1]==3 // CONCAT_HDR_READ_ONLY | |
| || body[1]==4 // FN_HDR | || body[1]==4 // FN_HDR |
| || body[1]==6 // SUBSTR_HDR | || body[1]==6 // SUBSTR_HDR |
| ); | ); |
| #else | |
| assert(!body // no body | |
| || *body // ordinary string | |
| || body[1]==1 // CONCAT_HDR | |
| || body[1]==4 // FN_HDR | |
| || body[1]==6 // SUBSTR_HDR | |
| ); | |
| #endif | |
| } | } |
| /// 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() { ZERO_LENGTH 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); } |
| #ifdef STRING_LENGTH_CACHING | |
| 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; } | |
| #else | |
| size_t length() const { return CORD_len(body); } | size_t length() const { return CORD_len(body); } |
| #endif | |
| bool is_empty() const { return body==CORD_EMPTY; } | 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){ |
| body=CORD_cat_char_star(body, str, known_length); | if(body){ |
| body = CORD_cat_char_star(body, str, known_length); | |
| ZERO_LENGTH | |
| } else { | |
| body=str; | |
| #ifdef STRING_LENGTH_CACHING | |
| string_length=known_length; | |
| #endif | |
| } | |
| } | |
| } | } |
| void append_strdup_know_length(const char* str, size_t known_length) { | void append_strdup_know_length(const char* str, size_t known_length) { |
| if(known_length) | if(known_length) |
| append_know_length(pa_strdup(str, known_length), known_length); | append_know_length(pa_strdup(str, known_length), known_length); |
| } | } |
| void append(char c) { body=CORD_cat_char(body, c); } | void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); } |
| Body& operator << (const Body src) { body=CORD_cat(body, src.body); return *this; } | Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; } |
| Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; } | Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; } |
| // could not figure out why this operator is needed [should do this chain: string->simple->==] | // could not figure out why this operator is needed [should do this chain: string->simple->==] |
| Line 319 public: | Line 412 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 333 public: | Line 426 public: |
| return CORD_chr(body, offset, c); | return CORD_chr(body, offset, c); |
| } | } |
| template<typename I> int for_each( | template<typename I> |
| int (*f)(char c, I), | int for_each(int (*f)(char c, I), I info) const { |
| I info) const { | |
| return CORD_iter(body, (CORD_iter_fn)f, (void*)info); | return CORD_iter(body, (CORD_iter_fn)f, (void*)info); |
| } | } |
| template<typename I> int for_each( | template<typename I> |
| int (*f1)(char c, I), | int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const { |
| int (*f2)(const char* s, I), | |
| I info) const { | |
| return 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); |
| } | } |
| Line 387 public: | Line 477 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 char* cstr, Language alang, size_t alength){ | |
| if(cstr && *cstr){ | |
| body=cstr; | |
| #ifdef STRING_LENGTH_CACHING | |
| body.set_length(alength); | |
| #endif | |
| 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_STRING_INVARIANT(*this); | ASSERT_STRING_INVARIANT(*this); |
| } | } |
| Line 397 public: | Line 503 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(); } |
| Line 442 public: | Line 552 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, Language lang, bool forced) 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); |
| } | } |
| 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.appendHelper(body, L_AS_IS, src); | |
| 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 504 public: | Line 618 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, |
| int& matches_count) const; | int& matches_count) const; |
| enum Change_case_kind { | enum Change_case_kind { |
| Line 528 private: //disabled | Line 640 private: //disabled |
| }; | }; |
| template<> | #ifndef HASH_CODE_CACHING |
| inline size_t get_length<String::Body>(String::Body body) { | |
| return body.length(); | |
| } | |
| /// 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(); |
| } | |
| /// 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 | #endif |