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