Annotation of parser3/src/include/pa_string.h, revision 1.231
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.231 ! moko 11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.230 2023/11/23 01:27:12 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: }
! 59: inline Array& operator+=(const String *src) {
! 60: assert(src != NULL);
! 61: return Array::operator+=(src);
! 62: }
! 63: };
! 64: #endif
1.145 paf 65:
1.155 paf 66: // generally useful
67:
1.230 moko 68: double pa_atod(const char* str, const String* problem_source);
1.226 moko 69: int pa_atoi(const char* str, int base=10, const String* problem_source=0);
70: unsigned int pa_atoui(const char *str, int base=10, const String* problem_source=0);
1.227 moko 71: uint64_t pa_atoul(const char *str, int base=10, const String* problem_source=0);
1.155 paf 72:
1.145 paf 73: /// this is result of pos functions which mean that substr were not found
74: #define STRING_NOT_FOUND ((size_t)-1)
75:
1.211 moko 76: /// CORD can't be empty string, thus checking it in assigment
77: #define AS_CORD(v) ((v) && *(v) ? (CORD)(v):0)
78:
1.42 paf 79: /**
1.146 paf 80: String which knows the lang of all it's langs.
1.41 paf 81:
82: All pieces remember
83: - whether they are tainted or not,
1.146 paf 84: and the lang which should be used to detaint them
1.41 paf 85: */
1.176 misha 86:
87:
1.145 paf 88: class String: public PA_Object {
1.218 moko 89: friend class StringSplitHelper;
1.1 paf 90: public:
1.48 paf 91:
1.146 paf 92: /** piece is tainted or not. the lang to use when detaint
1.106 parser 93: remember to change String_Untaint_lang_name @ untaint.C along
1.148 paf 94:
95: WARNING WARNING WARNING WARNING WARNING WARNING
96:
97: pos function compares(<=) languages, that is used in searching
98: for table column separator being L_CLEAN or L_AS_IS.
99: they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
100:
101: letters assigned for debugging, but it's important for no language-letter
102: come before L_AS_IS other then L_CLEAN
103:
104: WARNING WARNING WARNING WARNING WARNING WARNING
1.106 parser 105: */
1.225 moko 106:
107: #if _MSC_VER >= 1900
108: /// required for VS2015+ to make sizeof(Languages::opt) == sizeof(CORD), will be 16 byte under x64 without it
109: enum Language : size_t {
110: #else
1.145 paf 111: enum Language {
1.225 moko 112: #endif
113: L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145 paf 114: // these two must go before others, there are checks for >L_AS_IS
1.225 moko 115: L_CLEAN='0', ///< clean WARNING: read above warning before changing
116: L_AS_IS='A', ///< leave all characters intact WARNING: read above warning before changing
1.122 paf 117:
1.225 moko 118: L_TAINTED='T', ///< tainted, untaint lang as assigned later
1.146 paf 119: // untaint langs. assigned by ^untaint[lang]{...}
1.170 misha 120: L_FILE_SPEC='F', ///< file specification
121: L_HTTP_HEADER='h', ///< text in HTTP response header
122: L_MAIL_HEADER='m', ///< text in mail header
1.225 moko 123: L_URI='U', ///< text in uri
124: L_SQL='Q', ///< ^table:sql body
125: L_JS='J', ///< JavaScript code
126: L_XML='X', ///< ^xdoc:create xml
127: L_HTML='H', ///< HTML code
1.198 misha 128: L_REGEX='R', ///< RegExp
1.225 moko 129: L_JSON='S', ///< JSON code
1.170 misha 130: L_HTTP_COOKIE='C', ///< cookies encoded as %uXXXX for compartibility with js functions encode/decode
1.192 misha 131: L_PARSER_CODE='p', ///< ^process body
1.148 paf 132: // READ WARNING ABOVE BEFORE ADDING ANYTHING
1.146 paf 133: L_OPTIMIZE_BIT = 0x80 ///< flag, requiring cstr whitespace optimization
1.27 paf 134: };
135:
1.157 paf 136: enum Trim_kind {
137: TRIM_BOTH,
138: TRIM_START,
139: TRIM_END
140: };
141:
1.187 misha 142: class Body;
1.176 misha 143:
1.147 paf 144: union Languages {
1.146 paf 145:
1.147 paf 146: struct {
1.160 paf 147: #ifdef PA_LITTLE_ENDIAN
1.147 paf 148: Language lang:8;
1.179 misha 149: size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 150: #elif defined(PA_BIG_ENDIAN)
1.179 misha 151: size_t is_not_just_lang:sizeof(CORD)*8-8;
1.160 paf 152: Language lang:8;
153: #else
154: # error word endianness not determined for some obscure reason
155: #endif
1.147 paf 156: } opt;
157: CORD langs;
1.146 paf 158:
1.187 misha 159: CORD make_langs(const Body& current) const {
160: return opt.is_not_just_lang?langs:CORD_chars((char)opt.lang, current.length());
1.146 paf 161: }
162:
163: CORD make_langs(size_t aoffset, size_t alength) const {
1.147 paf 164: return opt.is_not_just_lang?
1.194 misha 165: CORD_substr(langs, aoffset, alength, 0)
1.162 paf 166: :CORD_chars((char)opt.lang, alength);
1.145 paf 167: }
168:
1.157 paf 169: /// appending when 'langs' already contain something [simple cases handled elsewhere]
1.187 misha 170: void append(size_t current, const CORD to_nonempty_target_langs) {
1.146 paf 171: assert(langs);
1.187 misha 172: if(opt.is_not_just_lang)
173: langs=CORD_cat(langs, to_nonempty_target_langs);
174: else {
175: assert(current);
176: langs=CORD_cat(CORD_chars((char)opt.lang, current), to_nonempty_target_langs);
177: }
178: }
1.146 paf 179:
1.187 misha 180: void append(const Body& current, const CORD to_nonempty_target_langs) {
181: assert(langs);
1.147 paf 182: if(opt.is_not_just_lang)
1.146 paf 183: langs=CORD_cat(langs, to_nonempty_target_langs);
1.187 misha 184: else {
185: size_t current_size=current.length();
1.146 paf 186: assert(current_size);
1.187 misha 187: langs=CORD_cat(CORD_chars((char)opt.lang, current_size), to_nonempty_target_langs);
1.146 paf 188: }
1.145 paf 189: }
1.146 paf 190:
1.145 paf 191: public:
1.146 paf 192:
1.222 moko 193: const char* visualize() const;
1.164 paf 194: void dump() const;
1.146 paf 195:
196: Languages(): langs(0) {}
1.147 paf 197: Languages(Language alang) {
198: opt.lang=alang;
199: opt.is_not_just_lang=0;
200: }
1.146 paf 201:
202: /// MUST be called exactly prior to modification of current [uses it's length]
1.187 misha 203: void append(size_t current, Language alang, size_t length) {
1.146 paf 204: assert(alang);
1.187 misha 205: assert(length);
1.146 paf 206:
1.208 moko 207: if(!opt.is_not_just_lang) {
1.147 paf 208: if(opt.lang) {
1.181 misha 209: if(opt.lang==alang) // same language? ignoring
1.146 paf 210: return;
211: } else {
1.147 paf 212: opt.lang=alang; // to uninitialized
1.146 paf 213: return;
1.145 paf 214: }
1.208 moko 215: }
1.146 paf 216:
1.187 misha 217: append(current, CORD_chars((char)alang, length));
1.146 paf 218: }
219:
1.187 misha 220: void append(const Body ¤t, Language alang, size_t length) {
1.176 misha 221: assert(alang);
1.187 misha 222: assert(length);
1.176 misha 223:
1.208 moko 224: if(!opt.is_not_just_lang) {
1.176 misha 225: if(opt.lang) {
1.183 misha 226: if(opt.lang==alang) // same language? ignoring
1.176 misha 227: return;
228: } else {
229: opt.lang=alang; // to uninitialized
230: return;
231: }
1.208 moko 232: }
1.176 misha 233:
1.187 misha 234: append(current, CORD_chars((char)alang, length));
1.176 misha 235: }
236:
1.187 misha 237: void appendHelper(const Body& current, Language alang, const Body &length_helper) {
238: assert(alang);
239:
1.208 moko 240: if(!opt.is_not_just_lang) {
1.187 misha 241: if(opt.lang) {
242: if(opt.lang==alang) // same language? ignoring
243: return;
244: } else {
245: opt.lang=alang; // to uninitialized
246: return;
247: }
1.208 moko 248: }
1.187 misha 249:
250: append(current, CORD_chars((char)alang, length_helper.length()));
251: }
252:
253: void appendHelper(const Body& current, const Languages &src, const Body& length_helper) {
1.186 misha 254: if(!langs){
1.176 misha 255: langs=src.langs; // to uninitialized
1.186 misha 256: #ifdef CORD_CAT_OPTIMIZATION
257: if(opt.is_not_just_lang && !CORD_IS_STRING(langs))
258: CORD_concatenation_protect(langs);
259: #endif
260: }
1.176 misha 261: else if(!src.opt.is_not_just_lang)
262: appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source
263: else
264: append(current, src.make_langs(length_helper));
265: }
266:
1.146 paf 267: /// MUST be called exactly prior to modification of current [uses it's length]
1.187 misha 268: void append(const Body& current, const Languages src, size_t aoffset, size_t alength) {
1.146 paf 269: assert(alength);
270:
271: if(!langs) // to uninitialized?
1.147 paf 272: if(src.opt.is_not_just_lang)
1.194 misha 273: langs=CORD_substr(src.langs, aoffset, alength, 0); // to uninitialized complex
1.146 paf 274: else
1.147 paf 275: opt.lang=src.opt.lang; // to uninitialized simple
1.146 paf 276: else
1.147 paf 277: 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 278: return; // ignoring
279: else
280: append(current, src.make_langs(aoffset, alength));
281: }
282:
283: /// checks if we have lang<=alang all from aoffset to aoffset+alength
284: bool check_lang(Language alang, size_t aoffset, size_t alength) const {
285: if(alang==L_UNSPECIFIED) // ignore lang?
286: return true;
287:
1.147 paf 288: if(opt.is_not_just_lang)
1.146 paf 289: return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
290: else
1.169 misha 291: return (unsigned)opt.lang<=(unsigned)alang;
1.146 paf 292: }
293:
1.148 paf 294: /// @returns count of blocks
295: /// @todo currently there can be adjucent blocks of same language. someday merge them
296: size_t count() const {
297: return opt.is_not_just_lang?
298: CORD_block_count(langs)
299: : opt.lang?
300: 1
301: : 0;
302: };
303:
1.187 misha 304: template<typename I>
305: void for_each(size_t current, int callback(char, size_t, I), I info) const {
306: if(opt.is_not_just_lang)
307: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
308: else
309: callback(opt.lang, current, info);
310: }
311:
312: template<typename I>
313: void for_each(const Body& current, int callback(char, size_t, I), I info) const {
1.147 paf 314: if(opt.is_not_just_lang)
1.146 paf 315: CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
316: else
1.187 misha 317: callback(opt.lang, current.length(), info);
1.146 paf 318: }
319:
1.164 paf 320: bool invariant(size_t current_length) const {
1.146 paf 321: if(!langs)
322: return current_length==0;
1.147 paf 323: if(opt.is_not_just_lang)
1.146 paf 324: return CORD_len(langs)==current_length;
325: return true; // uncheckable, actually
326: }
327: };
328:
1.213 moko 329: struct C {
330: const char *str;
331: size_t length;
332: C(): str(0), length(0) {}
333: C(const char *astr, size_t asize): str(astr), length(asize) {}
1.221 moko 334: explicit C(Body abody): str(abody.cstr()), length(abody.length()) {}
1.213 moko 335: };
336:
337: struct Cm {
338: char *str;
339: size_t length;
340: Cm(): str(0), length(0) {}
341: Cm(char *astr, size_t asize): str(astr), length(asize) {}
1.221 moko 342: explicit Cm(Body abody): str(abody.cstrm()), length(abody.length()) {}
1.213 moko 343: };
344:
1.146 paf 345: class Body {
346:
347: CORD body;
348:
1.183 misha 349: #ifdef HASH_CODE_CACHING
350: // cached hash code is not reseted on write operations as test shows
351: // that string body does not change after it is stored as a hash key
352: mutable uint hash_code;
1.213 moko 353: #define INIT_HASH_CODE(c) ,hash_code(c)
1.215 moko 354: #define ZERO_HASH_CODE hash_code=0;
1.213 moko 355: #else
356: #define INIT_HASH_CODE(c)
1.215 moko 357: #define ZERO_HASH_CODE
1.183 misha 358: #endif
359:
1.187 misha 360: #ifdef STRING_LENGTH_CACHING
361: // cached length is reseted on modification, used only for char*, not CORD
362: mutable size_t string_length;
1.213 moko 363: #define INIT_LENGTH(l) ,string_length(l)
1.187 misha 364: #define ZERO_LENGTH string_length=0;
365: #else
1.213 moko 366: #define INIT_LENGTH(l)
1.187 misha 367: #define ZERO_LENGTH
368: #endif
369:
1.146 paf 370: public:
371:
1.164 paf 372: void dump() const;
1.146 paf 373:
1.213 moko 374: Body(): body(CORD_EMPTY) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
375: Body(const char *abody): body(AS_CORD(abody)) INIT_HASH_CODE(0) INIT_LENGTH(0) {}
376: Body(CORD abody, uint ahash_code): body(abody) INIT_HASH_CODE(ahash_code) INIT_LENGTH(0) {}
377: explicit Body(C ac): body(AS_CORD(ac.str)) INIT_HASH_CODE(0) INIT_LENGTH(ac.length) {}
378: explicit Body(CORD abody): body(abody) INIT_HASH_CODE(0) INIT_LENGTH(0) {
1.186 misha 379: #ifdef CORD_CAT_OPTIMIZATION
380: assert(!body // no body
381: || *body // ordinary string
382: || body[1]==1 // CONCAT_HDR
383: || body[1]==3 // CONCAT_HDR_READ_ONLY
384: || body[1]==4 // FN_HDR
385: || body[1]==6 // SUBSTR_HDR
386: );
387: #else
1.146 paf 388: assert(!body // no body
389: || *body // ordinary string
390: || body[1]==1 // CONCAT_HDR
391: || body[1]==4 // FN_HDR
392: || body[1]==6 // SUBSTR_HDR
393: );
1.186 misha 394: #endif
1.146 paf 395: }
1.181 misha 396:
1.213 moko 397:
1.146 paf 398: static Body Format(int value);
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:
649: /**
650: ignore lang if it's L_UNSPECIFIED
651: but when specified: look for substring that lies in ONE fragment in THAT lang
652: @return position of substr in string, -1 means "not found" [const char* version]
653: */
1.216 moko 654: size_t pos(const Body substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.145 paf 655: /// String version of @see pos(const char*, int, Language)
1.216 moko 656: size_t pos(const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
657: size_t pos(char c, size_t this_offset=0) const {
1.145 paf 658: return body.pos(c, this_offset);
659: }
1.216 moko 660: size_t pos(Charset& charset, const String& substr, size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55 paf 661:
1.203 misha 662: size_t strrpbrk(const char* chars, size_t left=0) const {
663: return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND;
664: }
665: size_t strrpbrk(const char* chars, size_t left, size_t right) const {
666: return body.strrpbrk(chars, left, right);
667: }
668:
669: size_t rskipchars(const char* chars, size_t left=0) const {
670: return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND;
671: }
672: size_t rskipchars(const char* chars, size_t left, size_t right) const {
673: return body.rskipchars(chars, left, right);
674: }
675:
1.219 moko 676: void split(ArrayString& result, size_t pos_after, const char* delim, Language lang=L_UNSPECIFIED) const;
677: void split(ArrayString& result, size_t pos_after, const String& delim, Language lang=L_UNSPECIFIED) const;
1.216 moko 678:
679: typedef void (*Row_action)(Table& table, ArrayString* row, int prestart, int prefinish, int poststart, int postfinish, void *info);
680:
1.87 parser 681: /**
1.145 paf 682: @return table of found items, if any.
1.87 parser 683: table format is defined and fixed[can be used by others]:
684: @verbatim
685: prematch/match/postmatch/1/2/3/...
686: @endverbatim
687: */
1.216 moko 688: Table* match(VRegex* vregex, Row_action row_action, void *info, int& matches_count) const;
689:
1.87 parser 690: enum Change_case_kind {
691: CC_UPPER,
692: CC_LOWER
693: };
1.216 moko 694: String& change_case(Charset& source_charset, Change_case_kind kind) const;
695:
1.145 paf 696: const String& replace(const Dictionary& dict) const;
1.188 misha 697: const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const;
1.155 paf 698: double as_double() const { return pa_atod(cstr(), this); }
1.226 moko 699: int as_int() const { return pa_atoi(cstr(), 0, this); }
1.167 misha 700: bool as_bool() const { return as_int()!=0; }
1.174 misha 701: const String& escape(Charset& source_charset) const;
1.137 paf 702:
1.7 paf 703: private: //disabled
704:
1.12 paf 705: String& operator = (const String&) { return *this; }
1.7 paf 706:
1.1 paf 707: };
1.119 paf 708:
1.183 misha 709: #ifndef HASH_CODE_CACHING
1.145 paf 710: /// simple hash code of string. used by Hash
1.146 paf 711: inline uint hash_code(const String::Body self) {
1.184 misha 712: return self.get_hash_code();
1.119 paf 713: }
1.183 misha 714: #endif
1.147 paf 715:
716:
1.1 paf 717: #endif
E-mail: