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

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

E-mail: