Annotation of parser3/src/include/pa_string.h, revision 1.175
1.41 paf 1: /** @file
1.43 paf 2: Parser: string class decl.
3:
1.165 paf 4: Copyright (c) 2001-2005 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.175 ! misha 11: static const char * const IDENT_STRING_H="$Date: 2008-07-21 07:37:02 $";
1.145 paf 12:
13: // includes
1.4 paf 14: #include "pa_types.h"
1.145 paf 15: #include "pa_array.h"
16:
17: extern "C" { // cord's author forgot to do that
18: #define CORD_NO_IO
19: #include "cord.h"
20: };
1.4 paf 21:
1.164 paf 22: // defines
23:
1.146 paf 24: // cord extension
25: /* Returns true if x does contain */
26: /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x). */
27: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c);
1.148 paf 28: size_t CORD_block_count(CORD x);
1.146 paf 29:
1.145 paf 30: // forwards
1.9 paf 31:
1.145 paf 32: class Charset;
1.135 paf 33: class Table;
1.71 paf 34: class SQL_Connection;
1.101 parser 35: class Dictionary;
1.145 paf 36: class Request_charsets;
37: class String;
38: typedef Array<const String*> ArrayString;
39:
1.155 paf 40: // generally useful
41:
42: int pa_atoi(const char* str, const String* problem_source=0);
43: double pa_atod(const char* str, const String* problem_source=0);
44:
1.145 paf 45: /// this is result of pos functions which mean that substr were not found
46: #define STRING_NOT_FOUND ((size_t)-1)
47:
1.146 paf 48: template<typename T>
49: inline size_t get_length(T current) {
50: return current;
51: }
1.147 paf 52:
1.42 paf 53: /**
1.146 paf 54: String which knows the lang of all it's langs.
1.41 paf 55:
56: All pieces remember
57: - whether they are tainted or not,
1.146 paf 58: and the lang which should be used to detaint them
1.41 paf 59: */
1.145 paf 60: class String: public PA_Object {
1.1 paf 61: public:
1.48 paf 62:
1.146 paf 63: /** piece is tainted or not. the lang to use when detaint
1.106 parser 64: remember to change String_Untaint_lang_name @ untaint.C along
1.148 paf 65:
66: WARNING WARNING WARNING WARNING WARNING WARNING
67:
68: pos function compares(<=) languages, that is used in searching
69: for table column separator being L_CLEAN or L_AS_IS.
70: they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
71:
72: letters assigned for debugging, but it's important for no language-letter
73: come before L_AS_IS other then L_CLEAN
74:
75: WARNING WARNING WARNING WARNING WARNING WARNING
1.106 parser 76: */
1.145 paf 77: enum Language {
1.146 paf 78: L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145 paf 79: // these two must go before others, there are checks for >L_AS_IS
1.170 misha 80: L_CLEAN='0', ///< clean WARNING: read above warning before changing
81: L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing
1.122 paf 82:
1.148 paf 83: L_PASS_APPENDED='P',
1.41 paf 84: /**<
1.146 paf 85: leave lang built into string being appended.
1.41 paf 86: just a flag, that value not stored
87: */
1.170 misha 88: L_TAINTED='T', ///< tainted, untaint lang as assigned later
1.146 paf 89: // untaint langs. assigned by ^untaint[lang]{...}
1.170 misha 90: L_FILE_SPEC='F', ///< file specification
91: L_HTTP_HEADER='h', ///< text in HTTP response header
92: L_MAIL_HEADER='m', ///< text in mail header
93: L_URI='U', ///< text in uri
94: L_SQL='Q', ///< ^table:sql body
95: L_JS='J', ///< JavaScript code
1.148 paf 96: L_XML='X', ///< ^dom:set xml
1.170 misha 97: L_HTML='H', ///< HTML code
98: L_REGEX='R', ///< RegEx expression
99: L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode
1.175 ! misha 100: L_FILE_POST='f', ///temporary escaping zero-char
1.148 paf 101: // READ WARNING ABOVE BEFORE ADDING ANYTHING
1.146 paf 102: L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization
1.27 paf 103: };
104:
1.157 paf 105: enum Trim_kind {
106: TRIM_BOTH,
107: TRIM_START,
108: TRIM_END
109: };
110:
1.147 paf 111: union Languages {
1.146 paf 112:
1.147 paf 113: struct {
1.160 paf 114: #ifdef PA_LITTLE_ENDIAN
1.147 paf 115: Language lang:8;
116: int is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 117: #elif defined(PA_BIG_ENDIAN)
118: int is_not_just_lang:sizeof(CORD)*8-8;
119: Language lang:8;
120: #else
121: # error word endianness not determined for some obscure reason
122: #endif
1.147 paf 123: } opt;
124: CORD langs;
1.146 paf 125:
126: template<typename C>
127: CORD make_langs(C current) const {
1.147 paf 128: return opt.is_not_just_lang?
1.146 paf 129: langs
1.162 paf 130: :CORD_chars((char)opt.lang, get_length(current));
1.146 paf 131: }
132:
133: CORD make_langs(size_t aoffset, size_t alength) const {
1.147 paf 134: return opt.is_not_just_lang?
1.146 paf 135: CORD_substr(langs, aoffset, alength)
1.162 paf 136: :CORD_chars((char)opt.lang, alength);
1.145 paf 137: }
138:
1.157 paf 139: /// appending when 'langs' already contain something [simple cases handled elsewhere]
1.146 paf 140: template<typename C>
141: void append(C current,
142: const CORD to_nonempty_target_langs) {
143: assert(langs);
144:
1.147 paf 145: if(opt.is_not_just_lang)
1.146 paf 146: langs=CORD_cat(langs, to_nonempty_target_langs);
147: else { // we were "just lang"
148: size_t current_size=get_length(current);
149: assert(current_size);
150: langs=CORD_cat(
1.162 paf 151: CORD_chars((char)opt.lang, current_size), // first piece [making from just 'lang']
1.146 paf 152: to_nonempty_target_langs); // new piece
153: }
1.145 paf 154: }
1.146 paf 155:
1.145 paf 156: public:
1.146 paf 157:
158: const char* v() const;
1.164 paf 159: void dump() const;
1.146 paf 160:
161: Languages(): langs(0) {}
1.147 paf 162: Languages(Language alang) {
163: opt.lang=alang;
164: opt.is_not_just_lang=0;
165: }
1.146 paf 166:
167: /// MUST be called exactly prior to modification of current [uses it's length]
168: template<typename C>
169: void append(C current, Language alang, size_t asize) {
170: assert(alang);
171: assert(asize);
172:
1.147 paf 173: if(!opt.is_not_just_lang)
174: if(opt.lang) {
175: if(opt.lang==alang) // same length? ignoring
1.146 paf 176: return;
177: } else {
1.147 paf 178: opt.lang=alang; // to uninitialized
1.146 paf 179: return;
1.145 paf 180: }
1.146 paf 181:
1.162 paf 182: append(current, CORD_chars((char)alang, asize));
1.146 paf 183: }
184:
185: /// MUST be called exactly prior to modification of current [uses it's length]
186: template<typename C>
187: void append(C current, size_t appending_length,
188: const Languages src) {
189: assert(appending_length);
190:
191: if(!langs)
192: langs=src.langs; // to uninitialized
1.147 paf 193: else if(!src.opt.is_not_just_lang)
194: append(current, src.opt.lang, appending_length); // simplifying when simple source
1.146 paf 195: else
196: append(current, src.make_langs(appending_length));
197: }
198:
199: /// MUST be called exactly prior to modification of current [uses it's length]
200: template<typename C>
201: void append(C current,
202: const Languages src, size_t aoffset, size_t alength) {
203: assert(alength);
204:
205: if(!langs) // to uninitialized?
1.147 paf 206: if(src.opt.is_not_just_lang)
1.146 paf 207: langs=CORD_substr(src.langs, aoffset, alength); // to uninitialized complex
208: else
1.147 paf 209: opt.lang=src.opt.lang; // to uninitialized simple
1.146 paf 210: else
1.147 paf 211: if(!opt.is_not_just_lang && !src.opt.is_not_just_lang && opt.lang==src.opt.lang) // both simple & of same language?
1.146 paf 212: return; // ignoring
213: else
214: append(current, src.make_langs(aoffset, alength));
215: }
216:
217: /// checks if we have lang<=alang all from aoffset to aoffset+alength
218: bool check_lang(Language alang, size_t aoffset, size_t alength) const {
219: if(alang==L_UNSPECIFIED) // ignore lang?
220: return true;
221:
1.147 paf 222: if(opt.is_not_just_lang)
1.146 paf 223: return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
224: else
1.169 misha 225: return (unsigned)opt.lang<=(unsigned)alang;
1.146 paf 226: }
227:
1.148 paf 228: /// @returns count of blocks
229: /// @todo currently there can be adjucent blocks of same language. someday merge them
230: size_t count() const {
231: return opt.is_not_just_lang?
232: CORD_block_count(langs)
233: : opt.lang?
234: 1
235: : 0;
236: };
237:
1.146 paf 238: template<typename C, typename I>
239: void for_each(C current,
240: int callback(char, size_t, I), I info) const {
241:
1.147 paf 242: if(opt.is_not_just_lang)
1.146 paf 243: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
244: else
1.147 paf 245: callback(opt.lang, get_length(current), info);
1.146 paf 246: }
247:
1.164 paf 248: bool invariant(size_t current_length) const {
1.146 paf 249: if(!langs)
250: return current_length==0;
1.147 paf 251: if(opt.is_not_just_lang)
1.146 paf 252: return CORD_len(langs)==current_length;
253: return true; // uncheckable, actually
254: }
255: };
256:
257: class Body {
258:
259: CORD body;
260:
261: public:
262:
263: const char* v() const;
1.164 paf 264: void dump() const;
1.146 paf 265:
266: Body(): body(CORD_EMPTY) {}
267: Body(CORD abody): body(abody) {
268: assert(!body // no body
269: || *body // ordinary string
270: || body[1]==1 // CONCAT_HDR
271: || body[1]==4 // FN_HDR
272: || body[1]==6 // SUBSTR_HDR
273: );
274: }
275: /// WARNING: length is only HELPER length, str in ANY case should be zero-terminated
276: Body(const char* str, size_t helper_length): body(CORD_EMPTY) {
277: append_know_length(str, helper_length?helper_length:strlen(str));
278: }
279: static Body Format(int value);
280:
281: void clear() { body=CORD_EMPTY; }
282:
283: bool operator! () const { return is_empty(); }
284:
285: uint hash_code() const;
286:
287: const char* cstr() const { return CORD_to_const_char_star(body); }
288: char* cstrm() const { return CORD_to_char_star(body); }
289:
290: size_t length() const { return CORD_len(body); }
291:
292: bool is_empty() const { return body==CORD_EMPTY; }
293:
294: void append_know_length(const char *str, size_t known_length) {
295: if(known_length)
296: body=CORD_cat_char_star(body, str, known_length);
297: }
298: void append_strdup_know_length(const char* str, size_t known_length) {
299: if(known_length)
300: append_know_length(pa_strdup(str, known_length), known_length);
301: }
302: void append(char c) { body=CORD_cat_char(body, c); }
303: Body& operator << (const Body src) { body=CORD_cat(body, src.body); return *this; }
304: Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
305:
306: // could not figure out why this operator is needed [should do this chain: string->simple->==]
307: bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
308: bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
309: bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
310: bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
311: bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
312: bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
313:
314: int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
315: return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145 paf 316: }
317:
1.146 paf 318: char fetch(size_t index) const { return CORD_fetch(body, index); }
319: Body mid(size_t index, size_t length) const { return CORD_substr(body, index, length); }
320: size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr); }
321: size_t pos(const Body substr, size_t offset=0) const {
322: if(!substr.length())
323: return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150 paf 324:
325: // CORD_str checks for bad offset [CORD_chr does not]
1.146 paf 326: return CORD_str(body, offset, substr.body);
327: }
328: size_t pos(char c,
329: size_t offset=0) const {
1.150 paf 330: if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
331: return STRING_NOT_FOUND;
332:
1.146 paf 333: return CORD_chr(body, offset, c);
1.145 paf 334: }
1.146 paf 335:
1.159 paf 336: template<typename I> int for_each(
337: int (*f)(char c, I),
338: I info) const {
339: return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
340: }
341:
342: template<typename I> int for_each(
1.149 paf 343: int (*f1)(char c, I),
344: int (*f2)(const char* s, I),
345: I info) const {
1.159 paf 346: return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148 paf 347: }
1.146 paf 348:
349: void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
350:
351: /*Body normalize() const {
352: return Body(CORD_balance(body));
353: }*/
1.157 paf 354:
355: /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
356: Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
357: size_t* out_start=0, size_t* out_length=0) const;
1.145 paf 358: };
359:
360: struct C {
361: const char *str;
362: size_t length;
363: operator const char *() { return str; }
1.158 paf 364: C(): str(0), length(0) {}
1.145 paf 365: C(const char *astr, size_t asize): str(astr), length(asize) {}
366: };
367:
368: struct Cm {
369: char *str;
370: size_t length;
371: //operator char *() { return str; }
1.158 paf 372: Cm(): str(0), length(0) {}
1.145 paf 373: Cm(char *astr, size_t asize): str(astr), length(asize) {}
374: };
375:
376: private:
377:
1.152 paf 378: Body body; ///< all characters of string
1.146 paf 379: Languages langs; ///< string characters lang
380:
381: const char* v() const;
1.164 paf 382: void dump() const;
383: #define ASSERT_STRING_INVARIANT(string) \
384: assert((string).langs.invariant((string).body.length()))
1.145 paf 385:
1.8 paf 386: public:
1.151 paf 387:
388: static const String Empty;
1.8 paf 389:
1.145 paf 390: explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false);
391: explicit String(const C cstr, bool tainted=false);
1.146 paf 392: String(Body abody, Language alang): body(abody), langs(alang) {
393: ASSERT_STRING_INVARIANT(*this);
394: }
395: String(const String& src): body(src.body), langs(src.langs) {
396: ASSERT_STRING_INVARIANT(*this);
1.145 paf 397: }
398:
399: /// for convinient hash lookup
1.146 paf 400: operator const Body() const { return body; }
1.145 paf 401:
402: bool is_empty() const { return body.is_empty(); }
403: size_t length() const { return body.length(); }
1.171 misha 404: size_t length(Charset& charset) const;
1.145 paf 405:
406: /// convert to CORD. if 'lang' known, forcing 'lang' to it
1.146 paf 407: Body cstr_to_string_body(Language lang=L_AS_IS,
1.145 paf 408: SQL_Connection* connection=0,
409: const Request_charsets *charsets=0) const;
410:
411: /// convert to constant C string. if 'lang' known, forcing 'lang' to it
412: const char* cstr(Language lang=L_AS_IS,
413: SQL_Connection* connection=0,
414: const Request_charsets *charsets=0) const {
415: return cstr_to_string_body(lang, connection, charsets).cstr();
416: }
417: /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it
418: char *cstrm(Language lang=L_AS_IS,
419: SQL_Connection* connection=0,
420: const Request_charsets *charsets=0) const {
421: return cstr_to_string_body(lang, connection, charsets).cstrm();
1.50 paf 422: }
1.108 parser 423: /// puts pieces to buf
1.145 paf 424: Cm serialize(size_t prolog_size) const;
1.108 parser 425: /// appends pieces from buf to self
1.145 paf 426: bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146 paf 427: /// @see Body::append_know_length
1.145 paf 428: String& append_know_length(const char* str, size_t known_length, Language lang);
1.146 paf 429: /// @see Body::append_help_length
1.145 paf 430: String& append_help_length(const char* str, size_t helper_length, Language lang);
431: String& append_strdup(const char* str, size_t helper_length, Language lang);
432:
1.146 paf 433: bool operator == (const char* y) const { return body==Body(y); }
434: bool operator != (const char* y) const { return body!=Body(y); }
1.145 paf 435:
436: /// this starts with y
437: bool starts_with(const char* y) const {
1.146 paf 438: return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145 paf 439: }
440: /// x starts with this
441: bool this_starts(const char* x) const {
1.146 paf 442: return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26 paf 443: }
444:
1.145 paf 445: String& append_to(String& dest, Language lang, bool forced) const;
446: String& append(const String& src, Language lang, bool forced=false) {
447: return src.append_to(*this, lang, forced);
448: }
449: String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
450: String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.147 paf 451: String& operator << (const Body src);
1.100 parser 452:
1.142 paf 453: /// extracts first char of a string, if any
454: char first_char() const {
1.145 paf 455: return is_empty()?0:body.fetch(0);
1.142 paf 456: }
1.54 paf 457:
1.145 paf 458: bool operator < (const String& src) const { return body<src.body; }
459: bool operator > (const String& src) const { return body>src.body; }
460: bool operator <= (const String& src) const { return body<=src.body; }
461: bool operator >= (const String& src) const { return body>=src.body; }
462: bool operator != (const String& src) const { return body!=src.body; }
463: bool operator == (const String& src) const { return body==src.body; }
464:
1.54 paf 465: /// extracts [start, finish) piece of string
1.145 paf 466: String& mid(size_t substr_begin, size_t substr_end) const;
1.172 misha 467: String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const;
1.145 paf 468:
469: /**
470: ignore lang if it's L_UNSPECIFIED
471: but when specified: look for substring that lies in ONE fragment in THAT lang
472: @return position of substr in string, -1 means "not found" [const char* version]
473: */
1.146 paf 474: size_t pos(const Body substr,
1.145 paf 475: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
476: /// String version of @see pos(const char*, int, Language)
477: size_t pos(const String& substr,
478: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
479: size_t pos(char c,
480: size_t this_offset=0) const {
481: return body.pos(c, this_offset);
482: }
1.173 misha 483: size_t pos(Charset& charset,
1.171 misha 484: const String& substr,
485: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55 paf 486:
1.145 paf 487: void split(ArrayString& result,
488: size_t& pos_after,
489: const char* delim,
490: Language lang=L_UNSPECIFIED, int limit=-1) const;
491: void split(ArrayString& result,
492: size_t& pos_after,
1.62 paf 493: const String& delim,
1.145 paf 494: Language lang=L_UNSPECIFIED, int limit=-1) const;
1.62 paf 495:
1.145 paf 496: typedef void (*Row_action)(Table& table, ArrayString* row,
1.136 paf 497: int prestart, int prefinish,
498: int poststart, int postfinish,
1.68 paf 499: void *info);
1.87 parser 500: /**
1.145 paf 501: @return table of found items, if any.
1.87 parser 502: table format is defined and fixed[can be used by others]:
503: @verbatim
504: prematch/match/postmatch/1/2/3/...
505: @endverbatim
506: */
1.145 paf 507: Table* match(Charset& source_charset,
1.64 paf 508: const String& regexp,
1.145 paf 509: const String* options,
1.99 parser 510: Row_action row_action, void *info,
1.168 misha 511: int& matches_count) const;
1.87 parser 512: enum Change_case_kind {
513: CC_UPPER,
514: CC_LOWER
515: };
1.145 paf 516: String& change_case(Charset& source_charset,
1.87 parser 517: Change_case_kind kind) const;
1.145 paf 518: const String& replace(const Dictionary& dict) const;
1.157 paf 519: const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0) const;
1.155 paf 520: double as_double() const { return pa_atod(cstr(), this); }
521: int as_int() const { return pa_atoi(cstr(), this); }
1.167 misha 522: bool as_bool() const { return as_int()!=0; }
1.174 misha 523: const String& escape(Charset& source_charset) const;
1.137 paf 524:
1.7 paf 525: private: //disabled
526:
1.12 paf 527: String& operator = (const String&) { return *this; }
1.7 paf 528:
1.1 paf 529: };
1.119 paf 530:
1.146 paf 531: template<>
532: inline size_t get_length<String::Body>(String::Body body) {
533: return body.length();
534: }
535:
1.145 paf 536: /// simple hash code of string. used by Hash
1.146 paf 537: inline uint hash_code(const String::Body self) {
1.145 paf 538: return self.hash_code();
1.119 paf 539: }
1.147 paf 540:
541:
542: /// now that we've declared specialization we can use it
543: inline String& String::operator << (const String::Body src) {
544: langs.append(body, L_AS_IS, src.length());
545: body<<src;
546: return *this;
547: }
548:
1.1 paf 549:
550: #endif
E-mail: