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