|
|
| version 1.178, 2009/04/22 04:37:52 | version 1.185, 2009/05/15 06:57:43 |
|---|---|
| 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 21 extern "C" { // cord's author forgot to | Line 21 extern "C" { // cord's author forgot to |
| // defines | // defines |
| // cache hash code in String::Body for faster hash access | |
| #define HASH_CODE_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 117 public: | Line 120 public: |
| 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 176 public: | Line 179 public: |
| 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 |
| Line 192 public: | Line 195 public: |
| 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 |
| Line 276 public: | Line 279 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 | |
| public: | public: |
| const char* v() const; | const char* v() const; |
| void dump() const; | void dump() const; |
| #ifdef HASH_CODE_CACHING | |
| Body(): body(CORD_EMPTY), hash_code(0) {} | |
| Body(CORD abody, uint ahash_code): body(abody), hash_code(ahash_code) {} | |
| Body(CORD abody): body(abody), hash_code(0) { | |
| #else | |
| Body(): body(CORD_EMPTY) {} | Body(): body(CORD_EMPTY) {} |
| Body(CORD abody): body(abody) { | Body(CORD abody): body(abody) { |
| #endif | |
| assert(!body // no body | assert(!body // no body |
| || *body // ordinary string | || *body // ordinary string |
| || body[1]==1 // CONCAT_HDR | || body[1]==1 // CONCAT_HDR |
| Line 290 public: | Line 305 public: |
| || body[1]==6 // SUBSTR_HDR | || body[1]==6 // SUBSTR_HDR |
| ); | ); |
| } | } |
| /// 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() { 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); } |
| Line 337 public: | Line 350 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 405 public: | Line 418 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 String::C cstr, Language alang=L_CLEAN){ | |
| if(cstr.length){ | |
| body=cstr.str; | |
| 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 415 public: | Line 440 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 549 inline size_t get_length<String::Body>(S | Line 578 inline size_t get_length<String::Body>(S |
| return body.length(); | return body.length(); |
| } | } |
| #ifndef HASH_CODE_CACHING | |
| /// 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(); |
| } | } |
| #endif | |
| /// now that we've declared specialization we can use it | /// now that we've declared specialization we can use it |