Annotation of parser3/src/include/pa_string.h, revision 1.237
1.41 paf 1: /** @file
1.43 paf 2: Parser: string class decl.
3:
1.234 moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.229 moko 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.237 ! moko 11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.236 2025/05/25 17:44:27 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) {}
1.237 ! moko 381: explicit Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
! 382: explicit Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {}
1.213 moko 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.236 moko 403: static Body uitoa(size_t aindex);
1.213 moko 404:
1.215 moko 405: void clear() { ZERO_LENGTH ZERO_HASH_CODE body=CORD_EMPTY; }
1.146 paf 406:
407: bool operator! () const { return is_empty(); }
408:
1.211 moko 409: inline CORD get_cord() const { return body; }
1.183 misha 410: uint get_hash_code() const;
1.146 paf 411:
1.221 moko 412: // never null
1.197 misha 413: const char* cstr() const {
414: #ifdef STRING_LENGTH_CACHING
415: string_length = length();
1.211 moko 416: if(string_length){
417: const char *result=CORD_to_const_char_star(body, string_length);
418: const_cast<Body*>(this)->body=(CORD)result;
419: return result;
420: }
1.197 misha 421: #endif
422: return CORD_to_const_char_star(body, length());
423: }
424:
1.221 moko 425: // never null
1.194 misha 426: char* cstrm() const { return CORD_to_char_star(body, length()); }
1.146 paf 427:
1.187 misha 428: #ifdef STRING_LENGTH_CACHING
429: void set_length(size_t alength){ string_length = alength; }
1.211 moko 430: 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 431: #else
1.146 paf 432: size_t length() const { return CORD_len(body); }
1.187 misha 433: #endif
1.146 paf 434:
1.212 moko 435: inline bool is_empty() const { return body==CORD_EMPTY; }
1.146 paf 436:
437: void append_know_length(const char *str, size_t known_length) {
1.187 misha 438: if(known_length){
439: if(body){
440: body = CORD_cat_char_star(body, str, known_length);
441: ZERO_LENGTH
442: } else {
1.211 moko 443: body=(CORD)str;
1.187 misha 444: #ifdef STRING_LENGTH_CACHING
445: string_length=known_length;
446: #endif
447: }
448: }
1.146 paf 449: }
450: void append_strdup_know_length(const char* str, size_t known_length) {
451: if(known_length)
452: append_know_length(pa_strdup(str, known_length), known_length);
453: }
1.187 misha 454: void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); }
455: Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; }
456:
1.146 paf 457: Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
458:
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; }
462: bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
1.207 moko 463:
1.146 paf 464: bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
465: bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
466:
1.211 moko 467: bool operator != (const char *src) const { return CORD_cmp(body, AS_CORD(src))!=0; }
468: bool operator == (const char *src) const { return CORD_cmp(body, AS_CORD(src))==0; }
1.207 moko 469:
1.146 paf 470: int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
471: return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145 paf 472: }
473:
1.146 paf 474: char fetch(size_t index) const { return CORD_fetch(body, index); }
1.211 moko 475: Body mid(size_t aindex, size_t alength) const { return Body(CORD_substr(body, aindex, alength, length())); }
476: size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, AS_CORD(substr), length()); }
1.146 paf 477: size_t pos(const Body substr, size_t offset=0) const {
1.180 misha 478: if(substr.is_empty())
1.146 paf 479: return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150 paf 480:
481: // CORD_str checks for bad offset [CORD_chr does not]
1.194 misha 482: return CORD_str(body, offset, substr.body, length());
1.146 paf 483: }
1.207 moko 484: size_t pos(char c, size_t offset=0) const {
1.150 paf 485: if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
486: return STRING_NOT_FOUND;
487:
1.146 paf 488: return CORD_chr(body, offset, c);
1.145 paf 489: }
1.146 paf 490:
1.203 misha 491: size_t strrpbrk(const char* chars, size_t left, size_t right) const;
492:
493: size_t rskipchars(const char* chars, size_t left, size_t right) const;
494:
1.187 misha 495: template<typename I>
496: int for_each(int (*f)(char c, I), I info) const {
1.159 paf 497: return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
498: }
499:
1.187 misha 500: template<typename I>
501: int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const {
1.159 paf 502: return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148 paf 503: }
1.146 paf 504:
505: void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
506:
1.157 paf 507: /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
508: Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
1.188 misha 509: size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const;
1.145 paf 510: };
511:
1.218 moko 512: private:
1.145 paf 513:
1.152 paf 514: Body body; ///< all characters of string
1.146 paf 515: Languages langs; ///< string characters lang
516:
1.164 paf 517: void dump() const;
518: #define ASSERT_STRING_INVARIANT(string) \
519: assert((string).langs.invariant((string).body.length()))
1.145 paf 520:
1.8 paf 521: public:
1.151 paf 522:
523: static const String Empty;
1.8 paf 524:
1.181 misha 525: explicit String(){};
1.211 moko 526: explicit String(const char* cstr, Language alang=L_CLEAN) : body(cstr){
527: if(body.get_cord()){
1.182 misha 528: langs=alang;
1.181 misha 529: }
530: }
1.213 moko 531: explicit String(C ac, Language alang=L_CLEAN) : body(ac){
1.211 moko 532: if(body.get_cord()){
533: langs=alang;
534: }
535: }
1.146 paf 536: String(Body abody, Language alang): body(abody), langs(alang) {
537: ASSERT_STRING_INVARIANT(*this);
538: }
539: String(const String& src): body(src.body), langs(src.langs) {
540: ASSERT_STRING_INVARIANT(*this);
1.145 paf 541: }
1.212 moko 542: String(int value, const char *format);
1.145 paf 543:
544: /// for convinient hash lookup
1.183 misha 545: #ifdef HASH_CODE_CACHING
546: operator const Body&() const { return body; }
547: #else
1.146 paf 548: operator const Body() const { return body; }
1.183 misha 549: #endif
1.145 paf 550:
551: bool is_empty() const { return body.is_empty(); }
552: size_t length() const { return body.length(); }
1.171 misha 553: size_t length(Charset& charset) const;
1.145 paf 554:
1.191 misha 555: /// convert to CORD forcing lang tainting
556: Body cstr_to_string_body_taint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
557: /// convert to CORD with tainting dirty to lang
558: Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
1.145 paf 559:
1.212 moko 560: /// from body
561: const char* cstr() const { return body.cstr(); }
562: char* cstrm() const { return body.cstrm(); }
1.189 misha 563:
564: /// convert to constant C string forcing lang tainting
1.190 misha 565: const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 566: return cstr_to_string_body_taint(lang, connection, charsets).cstr();
567: }
1.190 misha 568: char *taint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 569: return cstr_to_string_body_taint(lang, connection, charsets).cstrm();
570: }
571:
572: /// convert to constant C string with tainting dirty to lang
1.190 misha 573: const char* untaint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 574: return cstr_to_string_body_untaint(lang, connection, charsets).cstr();
575: }
1.190 misha 576: char *untaint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189 misha 577: return cstr_to_string_body_untaint(lang, connection, charsets).cstrm();
578: }
1.195 misha 579:
1.196 misha 580: const char* untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const;
1.195 misha 581:
1.202 moko 582: bool is_not_just_lang() const {
583: return langs.opt.is_not_just_lang !=0;
584: }
585:
586: Language just_lang() const {
587: return langs.opt.lang;
588: }
589:
1.222 moko 590: char* visualize_langs() const;
591:
1.108 parser 592: /// puts pieces to buf
1.145 paf 593: Cm serialize(size_t prolog_size) const;
1.108 parser 594: /// appends pieces from buf to self
1.145 paf 595: bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146 paf 596: /// @see Body::append_know_length
1.145 paf 597: String& append_know_length(const char* str, size_t known_length, Language lang);
1.146 paf 598: /// @see Body::append_help_length
1.145 paf 599: String& append_help_length(const char* str, size_t helper_length, Language lang);
600: String& append_strdup(const char* str, size_t helper_length, Language lang);
601:
1.146 paf 602: bool operator == (const char* y) const { return body==Body(y); }
603: bool operator != (const char* y) const { return body!=Body(y); }
1.145 paf 604:
605: /// this starts with y
606: bool starts_with(const char* y) const {
1.146 paf 607: return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145 paf 608: }
609: /// x starts with this
610: bool this_starts(const char* x) const {
1.146 paf 611: return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26 paf 612: }
613:
1.220 moko 614: String& append_to(String& dest) const;
1.187 misha 615: String& append_to(String& dest, Language lang, bool forced=false) const;
1.145 paf 616: String& append(const String& src, Language lang, bool forced=false) {
617: return src.append_to(*this, lang, forced);
618: }
1.199 misha 619: String& append_quoted(const String* src, Language lang=L_JSON){
620: *this << "\"";
621: if(src)
622: this->append(*src, lang, true/*forced lang*/);
623: *this << "\"";
624: return *this;
625: }
626:
1.220 moko 627: String& operator << (const String& src) { return src.append_to(*this); }
1.145 paf 628: String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.187 misha 629: String& operator << (const Body& src){
630: langs.appendHelper(body, L_AS_IS, src);
631: body<<src;
632: return *this;
633: }
1.100 parser 634:
1.142 paf 635: char first_char() const {
1.145 paf 636: return is_empty()?0:body.fetch(0);
1.142 paf 637: }
1.54 paf 638:
1.205 moko 639: char last_char() const {
640: return is_empty()?0:body.fetch(body.length()-1);
641: }
642:
1.145 paf 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: bool operator == (const String& src) const { return body==src.body; }
649:
1.54 paf 650: /// extracts [start, finish) piece of string
1.145 paf 651: String& mid(size_t substr_begin, size_t substr_end) const;
1.172 misha 652: String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const;
1.145 paf 653:
1.235 moko 654: /// return position of substr in string, -1 means "not found" [const char* version]
655: size_t pos(const char* substr, size_t this_offset=0) const { return body.pos(substr, this_offset); }
656: size_t pos(const Body substr, size_t this_offset=0) const { return body.pos(substr, this_offset); }
657: size_t pos(const String& substr, size_t this_offset=0) const { return body.pos(substr.body, this_offset); }
658: size_t pos(char c, size_t this_offset=0) const { return body.pos(c, this_offset); }
659: /// ignore lang if it's L_UNSPECIFIED, otherwise look for substring that lies in ONE fragment in THAT lang
660: size_t pos(const Body substr, size_t this_offset, Language lang) const;
661: size_t pos(const String& substr, size_t this_offset, Language lang) const;
1.216 moko 662: size_t pos(Charset& charset, const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55 paf 663:
1.203 misha 664: size_t strrpbrk(const char* chars, size_t left=0) const {
665: return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND;
666: }
667: size_t strrpbrk(const char* chars, size_t left, size_t right) const {
668: return body.strrpbrk(chars, left, right);
669: }
670:
671: size_t rskipchars(const char* chars, size_t left=0) const {
672: return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND;
673: }
674: size_t rskipchars(const char* chars, size_t left, size_t right) const {
675: return body.rskipchars(chars, left, right);
676: }
677:
1.219 moko 678: void split(ArrayString& result, size_t pos_after, const char* delim, Language lang=L_UNSPECIFIED) const;
679: void split(ArrayString& result, size_t pos_after, const String& delim, Language lang=L_UNSPECIFIED) const;
1.216 moko 680:
681: typedef void (*Row_action)(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info);
682:
1.87 parser 683: /**
1.145 paf 684: @return table of found items, if any.
1.87 parser 685: table format is defined and fixed[can be used by others]:
686: @verbatim
687: prematch/match/postmatch/1/2/3/...
688: @endverbatim
689: */
1.216 moko 690: Table* match(VRegex* vregex, Row_action row_action, void *info, int& matches_count) const;
691:
1.87 parser 692: enum Change_case_kind {
693: CC_UPPER,
694: CC_LOWER
695: };
1.216 moko 696: String& change_case(Charset& source_charset, Change_case_kind kind) const;
697:
1.145 paf 698: const String& replace(const Dictionary& dict) const;
1.188 misha 699: const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const;
1.155 paf 700: double as_double() const { return pa_atod(cstr(), this); }
1.226 moko 701: int as_int() const { return pa_atoi(cstr(), 0, this); }
1.167 misha 702: bool as_bool() const { return as_int()!=0; }
1.174 misha 703: const String& escape(Charset& source_charset) const;
1.137 paf 704:
1.7 paf 705: private: //disabled
706:
1.12 paf 707: String& operator = (const String&) { return *this; }
1.7 paf 708:
1.1 paf 709: };
1.119 paf 710:
1.183 misha 711: #ifndef HASH_CODE_CACHING
1.145 paf 712: /// simple hash code of string. used by Hash
1.146 paf 713: inline uint hash_code(const String::Body self) {
1.184 misha 714: return self.get_hash_code();
1.119 paf 715: }
1.183 misha 716: #endif
1.147 paf 717:
718:
1.1 paf 719: #endif
E-mail: