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