Annotation of parser3/src/include/pa_string.h, revision 1.203
1.41 paf 1: /** @file
1.43 paf 2: Parser: string class decl.
3:
1.201 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (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.203 ! misha 11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.202 2012-04-12 22:44:46 moko Exp $"
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"
1.186 misha 20:
21: #ifdef CORD_CAT_OPTIMIZATION
22: #define CORD_cat(x, y) CORD_cat_optimized(x, y)
23: #define CORD_cat_char_star(x, y, leny) CORD_cat_char_star_optimized(x, y, leny)
24: #endif
1.145 paf 25: };
1.4 paf 26:
1.164 paf 27: // defines
28:
1.183 misha 29: // cache hash code in String::Body for faster hash access
30: #define HASH_CODE_CACHING
31:
1.187 misha 32: // cache String::Body.length() for char* strings only, CORDs have own length
33: #define STRING_LENGTH_CACHING
34:
35:
1.146 paf 36: // cord extension
37: /* Returns true if x does contain */
38: /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x). */
39: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c);
1.148 paf 40: size_t CORD_block_count(CORD x);
1.146 paf 41:
1.145 paf 42: // forwards
1.9 paf 43:
1.145 paf 44: class Charset;
1.135 paf 45: class Table;
1.71 paf 46: class SQL_Connection;
1.101 parser 47: class Dictionary;
1.145 paf 48: class Request_charsets;
49: class String;
50: typedef Array<const String*> ArrayString;
1.178 misha 51: class VRegex;
1.145 paf 52:
1.155 paf 53: // generally useful
54:
55: int pa_atoi(const char* str, const String* problem_source=0);
56: double pa_atod(const char* str, const String* problem_source=0);
57:
1.145 paf 58: /// this is result of pos functions which mean that substr were not found
59: #define STRING_NOT_FOUND ((size_t)-1)
60:
1.42 paf 61: /**
1.146 paf 62: String which knows the lang of all it's langs.
1.41 paf 63:
64: All pieces remember
65: - whether they are tainted or not,
1.146 paf 66: and the lang which should be used to detaint them
1.41 paf 67: */
1.176 misha 68:
69:
1.145 paf 70: class String: public PA_Object {
1.1 paf 71: public:
1.48 paf 72:
1.146 paf 73: /** piece is tainted or not. the lang to use when detaint
1.106 parser 74: remember to change String_Untaint_lang_name @ untaint.C along
1.148 paf 75:
76: WARNING WARNING WARNING WARNING WARNING WARNING
77:
78: pos function compares(<=) languages, that is used in searching
79: for table column separator being L_CLEAN or L_AS_IS.
80: they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
81:
82: letters assigned for debugging, but it's important for no language-letter
83: come before L_AS_IS other then L_CLEAN
84:
85: WARNING WARNING WARNING WARNING WARNING WARNING
1.106 parser 86: */
1.145 paf 87: enum Language {
1.146 paf 88: L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145 paf 89: // these two must go before others, there are checks for >L_AS_IS
1.170 misha 90: L_CLEAN='0', ///< clean WARNING: read above warning before changing
91: L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing
1.122 paf 92:
1.148 paf 93: L_PASS_APPENDED='P',
1.41 paf 94: /**<
1.146 paf 95: leave lang built into string being appended.
1.41 paf 96: just a flag, that value not stored
97: */
1.170 misha 98: L_TAINTED='T', ///< tainted, untaint lang as assigned later
1.146 paf 99: // untaint langs. assigned by ^untaint[lang]{...}
1.170 misha 100: L_FILE_SPEC='F', ///< file specification
101: L_HTTP_HEADER='h', ///< text in HTTP response header
102: L_MAIL_HEADER='m', ///< text in mail header
1.192 misha 103: L_URI='U', ///< text in uri
104: L_SQL='Q', ///< ^table:sql body
105: L_JS='J', ///< JavaScript code
1.198 misha 106: L_XML='X', ///< ^xdoc:create xml
1.192 misha 107: L_HTML='H', ///< HTML code
1.198 misha 108: L_REGEX='R', ///< RegExp
109: L_JSON='S', ///< JSON code
1.170 misha 110: L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode
1.192 misha 111: L_PARSER_CODE='p', ///< ^process body
1.148 paf 112: // READ WARNING ABOVE BEFORE ADDING ANYTHING
1.146 paf 113: L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization
1.27 paf 114: };
115:
1.157 paf 116: enum Trim_kind {
117: TRIM_BOTH,
118: TRIM_START,
119: TRIM_END
120: };
121:
1.187 misha 122: class Body;
1.176 misha 123:
1.147 paf 124: union Languages {
1.146 paf 125:
1.147 paf 126: struct {
1.160 paf 127: #ifdef PA_LITTLE_ENDIAN
1.147 paf 128: Language lang:8;
1.179 misha 129: size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 130: #elif defined(PA_BIG_ENDIAN)
1.179 misha 131: size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 132: Language lang:8;
133: #else
134: # error word endianness not determined for some obscure reason
135: #endif
1.147 paf 136: } opt;
137: CORD langs;
1.146 paf 138:
1.187 misha 139: CORD make_langs(const Body& current) const {
140: return opt.is_not_just_lang?langs:CORD_chars((char)opt.lang, current.length());
1.146 paf 141: }
142:
143: CORD make_langs(size_t aoffset, size_t alength) const {
1.147 paf 144: return opt.is_not_just_lang?
1.194 misha 145: CORD_substr(langs, aoffset, alength, 0)
1.162 paf 146: :CORD_chars((char)opt.lang, alength);
1.145 paf 147: }
148:
1.157 paf 149: /// appending when 'langs' already contain something [simple cases handled elsewhere]
1.187 misha 150: void append(size_t current, const CORD to_nonempty_target_langs) {
1.146 paf 151: assert(langs);
1.187 misha 152: if(opt.is_not_just_lang)
153: langs=CORD_cat(langs, to_nonempty_target_langs);
154: else {
155: assert(current);
156: langs=CORD_cat(CORD_chars((char)opt.lang, current), to_nonempty_target_langs);
157: }
158: }
1.146 paf 159:
1.187 misha 160: void append(const Body& current, const CORD to_nonempty_target_langs) {
161: assert(langs);
1.147 paf 162: if(opt.is_not_just_lang)
1.146 paf 163: langs=CORD_cat(langs, to_nonempty_target_langs);
1.187 misha 164: else {
165: size_t current_size=current.length();
1.146 paf 166: assert(current_size);
1.187 misha 167: langs=CORD_cat(CORD_chars((char)opt.lang, current_size), to_nonempty_target_langs);
1.146 paf 168: }
1.145 paf 169: }
1.146 paf 170:
1.145 paf 171: public:
1.146 paf 172:
173: const char* v() const;
1.164 paf 174: void dump() const;
1.146 paf 175:
176: Languages(): langs(0) {}
1.147 paf 177: Languages(Language alang) {
178: opt.lang=alang;
179: opt.is_not_just_lang=0;
180: }
1.146 paf 181:
182: /// MUST be called exactly prior to modification of current [uses it's length]
1.187 misha 183: void append(size_t current, Language alang, size_t length) {
1.146 paf 184: assert(alang);
1.187 misha 185: assert(length);
1.146 paf 186:
1.147 paf 187: if(!opt.is_not_just_lang)
188: if(opt.lang) {
1.181 misha 189: if(opt.lang==alang) // same language? ignoring
1.146 paf 190: return;
191: } else {
1.147 paf 192: opt.lang=alang; // to uninitialized
1.146 paf 193: return;
1.145 paf 194: }
1.146 paf 195:
1.187 misha 196: append(current, CORD_chars((char)alang, length));
1.146 paf 197: }
198:
1.187 misha 199: void append(const Body ¤t, Language alang, size_t length) {
1.176 misha 200: assert(alang);
1.187 misha 201: assert(length);
1.176 misha 202:
203: if(!opt.is_not_just_lang)
204: if(opt.lang) {
1.183 misha 205: if(opt.lang==alang) // same language? ignoring
1.176 misha 206: return;
207: } else {
208: opt.lang=alang; // to uninitialized
209: return;
210: }
211:
1.187 misha 212: append(current, CORD_chars((char)alang, length));
1.176 misha 213: }
214:
1.187 misha 215: void appendHelper(const Body& current, Language alang, const Body &length_helper) {
216: assert(alang);
217:
218: if(!opt.is_not_just_lang)
219: if(opt.lang) {
220: if(opt.lang==alang) // same language? ignoring
221: return;
222: } else {
223: opt.lang=alang; // to uninitialized
224: return;
225: }
226:
227: append(current, CORD_chars((char)alang, length_helper.length()));
228: }
229:
230: void appendHelper(const Body& current, const Languages &src, const Body& length_helper) {
1.186 misha 231: if(!langs){
1.176 misha 232: langs=src.langs; // to uninitialized
1.186 misha 233: #ifdef CORD_CAT_OPTIMIZATION
234: if(opt.is_not_just_lang && !CORD_IS_STRING(langs))
235: CORD_concatenation_protect(langs);
236: #endif
237: }
1.176 misha 238: else if(!src.opt.is_not_just_lang)
239: appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source
240: else
241: append(current, src.make_langs(length_helper));
242: }
243:
1.146 paf 244: /// MUST be called exactly prior to modification of current [uses it's length]
1.187 misha 245: void append(const Body& current, const Languages src, size_t aoffset, size_t alength) {
1.146 paf 246: assert(alength);
247:
248: if(!langs) // to uninitialized?
1.147 paf 249: if(src.opt.is_not_just_lang)
1.194 misha 250: langs=CORD_substr(src.langs, aoffset, alength, 0); // to uninitialized complex
1.146 paf 251: else
1.147 paf 252: opt.lang=src.opt.lang; // to uninitialized simple
1.146 paf 253: else
1.147 paf 254: 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 255: return; // ignoring
256: else
257: append(current, src.make_langs(aoffset, alength));
258: }
259:
260: /// checks if we have lang<=alang all from aoffset to aoffset+alength
261: bool check_lang(Language alang, size_t aoffset, size_t alength) const {
262: if(alang==L_UNSPECIFIED) // ignore lang?
263: return true;
264:
1.147 paf 265: if(opt.is_not_just_lang)
1.146 paf 266: return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
267: else
1.169 misha 268: return (unsigned)opt.lang<=(unsigned)alang;
1.146 paf 269: }
270:
1.148 paf 271: /// @returns count of blocks
272: /// @todo currently there can be adjucent blocks of same language. someday merge them
273: size_t count() const {
274: return opt.is_not_just_lang?
275: CORD_block_count(langs)
276: : opt.lang?
277: 1
278: : 0;
279: };
280:
1.187 misha 281: template<typename I>
282: void for_each(size_t current, int callback(char, size_t, I), I info) const {
283: if(opt.is_not_just_lang)
284: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
285: else
286: callback(opt.lang, current, info);
287: }
288:
289: template<typename I>
290: void for_each(const Body& current, int callback(char, size_t, I), I info) const {
1.147 paf 291: if(opt.is_not_just_lang)
1.146 paf 292: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
293: else
1.187 misha 294: callback(opt.lang, current.length(), info);
1.146 paf 295: }
296:
1.164 paf 297: bool invariant(size_t current_length) const {
1.146 paf 298: if(!langs)
299: return current_length==0;
1.147 paf 300: if(opt.is_not_just_lang)
1.146 paf 301: return CORD_len(langs)==current_length;
302: return true; // uncheckable, actually
303: }
304: };
305:
306: class Body {
307:
308: CORD body;
309:
1.183 misha 310: #ifdef HASH_CODE_CACHING
311: // cached hash code is not reseted on write operations as test shows
312: // that string body does not change after it is stored as a hash key
313: mutable uint hash_code;
314: #endif
315:
1.187 misha 316: #ifdef STRING_LENGTH_CACHING
317: // cached length is reseted on modification, used only for char*, not CORD
318: mutable size_t string_length;
319: #define INIT_LENGTH ,string_length(0)
320: #define ZERO_LENGTH string_length=0;
321: #else
322: #define INIT_LENGTH
323: #define ZERO_LENGTH
324: #endif
325:
1.146 paf 326: public:
327:
328: const char* v() const;
1.164 paf 329: void dump() const;
1.146 paf 330:
1.183 misha 331: #ifdef HASH_CODE_CACHING
1.187 misha 332: Body(): body(CORD_EMPTY), hash_code(0) INIT_LENGTH {}
333: Body(CORD abody, uint ahash_code): body(abody), hash_code(ahash_code) INIT_LENGTH {}
334: Body(CORD abody): body(abody), hash_code(0) INIT_LENGTH {
1.183 misha 335: #else
1.187 misha 336: Body(): body(CORD_EMPTY) INIT_LENGTH {}
337: Body(CORD abody): body(abody) INIT_LENGTH {
1.183 misha 338: #endif
1.186 misha 339: #ifdef CORD_CAT_OPTIMIZATION
340: assert(!body // no body
341: || *body // ordinary string
342: || body[1]==1 // CONCAT_HDR
343: || body[1]==3 // CONCAT_HDR_READ_ONLY
344: || body[1]==4 // FN_HDR
345: || body[1]==6 // SUBSTR_HDR
346: );
347: #else
1.146 paf 348: assert(!body // no body
349: || *body // ordinary string
350: || body[1]==1 // CONCAT_HDR
351: || body[1]==4 // FN_HDR
352: || body[1]==6 // SUBSTR_HDR
353: );
1.186 misha 354: #endif
1.146 paf 355: }
1.181 misha 356:
1.146 paf 357: static Body Format(int value);
358:
1.187 misha 359: void clear() { ZERO_LENGTH body=CORD_EMPTY; }
1.146 paf 360:
361: bool operator! () const { return is_empty(); }
362:
1.183 misha 363: CORD get_cord() const { return body; }
364: uint get_hash_code() const;
1.146 paf 365:
1.197 misha 366: const char* cstr() const {
367: #ifdef STRING_LENGTH_CACHING
368: string_length = length();
369: if(string_length)
370: return const_cast<Body*>(this)->body=CORD_to_const_char_star(body, string_length);
371: #endif
372: return CORD_to_const_char_star(body, length());
373: }
374:
1.194 misha 375: char* cstrm() const { return CORD_to_char_star(body, length()); }
1.146 paf 376:
1.187 misha 377: #ifdef STRING_LENGTH_CACHING
378: void set_length(size_t alength){ string_length = alength; }
379: size_t length() const { return body ? CORD_IS_STRING(body) ? string_length ? string_length : (string_length=strlen(body)) : CORD_len(body) : 0; }
380: #else
1.146 paf 381: size_t length() const { return CORD_len(body); }
1.187 misha 382: #endif
1.146 paf 383:
384: bool is_empty() const { return body==CORD_EMPTY; }
385:
386: void append_know_length(const char *str, size_t known_length) {
1.187 misha 387: if(known_length){
388: if(body){
389: body = CORD_cat_char_star(body, str, known_length);
390: ZERO_LENGTH
391: } else {
392: body=str;
393: #ifdef STRING_LENGTH_CACHING
394: string_length=known_length;
395: #endif
396: }
397: }
1.146 paf 398: }
399: void append_strdup_know_length(const char* str, size_t known_length) {
400: if(known_length)
401: append_know_length(pa_strdup(str, known_length), known_length);
402: }
1.187 misha 403: void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); }
404: Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; }
405:
1.146 paf 406: Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
407:
408: // could not figure out why this operator is needed [should do this chain: string->simple->==]
409: bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
410: bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
411: bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
412: bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
413: bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
414: bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
415:
416: int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
417: return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145 paf 418: }
419:
1.146 paf 420: char fetch(size_t index) const { return CORD_fetch(body, index); }
1.194 misha 421: Body mid(size_t aindex, size_t alength) const { return CORD_substr(body, aindex, alength, length()); }
422: size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr, length()); }
1.146 paf 423: size_t pos(const Body substr, size_t offset=0) const {
1.180 misha 424: if(substr.is_empty())
1.146 paf 425: return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150 paf 426:
427: // CORD_str checks for bad offset [CORD_chr does not]
1.194 misha 428: return CORD_str(body, offset, substr.body, length());
1.146 paf 429: }
430: size_t pos(char c,
431: size_t offset=0) const {
1.150 paf 432: if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
433: return STRING_NOT_FOUND;
434:
1.146 paf 435: return CORD_chr(body, offset, c);
1.145 paf 436: }
1.146 paf 437:
1.203 ! misha 438: size_t strrpbrk(const char* chars, size_t left, size_t right) const;
! 439:
! 440: size_t rskipchars(const char* chars, size_t left, size_t right) const;
! 441:
1.187 misha 442: template<typename I>
443: int for_each(int (*f)(char c, I), I info) const {
1.159 paf 444: return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
445: }
446:
1.187 misha 447: template<typename I>
448: int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const {
1.159 paf 449: return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148 paf 450: }
1.146 paf 451:
452: void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
453:
454: /*Body normalize() const {
455: return Body(CORD_balance(body));
456: }*/
1.157 paf 457:
458: /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
459: Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
1.188 misha 460: size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const;
1.145 paf 461: };
462:
463: struct C {
464: const char *str;
465: size_t length;
466: operator const char *() { return str; }
1.158 paf 467: C(): str(0), length(0) {}
1.145 paf 468: C(const char *astr, size_t asize): str(astr), length(asize) {}
469: };
470:
471: struct Cm {
472: char *str;
473: size_t length;
474: //operator char *() { return str; }
1.158 paf 475: Cm(): str(0), length(0) {}
1.145 paf 476: Cm(char *astr, size_t asize): str(astr), length(asize) {}
477: };
478:
479: private:
480:
1.152 paf 481: Body body; ///< all characters of string
1.146 paf 482: Languages langs; ///< string characters lang
483:
484: const char* v() const;
1.164 paf 485: void dump() const;
486: #define ASSERT_STRING_INVARIANT(string) \
487: assert((string).langs.invariant((string).body.length()))
1.145 paf 488:
1.8 paf 489: public:
1.151 paf 490:
491: static const String Empty;
1.8 paf 492:
1.181 misha 493: explicit String(){};
1.182 misha 494: explicit String(const char* cstr, Language alang=L_CLEAN){
1.181 misha 495: if(cstr && *cstr){
496: body=cstr;
1.182 misha 497: langs=alang;
1.181 misha 498: }
499: }
1.187 misha 500: explicit String(const char* cstr, Language alang, size_t alength){
501: if(cstr && *cstr){
502: body=cstr;
503: #ifdef STRING_LENGTH_CACHING
504: body.set_length(alength);
505: #endif
1.182 misha 506: langs=alang;
1.181 misha 507: }
508: }
1.187 misha 509:
1.185 misha 510: String(int value, char *format);
1.146 paf 511: String(Body abody, Language alang): body(abody), langs(alang) {
512: ASSERT_STRING_INVARIANT(*this);
513: }
514: String(const String& src): body(src.body), langs(src.langs) {
515: ASSERT_STRING_INVARIANT(*this);
1.145 paf 516: }
517:
518: /// for convinient hash lookup
1.183 misha 519: #ifdef HASH_CODE_CACHING
520: operator const Body&() const { return body; }
521: #else
1.146 paf 522: operator const Body() const { return body; }
1.183 misha 523: #endif
1.145 paf 524:
525: bool is_empty() const { return body.is_empty(); }
526: size_t length() const { return body.length(); }
1.171 misha 527: size_t length(Charset& charset) const;
1.145 paf 528:
1.191 misha 529: /// convert to CORD forcing lang tainting
530: Body cstr_to_string_body_taint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
531: /// convert to CORD with tainting dirty to lang
532: Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
1.145 paf 533:
1.189 misha 534: ///
535: const char* cstr() const {
536: return body.cstr();
537: }
538: ///
539: char* cstrm() const {
540: return body.cstrm();
541: }
542:
543: /// convert to constant C string forcing lang tainting
1.190 misha 544: const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 545: return cstr_to_string_body_taint(lang, connection, charsets).cstr();
546: }
1.190 misha 547: char *taint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 548: return cstr_to_string_body_taint(lang, connection, charsets).cstrm();
549: }
550:
551: /// convert to constant C string with tainting dirty to lang
1.190 misha 552: const char* untaint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 553: return cstr_to_string_body_untaint(lang, connection, charsets).cstr();
554: }
1.190 misha 555: char *untaint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 556: return cstr_to_string_body_untaint(lang, connection, charsets).cstrm();
557: }
1.195 misha 558:
1.196 misha 559: const char* untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const;
1.195 misha 560:
1.202 moko 561: bool is_not_just_lang() const {
562: return langs.opt.is_not_just_lang !=0;
563: }
564:
565: Language just_lang() const {
566: return langs.opt.lang;
567: }
568:
1.108 parser 569: /// puts pieces to buf
1.145 paf 570: Cm serialize(size_t prolog_size) const;
1.108 parser 571: /// appends pieces from buf to self
1.145 paf 572: bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146 paf 573: /// @see Body::append_know_length
1.145 paf 574: String& append_know_length(const char* str, size_t known_length, Language lang);
1.146 paf 575: /// @see Body::append_help_length
1.145 paf 576: String& append_help_length(const char* str, size_t helper_length, Language lang);
577: String& append_strdup(const char* str, size_t helper_length, Language lang);
578:
1.146 paf 579: bool operator == (const char* y) const { return body==Body(y); }
580: bool operator != (const char* y) const { return body!=Body(y); }
1.145 paf 581:
582: /// this starts with y
583: bool starts_with(const char* y) const {
1.146 paf 584: return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145 paf 585: }
586: /// x starts with this
587: bool this_starts(const char* x) const {
1.146 paf 588: return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26 paf 589: }
590:
1.187 misha 591: String& append_to(String& dest, Language lang, bool forced=false) const;
1.145 paf 592: String& append(const String& src, Language lang, bool forced=false) {
593: return src.append_to(*this, lang, forced);
594: }
1.199 misha 595: String& append_quoted(const String* src, Language lang=L_JSON){
596: *this << "\"";
597: if(src)
598: this->append(*src, lang, true/*forced lang*/);
599: *this << "\"";
600: return *this;
601: }
602:
1.145 paf 603: String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
604: String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.187 misha 605: String& operator << (const Body& src){
606: langs.appendHelper(body, L_AS_IS, src);
607: body<<src;
608: return *this;
609: }
1.100 parser 610:
1.142 paf 611: /// extracts first char of a string, if any
612: char first_char() const {
1.145 paf 613: return is_empty()?0:body.fetch(0);
1.142 paf 614: }
1.54 paf 615:
1.145 paf 616: bool operator < (const String& src) const { return body<src.body; }
617: bool operator > (const String& src) const { return body>src.body; }
618: bool operator <= (const String& src) const { return body<=src.body; }
619: bool operator >= (const String& src) const { return body>=src.body; }
620: bool operator != (const String& src) const { return body!=src.body; }
621: bool operator == (const String& src) const { return body==src.body; }
622:
1.54 paf 623: /// extracts [start, finish) piece of string
1.145 paf 624: String& mid(size_t substr_begin, size_t substr_end) const;
1.172 misha 625: String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const;
1.145 paf 626:
627: /**
628: ignore lang if it's L_UNSPECIFIED
629: but when specified: look for substring that lies in ONE fragment in THAT lang
630: @return position of substr in string, -1 means "not found" [const char* version]
631: */
1.146 paf 632: size_t pos(const Body substr,
1.145 paf 633: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
634: /// String version of @see pos(const char*, int, Language)
635: size_t pos(const String& substr,
636: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
637: size_t pos(char c,
638: size_t this_offset=0) const {
639: return body.pos(c, this_offset);
640: }
1.173 misha 641: size_t pos(Charset& charset,
1.171 misha 642: const String& substr,
643: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55 paf 644:
1.203 ! misha 645: size_t strrpbrk(const char* chars, size_t left=0) const {
! 646: return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND;
! 647: }
! 648: size_t strrpbrk(const char* chars, size_t left, size_t right) const {
! 649: return body.strrpbrk(chars, left, right);
! 650: }
! 651:
! 652: size_t rskipchars(const char* chars, size_t left=0) const {
! 653: return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND;
! 654: }
! 655: size_t rskipchars(const char* chars, size_t left, size_t right) const {
! 656: return body.rskipchars(chars, left, right);
! 657: }
! 658:
1.145 paf 659: void split(ArrayString& result,
660: size_t& pos_after,
661: const char* delim,
662: Language lang=L_UNSPECIFIED, int limit=-1) const;
663: void split(ArrayString& result,
664: size_t& pos_after,
1.62 paf 665: const String& delim,
1.145 paf 666: Language lang=L_UNSPECIFIED, int limit=-1) const;
1.62 paf 667:
1.145 paf 668: typedef void (*Row_action)(Table& table, ArrayString* row,
1.136 paf 669: int prestart, int prefinish,
670: int poststart, int postfinish,
1.68 paf 671: void *info);
1.87 parser 672: /**
1.145 paf 673: @return table of found items, if any.
1.87 parser 674: table format is defined and fixed[can be used by others]:
675: @verbatim
676: prematch/match/postmatch/1/2/3/...
677: @endverbatim
678: */
1.178 misha 679: Table* match(VRegex* vregex,
1.99 parser 680: Row_action row_action, void *info,
1.168 misha 681: int& matches_count) const;
1.87 parser 682: enum Change_case_kind {
683: CC_UPPER,
684: CC_LOWER
685: };
1.145 paf 686: String& change_case(Charset& source_charset,
1.87 parser 687: Change_case_kind kind) const;
1.145 paf 688: const String& replace(const Dictionary& dict) const;
1.188 misha 689: const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const;
1.155 paf 690: double as_double() const { return pa_atod(cstr(), this); }
691: int as_int() const { return pa_atoi(cstr(), this); }
1.167 misha 692: bool as_bool() const { return as_int()!=0; }
1.174 misha 693: const String& escape(Charset& source_charset) const;
1.137 paf 694:
1.7 paf 695: private: //disabled
696:
1.12 paf 697: String& operator = (const String&) { return *this; }
1.7 paf 698:
1.1 paf 699: };
1.119 paf 700:
1.183 misha 701: #ifndef HASH_CODE_CACHING
1.145 paf 702: /// simple hash code of string. used by Hash
1.146 paf 703: inline uint hash_code(const String::Body self) {
1.184 misha 704: return self.get_hash_code();
1.119 paf 705: }
1.183 misha 706: #endif
1.147 paf 707:
708:
1.1 paf 709: #endif
E-mail: