Annotation of parser3/src/include/pa_string.h, revision 1.215
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.215 ! moko 11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.214 2015/10/26 01:21:56 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)
1.215 ! moko 337: #define ZERO_HASH_CODE hash_code=0;
1.213 moko 338: #else
339: #define INIT_HASH_CODE(c)
1.215 ! moko 340: #define ZERO_HASH_CODE
1.183 misha 341: #endif
342:
1.187 misha 343: #ifdef STRING_LENGTH_CACHING
344: // cached length is reseted on modification, used only for char*, not CORD
345: mutable size_t string_length;
1.213 moko 346: #define INIT_LENGTH(l) ,string_length(l)
1.187 misha 347: #define ZERO_LENGTH string_length=0;
348: #else
1.213 moko 349: #define INIT_LENGTH(l)
1.187 misha 350: #define ZERO_LENGTH
351: #endif
352:
1.146 paf 353: public:
354:
355: const char* v() const;
1.164 paf 356: void dump() const;
1.146 paf 357:
1.213 moko 358: Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
359: Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
360: Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {}
361: explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {}
362: explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) {
1.186 misha 363: #ifdef CORD_CAT_OPTIMIZATION
364: assert(!body // no body
365: || *body // ordinary string
366: || body[1]==1 // CONCAT_HDR
367: || body[1]==3 // CONCAT_HDR_READ_ONLY
368: || body[1]==4 // FN_HDR
369: || body[1]==6 // SUBSTR_HDR
370: );
371: #else
1.146 paf 372: assert(!body // no body
373: || *body // ordinary string
374: || body[1]==1 // CONCAT_HDR
375: || body[1]==4 // FN_HDR
376: || body[1]==6 // SUBSTR_HDR
377: );
1.186 misha 378: #endif
1.146 paf 379: }
1.181 misha 380:
1.213 moko 381:
1.146 paf 382: static Body Format(int value);
383:
1.215 ! moko 384: void clear() { ZERO_LENGTH ZERO_HASH_CODE body=CORD_EMPTY; }
1.146 paf 385:
386: bool operator! () const { return is_empty(); }
387:
1.211 moko 388: inline CORD get_cord() const { return body; }
1.183 misha 389: uint get_hash_code() const;
1.146 paf 390:
1.197 misha 391: const char* cstr() const {
392: #ifdef STRING_LENGTH_CACHING
393: string_length = length();
1.211 moko 394: if(string_length){
395: const char *result=CORD_to_const_char_star(body, string_length);
396: const_cast<Body*>(this)->body=(CORD)result;
397: return result;
398: }
1.197 misha 399: #endif
400: return CORD_to_const_char_star(body, length());
401: }
402:
1.194 misha 403: char* cstrm() const { return CORD_to_char_star(body, length()); }
1.146 paf 404:
1.187 misha 405: #ifdef STRING_LENGTH_CACHING
406: void set_length(size_t alength){ string_length = alength; }
1.211 moko 407: 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 408: #else
1.146 paf 409: size_t length() const { return CORD_len(body); }
1.187 misha 410: #endif
1.146 paf 411:
1.212 moko 412: inline bool is_empty() const { return body==CORD_EMPTY; }
1.146 paf 413:
414: void append_know_length(const char *str, size_t known_length) {
1.187 misha 415: if(known_length){
416: if(body){
417: body = CORD_cat_char_star(body, str, known_length);
418: ZERO_LENGTH
419: } else {
1.211 moko 420: body=(CORD)str;
1.187 misha 421: #ifdef STRING_LENGTH_CACHING
422: string_length=known_length;
423: #endif
424: }
425: }
1.146 paf 426: }
427: void append_strdup_know_length(const char* str, size_t known_length) {
428: if(known_length)
429: append_know_length(pa_strdup(str, known_length), known_length);
430: }
1.187 misha 431: void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); }
432: Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; }
433:
1.146 paf 434: Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
435:
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; }
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; }
1.207 moko 440:
1.146 paf 441: bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
442: bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
443:
1.211 moko 444: bool operator != (const char *src) const { return CORD_cmp(body, AS_CORD(src))!=0; }
445: bool operator == (const char *src) const { return CORD_cmp(body, AS_CORD(src))==0; }
1.207 moko 446:
1.146 paf 447: int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
448: return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145 paf 449: }
450:
1.146 paf 451: char fetch(size_t index) const { return CORD_fetch(body, index); }
1.211 moko 452: Body mid(size_t aindex, size_t alength) const { return Body(CORD_substr(body, aindex, alength, length())); }
453: size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, AS_CORD(substr), length()); }
1.146 paf 454: size_t pos(const Body substr, size_t offset=0) const {
1.180 misha 455: if(substr.is_empty())
1.146 paf 456: return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150 paf 457:
458: // CORD_str checks for bad offset [CORD_chr does not]
1.194 misha 459: return CORD_str(body, offset, substr.body, length());
1.146 paf 460: }
1.207 moko 461: size_t pos(char c, size_t offset=0) const {
1.150 paf 462: if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
463: return STRING_NOT_FOUND;
464:
1.146 paf 465: return CORD_chr(body, offset, c);
1.145 paf 466: }
1.146 paf 467:
1.203 misha 468: size_t strrpbrk(const char* chars, size_t left, size_t right) const;
469:
470: size_t rskipchars(const char* chars, size_t left, size_t right) const;
471:
1.187 misha 472: template<typename I>
473: int for_each(int (*f)(char c, I), I info) const {
1.159 paf 474: return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
475: }
476:
1.187 misha 477: template<typename I>
478: int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const {
1.159 paf 479: return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148 paf 480: }
1.146 paf 481:
482: void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
483:
1.157 paf 484: /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
485: Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
1.188 misha 486: size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const;
1.145 paf 487: };
488:
489: private:
490:
1.152 paf 491: Body body; ///< all characters of string
1.146 paf 492: Languages langs; ///< string characters lang
493:
494: const char* v() const;
1.164 paf 495: void dump() const;
496: #define ASSERT_STRING_INVARIANT(string) \
497: assert((string).langs.invariant((string).body.length()))
1.145 paf 498:
1.8 paf 499: public:
1.151 paf 500:
501: static const String Empty;
1.8 paf 502:
1.181 misha 503: explicit String(){};
1.211 moko 504: explicit String(const char* cstr, Language alang=L_CLEAN) : body(cstr){
505: if(body.get_cord()){
1.182 misha 506: langs=alang;
1.181 misha 507: }
508: }
1.213 moko 509: explicit String(C ac, Language alang=L_CLEAN) : body(ac){
1.211 moko 510: if(body.get_cord()){
511: langs=alang;
512: }
513: }
1.146 paf 514: String(Body abody, Language alang): body(abody), langs(alang) {
515: ASSERT_STRING_INVARIANT(*this);
516: }
517: String(const String& src): body(src.body), langs(src.langs) {
518: ASSERT_STRING_INVARIANT(*this);
1.145 paf 519: }
1.212 moko 520: String(int value, const char *format);
1.145 paf 521:
522: /// for convinient hash lookup
1.183 misha 523: #ifdef HASH_CODE_CACHING
524: operator const Body&() const { return body; }
525: #else
1.146 paf 526: operator const Body() const { return body; }
1.183 misha 527: #endif
1.145 paf 528:
529: bool is_empty() const { return body.is_empty(); }
530: size_t length() const { return body.length(); }
1.171 misha 531: size_t length(Charset& charset) const;
1.145 paf 532:
1.191 misha 533: /// convert to CORD forcing lang tainting
534: Body cstr_to_string_body_taint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
535: /// convert to CORD with tainting dirty to lang
536: Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
1.145 paf 537:
1.212 moko 538: /// from body
539: const char* cstr() const { return body.cstr(); }
540: char* cstrm() const { return body.cstrm(); }
1.189 misha 541:
542: /// convert to constant C string forcing lang tainting
1.190 misha 543: const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 544: return cstr_to_string_body_taint(lang, connection, charsets).cstr();
545: }
1.190 misha 546: char *taint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 547: return cstr_to_string_body_taint(lang, connection, charsets).cstrm();
548: }
549:
550: /// convert to constant C string with tainting dirty to lang
1.190 misha 551: const char* untaint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 552: return cstr_to_string_body_untaint(lang, connection, charsets).cstr();
553: }
1.190 misha 554: char *untaint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 555: return cstr_to_string_body_untaint(lang, connection, charsets).cstrm();
556: }
1.195 misha 557:
1.196 misha 558: const char* untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const;
1.195 misha 559:
1.202 moko 560: bool is_not_just_lang() const {
561: return langs.opt.is_not_just_lang !=0;
562: }
563:
564: Language just_lang() const {
565: return langs.opt.lang;
566: }
567:
1.108 parser 568: /// puts pieces to buf
1.145 paf 569: Cm serialize(size_t prolog_size) const;
1.108 parser 570: /// appends pieces from buf to self
1.145 paf 571: bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146 paf 572: /// @see Body::append_know_length
1.145 paf 573: String& append_know_length(const char* str, size_t known_length, Language lang);
1.146 paf 574: /// @see Body::append_help_length
1.145 paf 575: String& append_help_length(const char* str, size_t helper_length, Language lang);
576: String& append_strdup(const char* str, size_t helper_length, Language lang);
577:
1.146 paf 578: bool operator == (const char* y) const { return body==Body(y); }
579: bool operator != (const char* y) const { return body!=Body(y); }
1.145 paf 580:
581: /// this starts with y
582: bool starts_with(const char* y) const {
1.146 paf 583: return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145 paf 584: }
585: /// x starts with this
586: bool this_starts(const char* x) const {
1.146 paf 587: return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26 paf 588: }
589:
1.187 misha 590: String& append_to(String& dest, Language lang, bool forced=false) const;
1.145 paf 591: String& append(const String& src, Language lang, bool forced=false) {
592: return src.append_to(*this, lang, forced);
593: }
1.199 misha 594: String& append_quoted(const String* src, Language lang=L_JSON){
595: *this << "\"";
596: if(src)
597: this->append(*src, lang, true/*forced lang*/);
598: *this << "\"";
599: return *this;
600: }
601:
1.145 paf 602: String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
603: String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.187 misha 604: String& operator << (const Body& src){
605: langs.appendHelper(body, L_AS_IS, src);
606: body<<src;
607: return *this;
608: }
1.100 parser 609:
1.142 paf 610: char first_char() const {
1.145 paf 611: return is_empty()?0:body.fetch(0);
1.142 paf 612: }
1.54 paf 613:
1.205 moko 614: char last_char() const {
615: return is_empty()?0:body.fetch(body.length()-1);
616: }
617:
1.145 paf 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: bool operator != (const String& src) const { return body!=src.body; }
623: bool operator == (const String& src) const { return body==src.body; }
624:
1.54 paf 625: /// extracts [start, finish) piece of string
1.145 paf 626: String& mid(size_t substr_begin, size_t substr_end) const;
1.172 misha 627: String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const;
1.145 paf 628:
629: /**
630: ignore lang if it's L_UNSPECIFIED
631: but when specified: look for substring that lies in ONE fragment in THAT lang
632: @return position of substr in string, -1 means "not found" [const char* version]
633: */
1.146 paf 634: size_t pos(const Body substr,
1.145 paf 635: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
636: /// String version of @see pos(const char*, int, Language)
637: size_t pos(const String& substr,
638: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
639: size_t pos(char c,
640: size_t this_offset=0) const {
641: return body.pos(c, this_offset);
642: }
1.173 misha 643: size_t pos(Charset& charset,
1.171 misha 644: const String& substr,
645: size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55 paf 646:
1.203 misha 647: size_t strrpbrk(const char* chars, size_t left=0) const {
648: return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND;
649: }
650: size_t strrpbrk(const char* chars, size_t left, size_t right) const {
651: return body.strrpbrk(chars, left, right);
652: }
653:
654: size_t rskipchars(const char* chars, size_t left=0) const {
655: return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND;
656: }
657: size_t rskipchars(const char* chars, size_t left, size_t right) const {
658: return body.rskipchars(chars, left, right);
659: }
660:
1.145 paf 661: void split(ArrayString& result,
662: size_t& pos_after,
663: const char* delim,
664: Language lang=L_UNSPECIFIED, int limit=-1) const;
665: void split(ArrayString& result,
666: size_t& pos_after,
1.62 paf 667: const String& delim,
1.145 paf 668: Language lang=L_UNSPECIFIED, int limit=-1) const;
1.62 paf 669:
1.145 paf 670: typedef void (*Row_action)(Table& table, ArrayString* row,
1.136 paf 671: int prestart, int prefinish,
672: int poststart, int postfinish,
1.68 paf 673: void *info);
1.87 parser 674: /**
1.145 paf 675: @return table of found items, if any.
1.87 parser 676: table format is defined and fixed[can be used by others]:
677: @verbatim
678: prematch/match/postmatch/1/2/3/...
679: @endverbatim
680: */
1.178 misha 681: Table* match(VRegex* vregex,
1.99 parser 682: Row_action row_action, void *info,
1.168 misha 683: int& matches_count) const;
1.87 parser 684: enum Change_case_kind {
685: CC_UPPER,
686: CC_LOWER
687: };
1.145 paf 688: String& change_case(Charset& source_charset,
1.87 parser 689: Change_case_kind kind) const;
1.145 paf 690: const String& replace(const Dictionary& dict) const;
1.188 misha 691: const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const;
1.155 paf 692: double as_double() const { return pa_atod(cstr(), this); }
693: int as_int() const { return pa_atoi(cstr(), this); }
1.167 misha 694: bool as_bool() const { return as_int()!=0; }
1.174 misha 695: const String& escape(Charset& source_charset) const;
1.137 paf 696:
1.7 paf 697: private: //disabled
698:
1.12 paf 699: String& operator = (const String&) { return *this; }
1.7 paf 700:
1.1 paf 701: };
1.119 paf 702:
1.183 misha 703: #ifndef HASH_CODE_CACHING
1.145 paf 704: /// simple hash code of string. used by Hash
1.146 paf 705: inline uint hash_code(const String::Body self) {
1.184 misha 706: return self.get_hash_code();
1.119 paf 707: }
1.183 misha 708: #endif
1.147 paf 709:
710:
1.1 paf 711: #endif
E-mail: