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

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

E-mail: