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