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