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