Annotation of parser3/src/include/pa_string.h, revision 1.214
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.214 ! moko 11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.213 2015/10/09 11:42:38 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.1 paf 76: public:
1.48 paf 77:
1.146 paf 78: /** piece is tainted or not. the lang to use when detaint
1.106 parser 79: remember to change String_Untaint_lang_name @ untaint.C along
1.148 paf 80:
81: WARNING WARNING WARNING WARNING WARNING WARNING
82:
83: pos function compares(<=) languages, that is used in searching
84: for table column separator being L_CLEAN or L_AS_IS.
85: they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
86:
87: letters assigned for debugging, but it's important for no language-letter
88: come before L_AS_IS other then L_CLEAN
89:
90: WARNING WARNING WARNING WARNING WARNING WARNING
1.106 parser 91: */
1.145 paf 92: enum Language {
1.146 paf 93: L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145 paf 94: // these two must go before others, there are checks for >L_AS_IS
1.170 misha 95: L_CLEAN='0', ///< clean WARNING: read above warning before changing
96: L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing
1.122 paf 97:
1.148 paf 98: L_PASS_APPENDED='P',
1.41 paf 99: /**<
1.146 paf 100: leave lang built into string being appended.
1.41 paf 101: just a flag, that value not stored
102: */
1.170 misha 103: L_TAINTED='T', ///< tainted, untaint lang as assigned later
1.146 paf 104: // untaint langs. assigned by ^untaint[lang]{...}
1.170 misha 105: L_FILE_SPEC='F', ///< file specification
106: L_HTTP_HEADER='h', ///< text in HTTP response header
107: L_MAIL_HEADER='m', ///< text in mail header
1.192 misha 108: L_URI='U', ///< text in uri
109: L_SQL='Q', ///< ^table:sql body
110: L_JS='J', ///< JavaScript code
1.198 misha 111: L_XML='X', ///< ^xdoc:create xml
1.192 misha 112: L_HTML='H', ///< HTML code
1.198 misha 113: L_REGEX='R', ///< RegExp
114: L_JSON='S', ///< JSON code
1.170 misha 115: L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode
1.192 misha 116: L_PARSER_CODE='p', ///< ^process body
1.148 paf 117: // READ WARNING ABOVE BEFORE ADDING ANYTHING
1.146 paf 118: L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization
1.27 paf 119: };
120:
1.157 paf 121: enum Trim_kind {
122: TRIM_BOTH,
123: TRIM_START,
124: TRIM_END
125: };
126:
1.187 misha 127: class Body;
1.176 misha 128:
1.147 paf 129: union Languages {
1.146 paf 130:
1.147 paf 131: struct {
1.160 paf 132: #ifdef PA_LITTLE_ENDIAN
1.147 paf 133: Language lang:8;
1.179 misha 134: size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 135: #elif defined(PA_BIG_ENDIAN)
1.179 misha 136: size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 137: Language lang:8;
138: #else
139: # error word endianness not determined for some obscure reason
140: #endif
1.147 paf 141: } opt;
142: CORD langs;
1.146 paf 143:
1.187 misha 144: CORD make_langs(const Body& current) const {
145: return opt.is_not_just_lang?langs:CORD_chars((char)opt.lang, current.length());
1.146 paf 146: }
147:
148: CORD make_langs(size_t aoffset, size_t alength) const {
1.147 paf 149: return opt.is_not_just_lang?
1.194 misha 150: CORD_substr(langs, aoffset, alength, 0)
1.162 paf 151: :CORD_chars((char)opt.lang, alength);
1.145 paf 152: }
153:
1.157 paf 154: /// appending when 'langs' already contain something [simple cases handled elsewhere]
1.187 misha 155: void append(size_t current, const CORD to_nonempty_target_langs) {
1.146 paf 156: assert(langs);
1.187 misha 157: if(opt.is_not_just_lang)
158: langs=CORD_cat(langs, to_nonempty_target_langs);
159: else {
160: assert(current);
161: langs=CORD_cat(CORD_chars((char)opt.lang, current), to_nonempty_target_langs);
162: }
163: }
1.146 paf 164:
1.187 misha 165: void append(const Body& current, const CORD to_nonempty_target_langs) {
166: assert(langs);
1.147 paf 167: if(opt.is_not_just_lang)
1.146 paf 168: langs=CORD_cat(langs, to_nonempty_target_langs);
1.187 misha 169: else {
170: size_t current_size=current.length();
1.146 paf 171: assert(current_size);
1.187 misha 172: langs=CORD_cat(CORD_chars((char)opt.lang, current_size), to_nonempty_target_langs);
1.146 paf 173: }
1.145 paf 174: }
1.146 paf 175:
1.145 paf 176: public:
1.146 paf 177:
178: const char* v() const;
1.164 paf 179: void dump() const;
1.146 paf 180:
181: Languages(): langs(0) {}
1.147 paf 182: Languages(Language alang) {
183: opt.lang=alang;
184: opt.is_not_just_lang=0;
185: }
1.146 paf 186:
187: /// MUST be called exactly prior to modification of current [uses it's length]
1.187 misha 188: void append(size_t current, Language alang, size_t length) {
1.146 paf 189: assert(alang);
1.187 misha 190: assert(length);
1.146 paf 191:
1.208 moko 192: if(!opt.is_not_just_lang) {
1.147 paf 193: if(opt.lang) {
1.181 misha 194: if(opt.lang==alang) // same language? ignoring
1.146 paf 195: return;
196: } else {
1.147 paf 197: opt.lang=alang; // to uninitialized
1.146 paf 198: return;
1.145 paf 199: }
1.208 moko 200: }
1.146 paf 201:
1.187 misha 202: append(current, CORD_chars((char)alang, length));
1.146 paf 203: }
204:
1.187 misha 205: void append(const Body ¤t, Language alang, size_t length) {
1.176 misha 206: assert(alang);
1.187 misha 207: assert(length);
1.176 misha 208:
1.208 moko 209: if(!opt.is_not_just_lang) {
1.176 misha 210: if(opt.lang) {
1.183 misha 211: if(opt.lang==alang) // same language? ignoring
1.176 misha 212: return;
213: } else {
214: opt.lang=alang; // to uninitialized
215: return;
216: }
1.208 moko 217: }
1.176 misha 218:
1.187 misha 219: append(current, CORD_chars((char)alang, length));
1.176 misha 220: }
221:
1.187 misha 222: void appendHelper(const Body& current, Language alang, const Body &length_helper) {
223: assert(alang);
224:
1.208 moko 225: if(!opt.is_not_just_lang) {
1.187 misha 226: if(opt.lang) {
227: if(opt.lang==alang) // same language? ignoring
228: return;
229: } else {
230: opt.lang=alang; // to uninitialized
231: return;
232: }
1.208 moko 233: }
1.187 misha 234:
235: append(current, CORD_chars((char)alang, length_helper.length()));
236: }
237:
238: void appendHelper(const Body& current, const Languages &src, const Body& length_helper) {
1.186 misha 239: if(!langs){
1.176 misha 240: langs=src.langs; // to uninitialized
1.186 misha 241: #ifdef CORD_CAT_OPTIMIZATION
242: if(opt.is_not_just_lang && !CORD_IS_STRING(langs))
243: CORD_concatenation_protect(langs);
244: #endif
245: }
1.176 misha 246: else if(!src.opt.is_not_just_lang)
247: appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source
248: else
249: append(current, src.make_langs(length_helper));
250: }
251:
1.146 paf 252: /// MUST be called exactly prior to modification of current [uses it's length]
1.187 misha 253: void append(const Body& current, const Languages src, size_t aoffset, size_t alength) {
1.146 paf 254: assert(alength);
255:
256: if(!langs) // to uninitialized?
1.147 paf 257: if(src.opt.is_not_just_lang)
1.194 misha 258: langs=CORD_substr(src.langs, aoffset, alength, 0); // to uninitialized complex
1.146 paf 259: else
1.147 paf 260: opt.lang=src.opt.lang; // to uninitialized simple
1.146 paf 261: else
1.147 paf 262: 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 263: return; // ignoring
264: else
265: append(current, src.make_langs(aoffset, alength));
266: }
267:
268: /// checks if we have lang<=alang all from aoffset to aoffset+alength
269: bool check_lang(Language alang, size_t aoffset, size_t alength) const {
270: if(alang==L_UNSPECIFIED) // ignore lang?
271: return true;
272:
1.147 paf 273: if(opt.is_not_just_lang)
1.146 paf 274: return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
275: else
1.169 misha 276: return (unsigned)opt.lang<=(unsigned)alang;
1.146 paf 277: }
278:
1.148 paf 279: /// @returns count of blocks
280: /// @todo currently there can be adjucent blocks of same language. someday merge them
281: size_t count() const {
282: return opt.is_not_just_lang?
283: CORD_block_count(langs)
284: : opt.lang?
285: 1
286: : 0;
287: };
288:
1.187 misha 289: template<typename I>
290: void for_each(size_t current, int callback(char, size_t, I), I info) const {
291: if(opt.is_not_just_lang)
292: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
293: else
294: callback(opt.lang, current, info);
295: }
296:
297: template<typename I>
298: void for_each(const Body& current, int callback(char, size_t, I), I info) const {
1.147 paf 299: if(opt.is_not_just_lang)
1.146 paf 300: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
301: else
1.187 misha 302: callback(opt.lang, current.length(), info);
1.146 paf 303: }
304:
1.164 paf 305: bool invariant(size_t current_length) const {
1.146 paf 306: if(!langs)
307: return current_length==0;
1.147 paf 308: if(opt.is_not_just_lang)
1.146 paf 309: return CORD_len(langs)==current_length;
310: return true; // uncheckable, actually
311: }
312: };
313:
1.213 moko 314: struct C {
315: const char *str;
316: size_t length;
317: C(): str(0), length(0) {}
318: C(const char *astr, size_t asize): str(astr), length(asize) {}
319: };
320:
321: struct Cm {
322: char *str;
323: size_t length;
324: Cm(): str(0), length(0) {}
325: Cm(char *astr, size_t asize): str(astr), length(asize) {}
326: };
327:
1.146 paf 328: class Body {
329:
330: CORD body;
331:
1.183 misha 332: #ifdef HASH_CODE_CACHING
333: // cached hash code is not reseted on write operations as test shows
334: // that string body does not change after it is stored as a hash key
335: mutable uint hash_code;
1.213 moko 336: #define INIT_HASH_CODE(c) ,hash_code(c)
337: #else
338: #define INIT_HASH_CODE(c)
1.183 misha 339: #endif
340:
1.187 misha 341: #ifdef STRING_LENGTH_CACHING
342: // cached length is reseted on modification, used only for char*, not CORD
343: mutable size_t string_length;
1.213 moko 344: #define INIT_LENGTH(l) ,string_length(l)
1.187 misha 345: #define ZERO_LENGTH string_length=0;
346: #else
1.213 moko 347: #define INIT_LENGTH(l)
1.187 misha 348: #define ZERO_LENGTH
349: #endif
350:
1.146 paf 351: public:
352:
353: const char* v() const;
1.164 paf 354: void dump() const;
1.146 paf 355:
1.213 moko 356: Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
357: Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
358: Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {}
359: explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {}
360: explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) {
1.186 misha 361: #ifdef CORD_CAT_OPTIMIZATION
362: assert(!body // no body
363: || *body // ordinary string
364: || body[1]==1 // CONCAT_HDR
365: || body[1]==3 // CONCAT_HDR_READ_ONLY
366: || body[1]==4 // FN_HDR
367: || body[1]==6 // SUBSTR_HDR
368: );
369: #else
1.146 paf 370: assert(!body // no body
371: || *body // ordinary string
372: || body[1]==1 // CONCAT_HDR
373: || body[1]==4 // FN_HDR
374: || body[1]==6 // SUBSTR_HDR
375: );
1.186 misha 376: #endif
1.146 paf 377: }
1.181 misha 378:
1.213 moko 379:
1.146 paf 380: static Body Format(int value);
381:
1.187 misha 382: void clear() { ZERO_LENGTH body=CORD_EMPTY; }
1.146 paf 383:
384: bool operator! () const { return is_empty(); }
385:
1.211 moko 386: inline CORD get_cord() const { return body; }
1.183 misha 387: uint get_hash_code() const;
1.146 paf 388:
1.197 misha 389: const char* cstr() const {
390: #ifdef STRING_LENGTH_CACHING
391: string_length = length();
1.211 moko 392: if(string_length){
393: const char *result=CORD_to_const_char_star(body, string_length);
394: const_cast<Body*>(this)->body=(CORD)result;
395: return result;
396: }
1.197 misha 397: #endif
398: return CORD_to_const_char_star(body, length());
399: }
400:
1.194 misha 401: char* cstrm() const { return CORD_to_char_star(body, length()); }
1.146 paf 402:
1.187 misha 403: #ifdef STRING_LENGTH_CACHING
404: void set_length(size_t alength){ string_length = alength; }
1.211 moko 405: 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 406: #else
1.146 paf 407: size_t length() const { return CORD_len(body); }
1.187 misha 408: #endif
1.146 paf 409:
1.212 moko 410: inline bool is_empty() const { return body==CORD_EMPTY; }
1.146 paf 411:
412: void append_know_length(const char *str, size_t known_length) {
1.187 misha 413: if(known_length){
414: if(body){
415: body = CORD_cat_char_star(body, str, known_length);
416: ZERO_LENGTH
417: } else {
1.211 moko 418: body=(CORD)str;
1.187 misha 419: #ifdef STRING_LENGTH_CACHING
420: string_length=known_length;
421: #endif
422: }
423: }
1.146 paf 424: }
425: void append_strdup_know_length(const char* str, size_t known_length) {
426: if(known_length)
427: append_know_length(pa_strdup(str, known_length), known_length);
428: }
1.187 misha 429: void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); }
430: Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; }
431:
1.146 paf 432: Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
433:
434: bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
435: bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
436: bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
437: bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
1.207 moko 438:
1.146 paf 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; }
441:
1.211 moko 442: bool operator != (const char *src) const { return CORD_cmp(body, AS_CORD(src))!=0; }
443: bool operator == (const char *src) const { return CORD_cmp(body, AS_CORD(src))==0; }
1.207 moko 444:
1.146 paf 445: int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
446: return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145 paf 447: }
448:
1.146 paf 449: char fetch(size_t index) const { return CORD_fetch(body, index); }
1.211 moko 450: Body mid(size_t aindex, size_t alength) const { return Body(CORD_substr(body, aindex, alength, length())); }
451: size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, AS_CORD(substr), length()); }
1.146 paf 452: size_t pos(const Body substr, size_t offset=0) const {
1.180 misha 453: if(substr.is_empty())
1.146 paf 454: return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150 paf 455:
456: // CORD_str checks for bad offset [CORD_chr does not]
1.194 misha 457: return CORD_str(body, offset, substr.body, length());
1.146 paf 458: }
1.207 moko 459: size_t pos(char c, size_t offset=0) const {
1.150 paf 460: if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
461: return STRING_NOT_FOUND;
462:
1.146 paf 463: return CORD_chr(body, offset, c);
1.145 paf 464: }
1.146 paf 465:
1.203 misha 466: size_t strrpbrk(const char* chars, size_t left, size_t right) const;
467:
468: size_t rskipchars(const char* chars, size_t left, size_t right) const;
469:
1.187 misha 470: template<typename I>
471: int for_each(int (*f)(char c, I), I info) const {
1.159 paf 472: return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
473: }
474:
1.187 misha 475: template<typename I>
476: int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const {
1.159 paf 477: return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148 paf 478: }
1.146 paf 479:
480: void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
481:
1.157 paf 482: /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
483: Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
1.188 misha 484: size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const;
1.145 paf 485: };
486:
487: private:
488:
1.152 paf 489: Body body; ///< all characters of string
1.146 paf 490: Languages langs; ///< string characters lang
491:
492: const char* v() const;
1.164 paf 493: void dump() const;
494: #define ASSERT_STRING_INVARIANT(string) \
495: assert((string).langs.invariant((string).body.length()))
1.145 paf 496:
1.8 paf 497: public:
1.151 paf 498:
499: static const String Empty;
1.8 paf 500:
1.181 misha 501: explicit String(){};
1.211 moko 502: explicit String(const char* cstr, Language alang=L_CLEAN) : body(cstr){
503: if(body.get_cord()){
1.182 misha 504: langs=alang;
1.181 misha 505: }
506: }
1.213 moko 507: explicit String(C ac, Language alang=L_CLEAN) : body(ac){
1.211 moko 508: if(body.get_cord()){
509: langs=alang;
510: }
511: }
1.146 paf 512: String(Body abody, Language alang): body(abody), langs(alang) {
513: ASSERT_STRING_INVARIANT(*this);
514: }
515: String(const String& src): body(src.body), langs(src.langs) {
516: ASSERT_STRING_INVARIANT(*this);
1.145 paf 517: }
1.212 moko 518: String(int value, const char *format);
1.145 paf 519:
520: /// for convinient hash lookup
1.183 misha 521: #ifdef HASH_CODE_CACHING
522: operator const Body&() const { return body; }
523: #else
1.146 paf 524: operator const Body() const { return body; }
1.183 misha 525: #endif
1.145 paf 526:
527: bool is_empty() const { return body.is_empty(); }
528: size_t length() const { return body.length(); }
1.171 misha 529: size_t length(Charset& charset) const;
1.145 paf 530:
1.191 misha 531: /// convert to CORD forcing lang tainting
532: Body cstr_to_string_body_taint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
533: /// convert to CORD with tainting dirty to lang
534: Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
1.145 paf 535:
1.212 moko 536: /// from body
537: const char* cstr() const { return body.cstr(); }
538: char* cstrm() const { return body.cstrm(); }
1.189 misha 539:
540: /// convert to constant C string forcing lang tainting
1.190 misha 541: const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 542: return cstr_to_string_body_taint(lang, connection, charsets).cstr();
543: }
1.190 misha 544: char *taint_cstrm(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).cstrm();
546: }
547:
548: /// convert to constant C string with tainting dirty to lang
1.190 misha 549: const char* untaint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 550: return cstr_to_string_body_untaint(lang, connection, charsets).cstr();
551: }
1.190 misha 552: char *untaint_cstrm(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).cstrm();
554: }
1.195 misha 555:
1.196 misha 556: const char* untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const;
1.195 misha 557:
1.202 moko 558: bool is_not_just_lang() const {
559: return langs.opt.is_not_just_lang !=0;
560: }
561:
562: Language just_lang() const {
563: return langs.opt.lang;
564: }
565:
1.108 parser 566: /// puts pieces to buf
1.145 paf 567: Cm serialize(size_t prolog_size) const;
1.108 parser 568: /// appends pieces from buf to self
1.145 paf 569: bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146 paf 570: /// @see Body::append_know_length
1.145 paf 571: String& append_know_length(const char* str, size_t known_length, Language lang);
1.146 paf 572: /// @see Body::append_help_length
1.145 paf 573: String& append_help_length(const char* str, size_t helper_length, Language lang);
574: String& append_strdup(const char* str, size_t helper_length, Language lang);
575:
1.146 paf 576: bool operator == (const char* y) const { return body==Body(y); }
577: bool operator != (const char* y) const { return body!=Body(y); }
1.145 paf 578:
579: /// this starts with y
580: bool starts_with(const char* y) const {
1.146 paf 581: return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145 paf 582: }
583: /// x starts with this
584: bool this_starts(const char* x) const {
1.146 paf 585: return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26 paf 586: }
587:
1.187 misha 588: String& append_to(String& dest, Language lang, bool forced=false) const;
1.145 paf 589: String& append(const String& src, Language lang, bool forced=false) {
590: return src.append_to(*this, lang, forced);
591: }
1.199 misha 592: String& append_quoted(const String* src, Language lang=L_JSON){
593: *this << "\"";
594: if(src)
595: this->append(*src, lang, true/*forced lang*/);
596: *this << "\"";
597: return *this;
598: }
599:
1.145 paf 600: String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
601: String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.187 misha 602: String& operator << (const Body& src){
603: langs.appendHelper(body, L_AS_IS, src);
604: body<<src;
605: return *this;
606: }
1.100 parser 607:
1.142 paf 608: char first_char() const {
1.145 paf 609: return is_empty()?0:body.fetch(0);
1.142 paf 610: }
1.54 paf 611:
1.205 moko 612: char last_char() const {
613: return is_empty()?0:body.fetch(body.length()-1);
614: }
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: