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