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

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.205   ! moko       11: #define IDENT_PA_STRING_H "$Id: pa_string.h,v 1.204 2012-05-28 10:33:18 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.145     paf        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.155     paf        58: 
1.145     paf        59: /// this is result of pos functions which mean that substr were not found
                     60: #define STRING_NOT_FOUND ((size_t)-1)
                     61: 
1.42      paf        62: /** 
1.146     paf        63:        String which knows the lang of all it's langs.
1.41      paf        64: 
                     65:        All pieces remember 
                     66:        - whether they are tainted or not, 
1.146     paf        67:          and the lang which should be used to detaint them
1.41      paf        68: */
1.176     misha      69: 
                     70: 
1.145     paf        71: class String: public PA_Object {
1.1       paf        72: public:
1.48      paf        73: 
1.146     paf        74:        /** piece is tainted or not. the lang to use when detaint
1.106     parser     75:                remember to change String_Untaint_lang_name @ untaint.C along
1.148     paf        76: 
                     77:                WARNING WARNING WARNING WARNING WARNING WARNING 
                     78:                
                     79:                pos function compares(<=) languages, that is used in searching
                     80:                for table column separator being L_CLEAN or L_AS_IS.
                     81:                they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
                     82:                
                     83:                letters assigned for debugging, but it's important for no language-letter
                     84:                come before L_AS_IS other then L_CLEAN
                     85: 
                     86:                WARNING WARNING WARNING WARNING WARNING WARNING 
1.106     parser     87:        */
1.145     paf        88:        enum Language {
1.146     paf        89:                L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145     paf        90:                // these two must go before others, there are checks for >L_AS_IS
1.170     misha      91:                L_CLEAN='0',    ///< clean  WARNING: read above warning before changing
                     92:                L_AS_IS='A',    ///< leave all characters intact  WARNING: read above warning before changing
1.122     paf        93: 
1.148     paf        94:                L_PASS_APPENDED='P',
1.41      paf        95:                        /**<
1.146     paf        96:                                leave lang built into string being appended.
1.41      paf        97:                                just a flag, that value not stored
                     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: 
                    174:                const char* v() 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.147     paf       188:                        if(!opt.is_not_just_lang)
                    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.146     paf       196: 
1.187     misha     197:                        append(current, CORD_chars((char)alang, length));
1.146     paf       198:                }
                    199: 
1.187     misha     200:                void append(const Body &current, Language alang, size_t length) {
1.176     misha     201:                        assert(alang);
1.187     misha     202:                        assert(length);
1.176     misha     203: 
                    204:                        if(!opt.is_not_just_lang)
                    205:                                if(opt.lang) {
1.183     misha     206:                                        if(opt.lang==alang) // same language? ignoring
1.176     misha     207:                                                return;
                    208:                                } else {
                    209:                                        opt.lang=alang; // to uninitialized
                    210:                                        return;
                    211:                                }
                    212: 
1.187     misha     213:                        append(current, CORD_chars((char)alang, length));
1.176     misha     214:                }
                    215: 
1.187     misha     216:                void appendHelper(const Body& current, Language alang, const Body &length_helper) {
                    217:                        assert(alang);
                    218: 
                    219:                        if(!opt.is_not_just_lang)
                    220:                                if(opt.lang) {
                    221:                                        if(opt.lang==alang) // same language? ignoring
                    222:                                                return;
                    223:                                } else {
                    224:                                        opt.lang=alang; // to uninitialized
                    225:                                        return;
                    226:                                }
                    227: 
                    228:                        append(current, CORD_chars((char)alang, length_helper.length()));
                    229:                }
                    230: 
                    231:                void appendHelper(const Body& current, const Languages &src, const Body& length_helper) {
1.186     misha     232:                        if(!langs){
1.176     misha     233:                                langs=src.langs; // to uninitialized
1.186     misha     234: #ifdef CORD_CAT_OPTIMIZATION
                    235:                                if(opt.is_not_just_lang && !CORD_IS_STRING(langs))
                    236:                                        CORD_concatenation_protect(langs);
                    237: #endif
                    238:                        }
1.176     misha     239:                        else if(!src.opt.is_not_just_lang)
                    240:                                appendHelper(current, src.opt.lang, length_helper); // simplifying when simple source
                    241:                        else
                    242:                                append(current, src.make_langs(length_helper));
                    243:                }
                    244: 
1.146     paf       245:                /// MUST be called exactly prior to modification of current [uses it's length]
1.187     misha     246:                void append(const Body& current, const Languages src, size_t aoffset, size_t alength) {
1.146     paf       247:                        assert(alength);
                    248: 
                    249:                        if(!langs) // to uninitialized?
1.147     paf       250:                                if(src.opt.is_not_just_lang)
1.194     misha     251:                                        langs=CORD_substr(src.langs, aoffset, alength, 0); // to uninitialized complex
1.146     paf       252:                                else
1.147     paf       253:                                        opt.lang=src.opt.lang; // to uninitialized simple
1.146     paf       254:                        else 
1.147     paf       255:                                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       256:                                        return; // ignoring
                    257:                                else
                    258:                                        append(current, src.make_langs(aoffset, alength));
                    259:                }
                    260: 
                    261:                /// checks if we have lang<=alang all from aoffset to aoffset+alength
                    262:                bool check_lang(Language alang, size_t aoffset, size_t alength) const {
                    263:                        if(alang==L_UNSPECIFIED) // ignore lang?
                    264:                                return true;
                    265: 
1.147     paf       266:                        if(opt.is_not_just_lang)
1.146     paf       267:                                return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
                    268:                        else
1.169     misha     269:                                return (unsigned)opt.lang<=(unsigned)alang;
1.146     paf       270:                }
                    271: 
1.148     paf       272:                /// @returns count of blocks
                    273:                /// @todo currently there can be adjucent blocks of same language. someday merge them
                    274:                size_t count() const {
                    275:                        return opt.is_not_just_lang?
                    276:                                CORD_block_count(langs)
                    277:                                : opt.lang?
                    278:                                        1
                    279:                                        : 0;
                    280:                };
                    281: 
1.187     misha     282:                template<typename I> 
                    283:                void for_each(size_t current, int callback(char, size_t, I), I info) const {
                    284:                        if(opt.is_not_just_lang)
                    285:                                CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
                    286:                        else
                    287:                                callback(opt.lang, current, info);
                    288:                }
                    289: 
                    290:                template<typename I> 
                    291:                void for_each(const Body& current, int callback(char, size_t, I), I info) const {
1.147     paf       292:                        if(opt.is_not_just_lang)
1.146     paf       293:                                CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
                    294:                        else
1.187     misha     295:                                callback(opt.lang, current.length(), info);
1.146     paf       296:                }
                    297: 
1.164     paf       298:                bool invariant(size_t current_length) const {
1.146     paf       299:                        if(!langs)
                    300:                                return current_length==0;
1.147     paf       301:                        if(opt.is_not_just_lang)
1.146     paf       302:                                return CORD_len(langs)==current_length;
                    303:                        return true; // uncheckable, actually
                    304:                }
                    305:        };
                    306: 
                    307:        class Body {
                    308: 
                    309:                CORD body;
                    310: 
1.183     misha     311: #ifdef HASH_CODE_CACHING
                    312:                // cached hash code is not reseted on write operations as test shows 
                    313:                // that string body does not change after it is stored as a hash key
                    314:                mutable uint hash_code;
                    315: #endif
                    316: 
1.187     misha     317: #ifdef STRING_LENGTH_CACHING
                    318:                // cached length is reseted on modification, used only for char*, not CORD
                    319:                mutable size_t string_length;
                    320: #define INIT_LENGTH ,string_length(0)
                    321: #define ZERO_LENGTH string_length=0;
                    322: #else
                    323: #define INIT_LENGTH
                    324: #define ZERO_LENGTH
                    325: #endif
                    326: 
1.146     paf       327:        public:
                    328: 
                    329:                const char* v() const;
1.164     paf       330:                void dump() const;
1.146     paf       331: 
1.183     misha     332: #ifdef HASH_CODE_CACHING
1.187     misha     333:                Body(): body(CORD_EMPTY), hash_code(0) INIT_LENGTH {}
                    334:                Body(CORD abody, uint ahash_code): body(abody), hash_code(ahash_code) INIT_LENGTH {}
                    335:                Body(CORD abody): body(abody), hash_code(0) INIT_LENGTH {
1.183     misha     336: #else
1.187     misha     337:                Body(): body(CORD_EMPTY) INIT_LENGTH {}
                    338:                Body(CORD abody): body(abody) INIT_LENGTH {
1.183     misha     339: #endif
1.186     misha     340: #ifdef CORD_CAT_OPTIMIZATION
                    341:                        assert(!body // no body
                    342:                                || *body // ordinary string
                    343:                                || body[1]==1 // CONCAT_HDR
                    344:                                || body[1]==3 // CONCAT_HDR_READ_ONLY
                    345:                                || body[1]==4 // FN_HDR 
                    346:                                || body[1]==6 // SUBSTR_HDR 
                    347:                                );
                    348: #else
1.146     paf       349:                        assert(!body // no body
                    350:                                || *body // ordinary string
                    351:                                || body[1]==1 // CONCAT_HDR
                    352:                                || body[1]==4 // FN_HDR 
                    353:                                || body[1]==6 // SUBSTR_HDR 
                    354:                                );
1.186     misha     355: #endif
1.146     paf       356:                }
1.181     misha     357: 
1.146     paf       358:                static Body Format(int value);
                    359: 
1.187     misha     360:                void clear() { ZERO_LENGTH body=CORD_EMPTY; }
1.146     paf       361: 
                    362:                bool operator! () const { return is_empty(); }
                    363: 
1.183     misha     364:                CORD get_cord() const { return body; }
                    365:                uint get_hash_code() const;
1.146     paf       366: 
1.197     misha     367:                const char* cstr() const {
                    368: #ifdef STRING_LENGTH_CACHING
                    369:                        string_length = length();
                    370:                        if(string_length)
                    371:                                return const_cast<Body*>(this)->body=CORD_to_const_char_star(body, string_length);
                    372: #endif
                    373:                        return CORD_to_const_char_star(body, length());
                    374:                }
                    375: 
1.194     misha     376:                char* cstrm() const { return CORD_to_char_star(body, length()); }
1.146     paf       377: 
1.187     misha     378: #ifdef STRING_LENGTH_CACHING
                    379:                void set_length(size_t alength){ string_length = alength; }
                    380:                size_t length() const { return body ? CORD_IS_STRING(body) ? string_length ? string_length : (string_length=strlen(body)) : CORD_len(body) : 0; }
                    381: #else
1.146     paf       382:                size_t length() const { return CORD_len(body); }
1.187     misha     383: #endif
1.146     paf       384: 
                    385:                bool is_empty() const { return body==CORD_EMPTY; }
                    386: 
                    387:                void append_know_length(const char *str, size_t known_length) {
1.187     misha     388:                        if(known_length){
                    389:                                if(body){
                    390:                                        body = CORD_cat_char_star(body, str, known_length);
                    391:                                        ZERO_LENGTH 
                    392:                                } else {
                    393:                                        body=str;
                    394: #ifdef STRING_LENGTH_CACHING
                    395:                                        string_length=known_length;
                    396: #endif
                    397:                                }
                    398:                        }
1.146     paf       399:                }
                    400:                void append_strdup_know_length(const char* str, size_t known_length) {
                    401:                        if(known_length)
                    402:                                append_know_length(pa_strdup(str, known_length), known_length);
                    403:                }
1.187     misha     404:                void append(char c) { ZERO_LENGTH body=CORD_cat_char(body, c); }
                    405:                Body& operator << (const Body src) { ZERO_LENGTH body=CORD_cat(body, src.body); return *this; }
                    406: 
1.146     paf       407:                Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
                    408: 
                    409:                // could not figure out why this operator is needed [should do this chain: string->simple->==]
                    410:                bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
                    411:                bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
                    412:                bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
                    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: 
                    417:                int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
                    418:                        return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145     paf       419:                }
                    420: 
1.146     paf       421:                char fetch(size_t index) const { return CORD_fetch(body, index); }
1.194     misha     422:                Body mid(size_t aindex, size_t alength) const { return CORD_substr(body, aindex, alength, length()); }
                    423:                size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr, length()); }
1.146     paf       424:                size_t pos(const Body substr, size_t offset=0) const { 
1.180     misha     425:                        if(substr.is_empty())
1.146     paf       426:                                return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150     paf       427: 
                    428:                        // CORD_str checks for bad offset [CORD_chr does not]
1.194     misha     429:                        return CORD_str(body, offset, substr.body, length()); 
1.146     paf       430:                }
                    431:                size_t pos(char c, 
                    432:                        size_t offset=0) const {
1.150     paf       433:                        if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
                    434:                                return STRING_NOT_FOUND;
                    435: 
1.146     paf       436:                        return CORD_chr(body, offset, c);
1.145     paf       437:                }
1.146     paf       438: 
1.203     misha     439:                size_t strrpbrk(const char* chars, size_t left, size_t right) const;
                    440: 
                    441:                size_t rskipchars(const char* chars, size_t left, size_t right) const;
                    442: 
1.187     misha     443:                template<typename I>
                    444:                int for_each(int (*f)(char c, I), I info) const {
1.159     paf       445:                        return CORD_iter(body, (CORD_iter_fn)f, (void*)info);
                    446:                }
                    447: 
1.187     misha     448:                template<typename I>
                    449:                int for_each(int (*f1)(char c, I), int (*f2)(const char* s, I), I info) const {
1.159     paf       450:                        return CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148     paf       451:                }
1.146     paf       452: 
                    453:                void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
                    454: 
                    455:                /*Body normalize() const {
                    456:                        return Body(CORD_balance(body));
                    457:                }*/
1.157     paf       458: 
                    459:                /// @returns this or 0 or mid. if returns this or 0 out_* are not filled
                    460:                Body trim(Trim_kind kind=TRIM_BOTH, const char* chars=0,
1.188     misha     461:                        size_t* out_start=0, size_t* out_length=0, Charset* source_charset=0) const;
1.145     paf       462:        };
                    463: 
                    464:        struct C {
                    465:                const char *str;
                    466:                size_t length;
                    467:                operator const char *() { return str; }
1.158     paf       468:                C(): str(0), length(0) {}
1.145     paf       469:                C(const char *astr, size_t asize): str(astr), length(asize) {}
                    470:        };
                    471: 
                    472:        struct Cm {
                    473:                char *str;
                    474:                size_t length;
                    475:                //operator char *() { return str; }
1.158     paf       476:                Cm(): str(0), length(0) {}
1.145     paf       477:                Cm(char *astr, size_t asize): str(astr), length(asize) {}
                    478:        };
                    479: 
                    480: private:
                    481: 
1.152     paf       482:        Body body; ///< all characters of string
1.146     paf       483:        Languages langs; ///< string characters lang
                    484: 
                    485:        const char* v() const;
1.164     paf       486:        void dump() const;
                    487:        #define ASSERT_STRING_INVARIANT(string) \
                    488:                assert((string).langs.invariant((string).body.length()))
1.145     paf       489: 
1.8       paf       490: public:
1.151     paf       491: 
                    492:        static const String Empty;
1.8       paf       493: 
1.181     misha     494:        explicit String(){};
1.182     misha     495:        explicit String(const char* cstr, Language alang=L_CLEAN){
1.181     misha     496:                if(cstr && *cstr){
                    497:                        body=cstr;
1.182     misha     498:                        langs=alang;
1.181     misha     499:                }
                    500:        }
1.187     misha     501:        explicit String(const char* cstr, Language alang, size_t alength){
                    502:                if(cstr && *cstr){
                    503:                        body=cstr;
                    504: #ifdef STRING_LENGTH_CACHING
                    505:                        body.set_length(alength);
                    506: #endif
1.182     misha     507:                        langs=alang;
1.181     misha     508:                }
                    509:        }
1.187     misha     510: 
1.185     misha     511:        String(int value, char *format);
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:        }
                    518: 
                    519:        /// for convinient hash lookup
1.183     misha     520: #ifdef HASH_CODE_CACHING
                    521:        operator const Body&() const { return body; }
                    522: #else
1.146     paf       523:        operator const Body() const { return body; }
1.183     misha     524: #endif
1.145     paf       525: 
                    526:        bool is_empty() const { return body.is_empty(); }
                    527:        size_t length() const { return body.length(); }
1.171     misha     528:        size_t length(Charset& charset) const;
1.145     paf       529: 
1.191     misha     530:        /// convert to CORD forcing lang tainting
                    531:        Body cstr_to_string_body_taint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
                    532:        /// convert to CORD with tainting dirty to lang
                    533:        Body cstr_to_string_body_untaint(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const;
1.145     paf       534: 
1.189     misha     535:        /// 
                    536:        const char* cstr() const {
                    537:                return body.cstr();
                    538:        }
                    539:        /// 
                    540:        char* cstrm() const {
                    541:                return body.cstrm();
                    542:        }
                    543: 
                    544:        /// convert to constant C string forcing lang tainting
1.190     misha     545:        const char* taint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     546:                return cstr_to_string_body_taint(lang, connection, charsets).cstr();
                    547:        }
1.190     misha     548:        char *taint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     549:                return cstr_to_string_body_taint(lang, connection, charsets).cstrm();
                    550:        }
                    551: 
                    552:        /// convert to constant C string with tainting dirty to lang
1.190     misha     553:        const char* untaint_cstr(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     554:                return cstr_to_string_body_untaint(lang, connection, charsets).cstr();
                    555:        }
1.190     misha     556:        char *untaint_cstrm(Language lang, SQL_Connection* connection=0, const Request_charsets *charsets=0) const {
1.189     misha     557:                return cstr_to_string_body_untaint(lang, connection, charsets).cstrm();
                    558:        }
1.195     misha     559: 
1.196     misha     560:        const char* untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const;
1.195     misha     561: 
1.202     moko      562:        bool is_not_just_lang() const {
                    563:                return langs.opt.is_not_just_lang !=0;
                    564:        }
                    565:        
                    566:        Language just_lang() const {
                    567:                return langs.opt.lang;
                    568:        }
                    569: 
1.108     parser    570:        /// puts pieces to buf
1.145     paf       571:        Cm serialize(size_t prolog_size) const;
1.108     parser    572:        /// appends pieces from buf to self
1.145     paf       573:        bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146     paf       574:        /// @see Body::append_know_length
1.145     paf       575:        String& append_know_length(const char* str, size_t known_length, Language lang);
1.146     paf       576:        /// @see Body::append_help_length
1.145     paf       577:        String& append_help_length(const char* str, size_t helper_length, Language lang);
                    578:        String& append_strdup(const char* str, size_t helper_length, Language lang);
                    579: 
1.146     paf       580:        bool operator == (const char* y) const { return body==Body(y); }
                    581:        bool operator != (const char* y) const { return body!=Body(y); }
1.145     paf       582: 
                    583:        /// this starts with y
                    584:        bool starts_with(const char* y) const {
1.146     paf       585:                return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145     paf       586:        }
                    587:        /// x starts with this
                    588:        bool this_starts(const char* x) const {
1.146     paf       589:                return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26      paf       590:        }
                    591: 
1.187     misha     592:        String& append_to(String& dest, Language lang, bool forced=false) const;
1.145     paf       593:        String& append(const String& src, Language lang, bool forced=false) { 
                    594:                return src.append_to(*this, lang, forced);
                    595:        }
1.199     misha     596:        String& append_quoted(const String* src, Language lang=L_JSON){
                    597:                *this << "\"";
                    598:                if(src)
                    599:                        this->append(*src, lang, true/*forced lang*/);
                    600:                *this << "\"";
                    601:                return *this;
                    602:        }
                    603: 
1.145     paf       604:        String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
                    605:        String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.187     misha     606:        String& operator << (const Body& src){
                    607:                langs.appendHelper(body, L_AS_IS, src);
                    608:                body<<src;
                    609:                return *this;
                    610:        }
1.100     parser    611: 
1.142     paf       612:        char first_char() const {
1.145     paf       613:                return is_empty()?0:body.fetch(0);
1.142     paf       614:        }
1.54      paf       615: 
1.205   ! moko      616:        char last_char() const {
        !           617:                return is_empty()?0:body.fetch(body.length()-1);
        !           618:        }
        !           619: 
1.145     paf       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:        bool operator == (const String& src) const { return body==src.body; }
                    626: 
1.54      paf       627:        /// extracts [start, finish) piece of string
1.145     paf       628:        String& mid(size_t substr_begin, size_t substr_end) const;
1.172     misha     629:        String& mid(Charset& charset, size_t from, size_t to, size_t helper_length=0) const;
1.145     paf       630: 
                    631:        /** 
                    632:                ignore lang if it's L_UNSPECIFIED
                    633:                but when specified: look for substring that lies in ONE fragment in THAT lang
                    634:                @return position of substr in string, -1 means "not found" [const char* version]
                    635:        */
1.146     paf       636:        size_t pos(const Body substr, 
1.145     paf       637:                size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
                    638:        /// String version of @see pos(const char*, int, Language)
                    639:        size_t pos(const String& substr, 
                    640:                size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
                    641:        size_t pos(char c, 
                    642:                size_t this_offset=0) const {
                    643:                return body.pos(c, this_offset);
                    644:        }
1.173     misha     645:        size_t pos(Charset& charset,
1.171     misha     646:                const String& substr, 
                    647:                size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
1.55      paf       648: 
1.203     misha     649:        size_t strrpbrk(const char* chars, size_t left=0) const {
                    650:                return (length()) ? body.strrpbrk(chars, left, length()-1) : STRING_NOT_FOUND;
                    651:        }
                    652:        size_t strrpbrk(const char* chars, size_t left, size_t right) const {
                    653:                return body.strrpbrk(chars, left, right);
                    654:        }
                    655: 
                    656:        size_t rskipchars(const char* chars, size_t left=0) const {
                    657:                return (length()) ? body.rskipchars(chars, left, length()-1) : STRING_NOT_FOUND;
                    658:        }
                    659:        size_t rskipchars(const char* chars, size_t left, size_t right) const {
                    660:                return body.rskipchars(chars, left, right);
                    661:        }
                    662: 
1.145     paf       663:        void split(ArrayString& result, 
                    664:                size_t& pos_after,
                    665:                const char* delim, 
                    666:                Language lang=L_UNSPECIFIED, int limit=-1) const;
                    667:        void split(ArrayString& result, 
                    668:                size_t& pos_after, 
1.62      paf       669:                const String& delim, 
1.145     paf       670:                Language lang=L_UNSPECIFIED, int limit=-1) const;
1.62      paf       671: 
1.145     paf       672:        typedef void (*Row_action)(Table& table, ArrayString* row, 
1.136     paf       673:                int prestart, int prefinish, 
                    674:                int poststart, int postfinish,
1.68      paf       675:                void *info);
1.87      parser    676:        /**
1.145     paf       677:                @return table of found items, if any.
1.87      parser    678:                table format is defined and fixed[can be used by others]: 
                    679:                @verbatim
                    680:                        prematch/match/postmatch/1/2/3/...
                    681:                @endverbatim
                    682:        */
1.178     misha     683:        Table* match(VRegex* vregex,
1.99      parser    684:                Row_action row_action, void *info,
1.168     misha     685:                int& matches_count) const;
1.87      parser    686:        enum Change_case_kind {
                    687:                CC_UPPER,
                    688:                CC_LOWER
                    689:        };
1.145     paf       690:        String& change_case(Charset& source_charset,
1.87      parser    691:                Change_case_kind kind) const;
1.145     paf       692:        const String& replace(const Dictionary& dict) const;
1.188     misha     693:        const String& trim(Trim_kind kind=TRIM_BOTH, const char* chars=0, Charset* source_charset=0) const;
1.155     paf       694:        double as_double() const { return pa_atod(cstr(), this); }
                    695:        int as_int() const { return pa_atoi(cstr(), this); }
1.167     misha     696:        bool as_bool() const { return as_int()!=0; }
1.174     misha     697:        const String& escape(Charset& source_charset) const;
1.137     paf       698: 
1.7       paf       699: private: //disabled
                    700: 
1.12      paf       701:        String& operator = (const String&) { return *this; }
1.7       paf       702: 
1.1       paf       703: };
1.119     paf       704: 
1.183     misha     705: #ifndef HASH_CODE_CACHING
1.145     paf       706: /// simple hash code of string. used by Hash
1.146     paf       707: inline uint hash_code(const String::Body self) {
1.184     misha     708:        return self.get_hash_code();
1.119     paf       709: }
1.183     misha     710: #endif
1.147     paf       711: 
                    712: 
1.1       paf       713: #endif

E-mail: