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