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