Annotation of parser3/src/include/pa_string.h, revision 1.228

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

E-mail: