Annotation of parser3/src/include/pa_string.h, revision 1.146
1.41 paf 1: /** @file
1.43 paf 2: Parser: string class decl.
3:
1.145 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.124 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_STRING_H
9: #define PA_STRING_H
1.140 paf 10:
1.146 ! paf 11: static const char* IDENT_STRING_H="$Date: 2003/09/24 14:32:06 $";
1.145 paf 12:
13: // includes
1.1 paf 14:
1.4 paf 15: #include "pa_types.h"
1.145 paf 16: #include "pa_array.h"
17:
18: extern "C" { // cord's author forgot to do that
19: #define CORD_NO_IO
20: #include "cord.h"
21: };
1.4 paf 22:
1.146 ! paf 23: // cord extension
! 24: /* Returns true if x does contain */
! 25: /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x). */
! 26: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c);
! 27:
1.145 paf 28: // forwards
1.9 paf 29:
1.145 paf 30: class Charset;
1.135 paf 31: class Table;
1.71 paf 32: class SQL_Connection;
1.101 parser 33: class Dictionary;
1.145 paf 34: class Request_charsets;
35: class String;
36: typedef Array<const String*> ArrayString;
37:
38: /// this is result of pos functions which mean that substr were not found
39: #define STRING_NOT_FOUND ((size_t)-1)
40:
1.146 ! paf 41: template<typename T>
! 42: inline size_t get_length(T current) {
! 43: return current;
! 44: }
1.42 paf 45: /**
1.146 ! paf 46: String which knows the lang of all it's langs.
1.41 paf 47:
48: All pieces remember
49: - whether they are tainted or not,
1.146 ! paf 50: and the lang which should be used to detaint them
1.41 paf 51: */
1.145 paf 52: class String: public PA_Object {
1.1 paf 53: public:
1.48 paf 54:
1.146 ! paf 55: /** piece is tainted or not. the lang to use when detaint
1.106 parser 56: remember to change String_Untaint_lang_name @ untaint.C along
57: */
1.145 paf 58: enum Language {
1.146 ! paf 59: L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145 paf 60: // these two must go before others, there are checks for >L_AS_IS
1.146 ! paf 61: L_CLEAN='1', ///< clean
1.145 paf 62: L_AS_IS, ///< leave all characters intact
1.122 paf 63:
1.145 paf 64: L_PASS_APPENDED,
1.41 paf 65: /**<
1.146 ! paf 66: leave lang built into string being appended.
1.41 paf 67: just a flag, that value not stored
68: */
1.146 ! paf 69: L_TAINTED, ///< tainted, untaint lang as assigned later
! 70: // untaint langs. assigned by ^untaint[lang]{...}
1.145 paf 71: L_FILE_SPEC, ///< file specification
72: L_HTTP_HEADER, ///< text in HTTP response header
73: L_MAIL_HEADER, ///< text in mail header
74: L_URI, ///< text in uri
75: L_TABLE, ///< ^table:set body
76: L_SQL, ///< ^table:sql body
77: L_JS, ///< JavaScript code
78: L_XML, ///< ^dom:set xml
79: L_HTML, ///< HTML code (for editing)
1.146 ! paf 80: L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization
1.27 paf 81: };
82:
1.146 ! paf 83: class Languages {
! 84:
! 85: union {
! 86: struct {
! 87: Language lang:8;
! 88: int is_not_just_lang:sizeof(CORD)*8-8;
! 89: };
! 90: CORD langs;
! 91: };
! 92:
! 93: template<typename C>
! 94: CORD make_langs(C current) const {
! 95: return is_not_just_lang?
! 96: langs
! 97: :CORD_chars((char)lang, get_length(current));
! 98: }
! 99:
! 100: CORD make_langs(size_t aoffset, size_t alength) const {
! 101: return is_not_just_lang?
! 102: CORD_substr(langs, aoffset, alength)
! 103: :CORD_chars((char)lang, alength);
1.145 paf 104: }
105:
1.146 ! paf 106: /// appending when 'langs' already contain something [simple cases hanled elsewhere]
! 107: template<typename C>
! 108: void append(C current,
! 109: const CORD to_nonempty_target_langs) {
! 110: assert(langs);
! 111:
! 112: if(is_not_just_lang)
! 113: langs=CORD_cat(langs, to_nonempty_target_langs);
! 114: else { // we were "just lang"
! 115: size_t current_size=get_length(current);
! 116: assert(current_size);
! 117: langs=CORD_cat(
! 118: CORD_chars((char)lang, current_size), // first piece [making from just 'lang']
! 119: to_nonempty_target_langs); // new piece
! 120: }
1.145 paf 121: }
1.146 ! paf 122:
1.145 paf 123: public:
1.146 ! paf 124:
! 125: const char* v() const;
! 126:
! 127: Languages(): langs(0) {}
! 128: Languages(Language alang): lang(alang), is_not_just_lang(0) {}
! 129:
! 130: /// MUST be called exactly prior to modification of current [uses it's length]
! 131: template<typename C>
! 132: void append(C current, Language alang, size_t asize) {
! 133: assert(alang);
! 134: assert(asize);
! 135:
! 136: if(!is_not_just_lang)
! 137: if(lang) {
! 138: if(lang==alang) // same length? ignoring
! 139: return;
! 140: } else {
! 141: lang=alang; // to uninitialized
! 142: return;
1.145 paf 143: }
1.146 ! paf 144:
! 145: append(current, CORD_chars((char)alang, asize));
! 146: }
! 147:
! 148: /// MUST be called exactly prior to modification of current [uses it's length]
! 149: template<typename C>
! 150: void append(C current, size_t appending_length,
! 151: const Languages src) {
! 152: assert(appending_length);
! 153:
! 154: if(!langs)
! 155: langs=src.langs; // to uninitialized
! 156: else if(!src.is_not_just_lang)
! 157: append(current, src.lang, appending_length); // simplifying when simple source
! 158: else
! 159: append(current, src.make_langs(appending_length));
! 160: }
! 161:
! 162: /// MUST be called exactly prior to modification of current [uses it's length]
! 163: template<typename C>
! 164: void append(C current,
! 165: const Languages src, size_t aoffset, size_t alength) {
! 166: assert(alength);
! 167:
! 168: if(!langs) // to uninitialized?
! 169: if(src.is_not_just_lang)
! 170: langs=CORD_substr(src.langs, aoffset, alength); // to uninitialized complex
! 171: else
! 172: lang=src.lang; // to uninitialized simple
! 173: else
! 174: if(!is_not_just_lang && !src.is_not_just_lang && lang==src.lang) // both simple & of same language?
! 175: return; // ignoring
! 176: else
! 177: append(current, src.make_langs(aoffset, alength));
! 178: }
! 179:
! 180: /// checks if we have lang<=alang all from aoffset to aoffset+alength
! 181: bool check_lang(Language alang, size_t aoffset, size_t alength) const {
! 182: if(alang==L_UNSPECIFIED) // ignore lang?
! 183: return true;
! 184:
! 185: if(is_not_just_lang)
! 186: return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
! 187: else
! 188: return lang<=alang;
! 189: }
! 190:
! 191: template<typename C, typename I>
! 192: void for_each(C current,
! 193: int callback(char, size_t, I), I info) const {
! 194:
! 195: if(is_not_just_lang)
! 196: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
! 197: else
! 198: callback(lang, get_length(current), info);
! 199: }
! 200:
! 201: bool invariant(size_t current_length) {
! 202: if(!langs)
! 203: return current_length==0;
! 204: if(is_not_just_lang)
! 205: return CORD_len(langs)==current_length;
! 206: return true; // uncheckable, actually
! 207: }
! 208: };
! 209:
! 210: class Body {
! 211:
! 212: CORD body;
! 213:
! 214: public:
! 215:
! 216: const char* v() const;
! 217:
! 218: Body(): body(CORD_EMPTY) {}
! 219: Body(CORD abody): body(abody) {
! 220: assert(!body // no body
! 221: || *body // ordinary string
! 222: || body[1]==1 // CONCAT_HDR
! 223: || body[1]==4 // FN_HDR
! 224: || body[1]==6 // SUBSTR_HDR
! 225: );
! 226: }
! 227: /// WARNING: length is only HELPER length, str in ANY case should be zero-terminated
! 228: Body(const char* str, size_t helper_length): body(CORD_EMPTY) {
! 229: append_know_length(str, helper_length?helper_length:strlen(str));
! 230: }
! 231: static Body Format(int value);
! 232:
! 233: void clear() { body=CORD_EMPTY; }
! 234:
! 235: bool operator! () const { return is_empty(); }
! 236:
! 237: uint hash_code() const;
! 238:
! 239: const char* cstr() const { return CORD_to_const_char_star(body); }
! 240: char* cstrm() const { return CORD_to_char_star(body); }
! 241:
! 242: size_t length() const { return CORD_len(body); }
! 243:
! 244: bool is_empty() const { return body==CORD_EMPTY; }
! 245:
! 246: void append_know_length(const char *str, size_t known_length) {
! 247: if(known_length)
! 248: body=CORD_cat_char_star(body, str, known_length);
! 249: }
! 250: void append_strdup_know_length(const char* str, size_t known_length) {
! 251: if(known_length)
! 252: append_know_length(pa_strdup(str, known_length), known_length);
! 253: }
! 254: void append(char c) { body=CORD_cat_char(body, c); }
! 255: Body& operator << (const Body src) { body=CORD_cat(body, src.body); return *this; }
! 256: Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
! 257:
! 258: // could not figure out why this operator is needed [should do this chain: string->simple->==]
! 259: bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
! 260: bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
! 261: bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
! 262: bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
! 263: bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
! 264: bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
! 265:
! 266: int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
! 267: return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145 paf 268: }
269:
1.146 ! paf 270: char fetch(size_t index) const { return CORD_fetch(body, index); }
! 271: Body mid(size_t index, size_t length) const { return CORD_substr(body, index, length); }
! 272: size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr); }
! 273: size_t pos(const Body substr, size_t offset=0) const {
! 274: if(!substr.length())
! 275: return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
! 276: return CORD_str(body, offset, substr.body);
! 277: }
! 278: size_t pos(char c,
! 279: size_t offset=0) const {
! 280: return CORD_chr(body, offset, c);
1.145 paf 281: }
1.146 ! paf 282:
! 283: /* template<typename I> void for_each(int (*callback)(const char* s, I), I info) const {
! 284: CORD_iter5(body, 0, 0, (CORD_batched_iter_fn)callback, info);
! 285: }*/
! 286:
! 287: void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
! 288:
! 289: /*Body normalize() const {
! 290: return Body(CORD_balance(body));
! 291: }*/
1.145 paf 292: };
293:
1.146 ! paf 294:
1.145 paf 295: struct C {
296: const char *str;
297: size_t length;
298: operator const char *() { return str; }
299: C(const char *astr, size_t asize): str(astr), length(asize) {}
300: };
301:
302: struct Cm {
303: char *str;
304: size_t length;
305: //operator char *() { return str; }
306: Cm(char *astr, size_t asize): str(astr), length(asize) {}
307: };
308:
309: private:
310:
1.146 ! paf 311: Languages langs; ///< string characters lang
! 312: Body body; ///< all characters of string
! 313:
! 314: const char* v() const;
! 315:
! 316: #define ASSERT_STRING_INVARIANT(string) \
! 317: assert((string).langs.invariant((string).body.length()))
1.145 paf 318:
1.8 paf 319: public:
320:
1.145 paf 321: explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false);
322: explicit String(const C cstr, bool tainted=false);
1.146 ! paf 323: String(Body abody, Language alang): body(abody), langs(alang) {
! 324: assert(!body.is_empty());
! 325: ASSERT_STRING_INVARIANT(*this);
! 326: }
! 327: String(const String& src): body(src.body), langs(src.langs) {
! 328: ASSERT_STRING_INVARIANT(*this);
1.145 paf 329: }
330:
331: /// for convinient hash lookup
1.146 ! paf 332: operator const Body() const { return body; }
1.145 paf 333:
334: bool is_empty() const { return body.is_empty(); }
335: size_t length() const { return body.length(); }
336:
337: /// convert to CORD. if 'lang' known, forcing 'lang' to it
1.146 ! paf 338: Body cstr_to_string_body(Language lang=L_AS_IS,
1.145 paf 339: SQL_Connection* connection=0,
340: const Request_charsets *charsets=0) const;
341:
342: /// convert to constant C string. if 'lang' known, forcing 'lang' to it
343: const char* cstr(Language lang=L_AS_IS,
344: SQL_Connection* connection=0,
345: const Request_charsets *charsets=0) const {
346: return cstr_to_string_body(lang, connection, charsets).cstr();
347: }
348: /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it
349: char *cstrm(Language lang=L_AS_IS,
350: SQL_Connection* connection=0,
351: const Request_charsets *charsets=0) const {
352: return cstr_to_string_body(lang, connection, charsets).cstrm();
1.50 paf 353: }
1.108 parser 354: /// puts pieces to buf
1.145 paf 355: Cm serialize(size_t prolog_size) const;
1.108 parser 356: /// appends pieces from buf to self
1.145 paf 357: bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146 ! paf 358: /// @see Body::append_know_length
1.145 paf 359: String& append_know_length(const char* str, size_t known_length, Language lang);
1.146 ! paf 360: /// @see Body::append_help_length
1.145 paf 361: String& append_help_length(const char* str, size_t helper_length, Language lang);
362: String& append_strdup(const char* str, size_t helper_length, Language lang);
363:
1.146 ! paf 364: bool operator == (const char* y) const { return body==Body(y); }
! 365: bool operator != (const char* y) const { return body!=Body(y); }
1.145 paf 366:
367: /// this starts with y
368: bool starts_with(const char* y) const {
1.146 ! paf 369: return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145 paf 370: }
371: /// x starts with this
372: bool this_starts(const char* x) const {
1.146 ! paf 373: return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26 paf 374: }
375:
1.145 paf 376: String& append_to(String& dest, Language lang, bool forced) const;
377: String& append(const String& src, Language lang, bool forced=false) {
378: return src.append_to(*this, lang, forced);
379: }
380: String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
381: String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.146 ! paf 382: String& operator << (const Body src) {
! 383: langs.append(body, L_AS_IS, src.length());
1.145 paf 384: body<<src;
385: return *this;
386: }
1.100 parser 387:
1.142 paf 388: /// extracts first char of a string, if any
389: char first_char() const {
1.145 paf 390: return is_empty()?0:body.fetch(0);
1.142 paf 391: }
1.54 paf 392:
1.145 paf 393: bool operator < (const String& src) const { return body<src.body; }
394: bool operator > (const String& src) const { return body>src.body; }
395: bool operator <= (const String& src) const { return body<=src.body; }
396: bool operator >= (const String& src) const { return body>=src.body; }
397: bool operator != (const String& src) const { return body!=src.body; }
398: bool operator == (const String& src) const { return body==src.body; }
399:
1.54 paf 400: /// extracts [start, finish) piece of string
1.145 paf 401: String& mid(size_t substr_begin, size_t substr_end) const;
402:
403: /**
404: ignore lang if it's L_UNSPECIFIED
405: but when specified: look for substring that lies in ONE fragment in THAT lang
406: @return position of substr in string, -1 means "not found" [const char* version]
407: */
1.146 ! paf 408: size_t pos(const Body substr,
1.145 paf 409: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
410: /// String version of @see pos(const char*, int, Language)
411: size_t pos(const String& substr,
412: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
413: size_t pos(char c,
414: size_t this_offset=0) const {
415: return body.pos(c, this_offset);
416: }
1.55 paf 417:
1.145 paf 418: void split(ArrayString& result,
419: size_t& pos_after,
420: const char* delim,
421: Language lang=L_UNSPECIFIED, int limit=-1) const;
422: void split(ArrayString& result,
423: size_t& pos_after,
1.62 paf 424: const String& delim,
1.145 paf 425: Language lang=L_UNSPECIFIED, int limit=-1) const;
1.62 paf 426:
1.145 paf 427: typedef void (*Row_action)(Table& table, ArrayString* row,
1.136 paf 428: int prestart, int prefinish,
429: int poststart, int postfinish,
1.68 paf 430: void *info);
1.87 parser 431: /**
1.145 paf 432: @return table of found items, if any.
1.87 parser 433: table format is defined and fixed[can be used by others]:
434: @verbatim
435: prematch/match/postmatch/1/2/3/...
436: @endverbatim
437: */
1.145 paf 438: Table* match(Charset& source_charset,
1.64 paf 439: const String& regexp,
1.145 paf 440: const String* options,
1.99 parser 441: Row_action row_action, void *info,
1.145 paf 442: bool& just_matched) const;
1.87 parser 443: enum Change_case_kind {
444: CC_UPPER,
445: CC_LOWER
446: };
1.145 paf 447: String& change_case(Charset& source_charset,
1.87 parser 448: Change_case_kind kind) const;
1.145 paf 449: const String& replace(const Dictionary& dict) const;
1.96 parser 450: double as_double() const;
451: int as_int() const;
1.137 paf 452:
1.7 paf 453: private: //disabled
454:
1.12 paf 455: String& operator = (const String&) { return *this; }
1.7 paf 456:
1.1 paf 457: };
1.119 paf 458:
1.146 ! paf 459: template<>
! 460: inline size_t get_length<String::Body>(String::Body body) {
! 461: return body.length();
! 462: }
! 463:
1.145 paf 464: /// simple hash code of string. used by Hash
1.146 ! paf 465: inline uint hash_code(const String::Body self) {
1.145 paf 466: return self.hash_code();
1.119 paf 467: }
1.1 paf 468:
469: #endif
E-mail: