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

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

E-mail: