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