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