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

1.41      paf         1: /** @file
1.43      paf         2:        Parser: string class decl.
                      3: 
1.145     paf         4:        Copyright (c) 2001-2003 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.152   ! paf        11: static const char* IDENT_STRING_H="$Date: 2003/10/21 05:11:00 $";
1.145     paf        12: 
                     13: // includes
1.1       paf        14: 
1.4       paf        15: #include "pa_types.h"
1.145     paf        16: #include "pa_array.h"
                     17: 
                     18: extern "C" { // cord's author forgot to do that
                     19: #define CORD_NO_IO
                     20: #include "cord.h"
                     21: };
1.4       paf        22: 
1.146     paf        23: // cord extension
                     24: /* Returns true if x does contain                                       */
                     25: /* char not_c at positions i..i+n. Value i,i+n must be < CORD_len(x).  */
                     26: int CORD_range_contains_chr_greater_then(CORD x, size_t i, size_t n, int c);
1.148     paf        27: size_t CORD_block_count(CORD x);
1.146     paf        28: 
1.145     paf        29: // forwards
1.9       paf        30: 
1.145     paf        31: class Charset;
1.135     paf        32: class Table;
1.71      paf        33: class SQL_Connection;
1.101     parser     34: class Dictionary;
1.145     paf        35: class Request_charsets;
                     36: class String;
                     37: typedef Array<const String*> ArrayString;
                     38: 
                     39: /// this is result of pos functions which mean that substr were not found
                     40: #define STRING_NOT_FOUND ((size_t)-1)
                     41: 
1.146     paf        42: template<typename T>
                     43: inline size_t get_length(T current) {
                     44:        return current;
                     45: }
1.147     paf        46: 
1.42      paf        47: /** 
1.146     paf        48:        String which knows the lang of all it's langs.
1.41      paf        49: 
                     50:        All pieces remember 
                     51:        - whether they are tainted or not, 
1.146     paf        52:          and the lang which should be used to detaint them
1.41      paf        53: */
1.145     paf        54: class String: public PA_Object {
1.1       paf        55: public:
1.48      paf        56: 
1.146     paf        57:        /** piece is tainted or not. the lang to use when detaint
1.106     parser     58:                remember to change String_Untaint_lang_name @ untaint.C along
1.148     paf        59: 
                     60:                WARNING WARNING WARNING WARNING WARNING WARNING 
                     61:                
                     62:                pos function compares(<=) languages, that is used in searching
                     63:                for table column separator being L_CLEAN or L_AS_IS.
                     64:                they search for AS_IS, meaning AS_IS|CLEAN [doing <=L_AS_IS check].
                     65:                
                     66:                letters assigned for debugging, but it's important for no language-letter
                     67:                come before L_AS_IS other then L_CLEAN
                     68: 
                     69:                WARNING WARNING WARNING WARNING WARNING WARNING 
1.106     parser     70:        */
1.145     paf        71:        enum Language {
1.146     paf        72:                L_UNSPECIFIED=0, ///< no real string has parts of this lange: it's just convinient to check when string's empty
1.145     paf        73:                // these two must go before others, there are checks for >L_AS_IS
1.148     paf        74:                L_CLEAN='0', ///< clean  WARNING: read above warning before changing
                     75:                L_AS_IS='A',     ///< leave all characters intact  WARNING: read above warning before changing
1.122     paf        76: 
1.148     paf        77:                L_PASS_APPENDED='P',
1.41      paf        78:                        /**<
1.146     paf        79:                                leave lang built into string being appended.
1.41      paf        80:                                just a flag, that value not stored
                     81:                        */
1.148     paf        82:                L_TAINTED='T',  ///< tainted, untaint lang as assigned later 
1.146     paf        83:                // untaint langs. assigned by ^untaint[lang]{...}
1.148     paf        84:                L_FILE_SPEC='F', ///< file specification
                     85:                L_HTTP_HEADER='h',    ///< text in HTTP response header
                     86:                L_MAIL_HEADER='m',    ///< text in mail header
                     87:                L_URI='U',       ///< text in uri
                     88:                L_TABLE='L',     ///< ^table:set body
                     89:                L_SQL='Q',       ///< ^table:sql body
                     90:                L_JS='J',        ///< JavaScript code
                     91:                L_XML='X',              ///< ^dom:set xml
                     92:                L_HTML='H',      ///< HTML code (for editing)
                     93:                // READ WARNING ABOVE BEFORE ADDING ANYTHING
1.146     paf        94:                L_OPTIMIZE_BIT = 0x80  ///< flag, requiring cstr whitespace optimization
1.27      paf        95:        };
                     96: 
1.147     paf        97:        union Languages {
1.146     paf        98: 
1.147     paf        99:                struct {
                    100:                        Language lang:8;
                    101:                        int is_not_just_lang:sizeof(CORD)*8-8;
                    102:                } opt;
                    103:                CORD langs;
1.146     paf       104: 
                    105:                template<typename C>
                    106:                CORD make_langs(C current) const {
1.147     paf       107:                        return opt.is_not_just_lang?
1.146     paf       108:                                langs
1.147     paf       109:                                :CORD_chars((char)opt.lang, get_length(current));
1.146     paf       110:                }
                    111: 
                    112:                CORD make_langs(size_t aoffset, size_t alength)  const {
1.147     paf       113:                        return opt.is_not_just_lang?
1.146     paf       114:                                CORD_substr(langs, aoffset, alength)
1.147     paf       115:                                :CORD_chars((char)opt.lang, alength);
1.145     paf       116:                }
                    117: 
1.146     paf       118:                /// appending when 'langs' already contain something [simple cases hanled elsewhere]
                    119:                template<typename C>
                    120:                void append(C current, 
                    121:                        const CORD to_nonempty_target_langs) {
                    122:                        assert(langs);
                    123: 
1.147     paf       124:                        if(opt.is_not_just_lang)
1.146     paf       125:                                langs=CORD_cat(langs, to_nonempty_target_langs);
                    126:                        else { // we were "just lang"
                    127:                                size_t current_size=get_length(current);
                    128:                                assert(current_size);
                    129:                                langs=CORD_cat(
1.147     paf       130:                                        CORD_chars((char)opt.lang, current_size),  // first piece [making from just 'lang']
1.146     paf       131:                                        to_nonempty_target_langs); // new piece
                    132:                        }
1.145     paf       133:                }
1.146     paf       134: 
1.145     paf       135:        public:
1.146     paf       136: 
                    137:                const char* v() const;
                    138: 
                    139:                Languages(): langs(0) {}
1.147     paf       140:                Languages(Language alang) {
                    141:                        opt.lang=alang;
                    142:                        opt.is_not_just_lang=0;
                    143:                }
1.146     paf       144: 
                    145:                /// MUST be called exactly prior to modification of current [uses it's length]
                    146:                template<typename C>
                    147:                void append(C current, Language alang, size_t asize) {
                    148:                        assert(alang);
                    149:                        assert(asize);
                    150: 
1.147     paf       151:                        if(!opt.is_not_just_lang)
                    152:                                if(opt.lang) {
                    153:                                        if(opt.lang==alang) // same length? ignoring
1.146     paf       154:                                                return;
                    155:                                } else {
1.147     paf       156:                                        opt.lang=alang; // to uninitialized
1.146     paf       157:                                        return;
1.145     paf       158:                                }
1.146     paf       159: 
                    160:                        append(current, CORD_chars((char)alang, asize));
                    161:                }
                    162: 
                    163:                /// MUST be called exactly prior to modification of current [uses it's length]
                    164:                template<typename C>
                    165:                void append(C current, size_t appending_length, 
                    166:                        const Languages src) {
                    167:                        assert(appending_length);
                    168: 
                    169:                        if(!langs)
                    170:                                langs=src.langs; // to uninitialized
1.147     paf       171:                        else if(!src.opt.is_not_just_lang)
                    172:                                append(current, src.opt.lang, appending_length); // simplifying when simple source
1.146     paf       173:                        else
                    174:                                append(current, src.make_langs(appending_length));
                    175:                }
                    176:                
                    177:                /// MUST be called exactly prior to modification of current [uses it's length]
                    178:                template<typename C>
                    179:                void append(C current,
                    180:                        const Languages src, size_t aoffset, size_t alength) {
                    181:                        assert(alength);
                    182: 
                    183:                        if(!langs) // to uninitialized?
1.147     paf       184:                                if(src.opt.is_not_just_lang)
1.146     paf       185:                                        langs=CORD_substr(src.langs, aoffset, alength); // to uninitialized complex
                    186:                                else
1.147     paf       187:                                        opt.lang=src.opt.lang; // to uninitialized simple
1.146     paf       188:                        else 
1.147     paf       189:                                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       190:                                        return; // ignoring
                    191:                                else
                    192:                                        append(current, src.make_langs(aoffset, alength));
                    193:                }
                    194: 
                    195:                /// checks if we have lang<=alang all from aoffset to aoffset+alength
                    196:                bool check_lang(Language alang, size_t aoffset, size_t alength) const {
                    197:                        if(alang==L_UNSPECIFIED) // ignore lang?
                    198:                                return true;
                    199: 
1.147     paf       200:                        if(opt.is_not_just_lang)
1.146     paf       201:                                return CORD_range_contains_chr_greater_then(langs, aoffset, alength, (unsigned)alang)==0;
                    202:                        else
1.147     paf       203:                                return opt.lang<=alang;
1.146     paf       204:                }
                    205: 
1.148     paf       206:                /// @returns count of blocks
                    207:                /// @todo currently there can be adjucent blocks of same language. someday merge them
                    208:                size_t count() const {
                    209:                        return opt.is_not_just_lang?
                    210:                                CORD_block_count(langs)
                    211:                                : opt.lang?
                    212:                                        1
                    213:                                        : 0;
                    214:                };
                    215: 
1.146     paf       216:                template<typename C, typename I> 
                    217:                void for_each(C current, 
                    218:                        int callback(char, size_t, I), I info) const {
                    219:                        
1.147     paf       220:                        if(opt.is_not_just_lang)
1.146     paf       221:                                CORD_block_iter(langs, 0, (CORD_block_iter_fn)callback, info);
                    222:                        else
1.147     paf       223:                                callback(opt.lang, get_length(current), info);
1.146     paf       224:                }
                    225: 
                    226:                bool invariant(size_t current_length) {
                    227:                        if(!langs)
                    228:                                return current_length==0;
1.147     paf       229:                        if(opt.is_not_just_lang)
1.146     paf       230:                                return CORD_len(langs)==current_length;
                    231:                        return true; // uncheckable, actually
                    232:                }
                    233:        };
                    234: 
                    235:        class Body {
                    236: 
                    237:                CORD body;
                    238: 
                    239:        public:
                    240: 
                    241:                const char* v() const;
                    242: 
                    243:                Body(): body(CORD_EMPTY) {}
                    244:                Body(CORD abody): body(abody) {
                    245:                        assert(!body // no body
                    246:                                || *body // ordinary string
                    247:                                || body[1]==1 // CONCAT_HDR
                    248:                                || body[1]==4 // FN_HDR 
                    249:                                || body[1]==6 // SUBSTR_HDR 
                    250:                                );
                    251:                }
                    252:                /// WARNING: length is only HELPER length, str in ANY case should be zero-terminated
                    253:                Body(const char* str, size_t helper_length): body(CORD_EMPTY) {
                    254:                        append_know_length(str, helper_length?helper_length:strlen(str));
                    255:                }
                    256:                static Body Format(int value);
                    257: 
                    258:                void clear() { body=CORD_EMPTY; }
                    259: 
                    260:                bool operator! () const { return is_empty(); }
                    261: 
                    262:                uint hash_code() const;
                    263: 
                    264:                const char* cstr() const { return CORD_to_const_char_star(body); }
                    265:                char* cstrm() const { return CORD_to_char_star(body); }
                    266: 
                    267:                size_t length() const { return CORD_len(body); }
                    268: 
                    269:                bool is_empty() const { return body==CORD_EMPTY; }
                    270: 
                    271:                void append_know_length(const char *str, size_t known_length) {
                    272:                        if(known_length)
                    273:                                body=CORD_cat_char_star(body, str, known_length);
                    274:                }
                    275:                void append_strdup_know_length(const char* str, size_t known_length) {
                    276:                        if(known_length)
                    277:                                append_know_length(pa_strdup(str, known_length), known_length);
                    278:                }
                    279:                void append(char c) { body=CORD_cat_char(body, c); }
                    280:                Body& operator << (const Body src) { body=CORD_cat(body, src.body); return *this; }
                    281:                Body& operator << (const char* str) { append_know_length(str, strlen(str)); return *this; }
                    282: 
                    283:                // could not figure out why this operator is needed [should do this chain: string->simple->==]
                    284:                bool operator < (const Body src) const { return CORD_cmp(body, src.body)<0; }
                    285:                bool operator > (const Body src) const { return CORD_cmp(body, src.body)>0; }
                    286:                bool operator <= (const Body src) const { return CORD_cmp(body, src.body)<=0; }
                    287:                bool operator >= (const Body src) const { return CORD_cmp(body, src.body)>=0; }
                    288:                bool operator != (const Body src) const { return CORD_cmp(body, src.body)!=0; }
                    289:                bool operator == (const Body src) const { return CORD_cmp(body, src.body)==0; }
                    290: 
                    291:                int ncmp(size_t x_begin, const Body y, size_t y_begin, size_t size) const {
                    292:                        return CORD_ncmp(body, x_begin, y.body, y_begin, size);
1.145     paf       293:                }
                    294: 
1.146     paf       295:                char fetch(size_t index) const { return CORD_fetch(body, index); }
                    296:                Body mid(size_t index, size_t length) const { return CORD_substr(body, index, length); }
                    297:                size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr); }
                    298:                size_t pos(const Body substr, size_t offset=0) const { 
                    299:                        if(!substr.length())
                    300:                                return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1]
1.150     paf       301: 
                    302:                        // CORD_str checks for bad offset [CORD_chr does not]
1.146     paf       303:                        return CORD_str(body, offset, substr.body); 
                    304:                }
                    305:                size_t pos(char c, 
                    306:                        size_t offset=0) const {
1.150     paf       307:                        if(offset>=length()) // CORD_chr does not check that [and ABORT's in that case]
                    308:                                return STRING_NOT_FOUND;
                    309: 
1.146     paf       310:                        return CORD_chr(body, offset, c);
1.145     paf       311:                }
1.146     paf       312: 
1.149     paf       313:                template<typename I> void for_each(
                    314:                        int (*f1)(char c, I), 
                    315:                        int (*f2)(const char* s, I), 
                    316:                        I info) const {
                    317:                        CORD_iter5(body, 0, (CORD_iter_fn)f1, (CORD_batched_iter_fn)f2, info);
1.148     paf       318:                }
1.146     paf       319: 
                    320:                void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); }
                    321: 
                    322:                /*Body normalize() const {
                    323:                        return Body(CORD_balance(body));
                    324:                }*/
1.145     paf       325:        };
                    326: 
                    327:        struct C {
                    328:                const char *str;
                    329:                size_t length;
                    330:                operator const char *() { return str; }
                    331:                C(const char *astr, size_t asize): str(astr), length(asize) {}
                    332:        };
                    333: 
                    334:        struct Cm {
                    335:                char *str;
                    336:                size_t length;
                    337:                //operator char *() { return str; }
                    338:                Cm(char *astr, size_t asize): str(astr), length(asize) {}
                    339:        };
                    340: 
                    341: private:
                    342: 
1.152   ! paf       343:        Body body; ///< all characters of string
1.146     paf       344:        Languages langs; ///< string characters lang
                    345: 
                    346:        const char* v() const;
                    347: 
                    348: #define ASSERT_STRING_INVARIANT(string) \
                    349:        assert((string).langs.invariant((string).body.length()))
1.145     paf       350: 
1.8       paf       351: public:
1.151     paf       352: 
                    353:        static const String Empty;
1.8       paf       354: 
1.145     paf       355:        explicit String(const char* cstr=0, size_t helper_length=0, bool tainted=false);
                    356:        explicit String(const C cstr, bool tainted=false);
1.146     paf       357:        String(Body abody, Language alang): body(abody), langs(alang) {
                    358:                assert(!body.is_empty());
                    359:                ASSERT_STRING_INVARIANT(*this);
                    360:        }
                    361:        String(const String& src): body(src.body), langs(src.langs) {
                    362:                ASSERT_STRING_INVARIANT(*this);
1.145     paf       363:        }
                    364: 
                    365:        /// for convinient hash lookup
1.146     paf       366:        operator const Body() const { return body; }
1.145     paf       367: 
                    368:        bool is_empty() const { return body.is_empty(); }
                    369:        size_t length() const { return body.length(); }
                    370: 
                    371:        /// convert to CORD. if 'lang' known, forcing 'lang' to it
1.146     paf       372:        Body cstr_to_string_body(Language lang=L_AS_IS, 
1.145     paf       373:                SQL_Connection* connection=0,
                    374:                const Request_charsets *charsets=0) const;
                    375: 
                    376:        /// convert to constant C string. if 'lang' known, forcing 'lang' to it
                    377:        const char* cstr(Language lang=L_AS_IS, 
                    378:                SQL_Connection* connection=0,
                    379:                const Request_charsets *charsets=0) const {
                    380:                return cstr_to_string_body(lang, connection, charsets).cstr();
                    381:        }
                    382:        /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it
                    383:        char *cstrm(Language lang=L_AS_IS, 
                    384:                SQL_Connection* connection=0,
                    385:                const Request_charsets *charsets=0) const {
                    386:                return cstr_to_string_body(lang, connection, charsets).cstrm();
1.50      paf       387:        }
1.108     parser    388:        /// puts pieces to buf
1.145     paf       389:        Cm serialize(size_t prolog_size) const;
1.108     parser    390:        /// appends pieces from buf to self
1.145     paf       391:        bool deserialize(size_t prolog_size, void *buf, size_t buf_size);
1.146     paf       392:        /// @see Body::append_know_length
1.145     paf       393:        String& append_know_length(const char* str, size_t known_length, Language lang);
1.146     paf       394:        /// @see Body::append_help_length
1.145     paf       395:        String& append_help_length(const char* str, size_t helper_length, Language lang);
                    396:        String& append_strdup(const char* str, size_t helper_length, Language lang);
                    397: 
1.146     paf       398:        bool operator == (const char* y) const { return body==Body(y); }
                    399:        bool operator != (const char* y) const { return body!=Body(y); }
1.145     paf       400: 
                    401:        /// this starts with y
                    402:        bool starts_with(const char* y) const {
1.146     paf       403:                return body.ncmp(0/*x_begin*/, Body(y), 0/*y_begin*/, strlen(y))==0;
1.145     paf       404:        }
                    405:        /// x starts with this
                    406:        bool this_starts(const char* x) const {
1.146     paf       407:                return Body(x).ncmp(0/*x_begin*/, body, 0/*y_begin*/, length())==0;
1.26      paf       408:        }
                    409: 
1.145     paf       410:        String& append_to(String& dest, Language lang, bool forced) const;
                    411:        String& append(const String& src, Language lang, bool forced=false) { 
                    412:                return src.append_to(*this, lang, forced);
                    413:        }
                    414:        String& operator << (const String& src) { return append(src, L_PASS_APPENDED); }
                    415:        String& operator << (const char* src) { return append_help_length(src, 0, L_AS_IS); }
1.147     paf       416:        String& operator << (const Body src);
1.100     parser    417: 
1.142     paf       418:        /// extracts first char of a string, if any
                    419:        char first_char() const {
1.145     paf       420:                return is_empty()?0:body.fetch(0);
1.142     paf       421:        }
1.54      paf       422: 
1.145     paf       423:        bool operator < (const String& src) const { return body<src.body; }
                    424:        bool operator > (const String& src) const { return body>src.body; }
                    425:        bool operator <= (const String& src) const { return body<=src.body; }
                    426:        bool operator >= (const String& src) const { return body>=src.body; }
                    427:        bool operator != (const String& src) const { return body!=src.body; }
                    428:        bool operator == (const String& src) const { return body==src.body; }
                    429: 
1.54      paf       430:        /// extracts [start, finish) piece of string
1.145     paf       431:        String& mid(size_t substr_begin, size_t substr_end) const;
                    432: 
                    433:        /** 
                    434:                ignore lang if it's L_UNSPECIFIED
                    435:                but when specified: look for substring that lies in ONE fragment in THAT lang
                    436:                @return position of substr in string, -1 means "not found" [const char* version]
                    437:        */
1.146     paf       438:        size_t pos(const Body substr, 
1.145     paf       439:                size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
                    440:        /// String version of @see pos(const char*, int, Language)
                    441:        size_t pos(const String& substr, 
                    442:                size_t this_offset=0, Language lang=L_UNSPECIFIED) const;
                    443:        size_t pos(char c, 
                    444:                size_t this_offset=0) const {
                    445:                return body.pos(c, this_offset);
                    446:        }
1.55      paf       447: 
1.145     paf       448:        void split(ArrayString& result, 
                    449:                size_t& pos_after,
                    450:                const char* delim, 
                    451:                Language lang=L_UNSPECIFIED, int limit=-1) const;
                    452:        void split(ArrayString& result, 
                    453:                size_t& pos_after, 
1.62      paf       454:                const String& delim, 
1.145     paf       455:                Language lang=L_UNSPECIFIED, int limit=-1) const;
1.62      paf       456: 
1.145     paf       457:        typedef void (*Row_action)(Table& table, ArrayString* row, 
1.136     paf       458:                int prestart, int prefinish, 
                    459:                int poststart, int postfinish,
1.68      paf       460:                void *info);
1.87      parser    461:        /**
1.145     paf       462:                @return table of found items, if any.
1.87      parser    463:                table format is defined and fixed[can be used by others]: 
                    464:                @verbatim
                    465:                        prematch/match/postmatch/1/2/3/...
                    466:                @endverbatim
                    467:        */
1.145     paf       468:        Table* match(Charset& source_charset,
1.64      paf       469:                const String& regexp, 
1.145     paf       470:                const String* options,
1.99      parser    471:                Row_action row_action, void *info,
1.145     paf       472:                bool& just_matched) const;
1.87      parser    473:        enum Change_case_kind {
                    474:                CC_UPPER,
                    475:                CC_LOWER
                    476:        };
1.145     paf       477:        String& change_case(Charset& source_charset,
1.87      parser    478:                Change_case_kind kind) const;
1.145     paf       479:        const String& replace(const Dictionary& dict) const;
1.96      parser    480:        double as_double() const;
                    481:        int as_int() const;
1.137     paf       482: 
1.7       paf       483: private: //disabled
                    484: 
1.12      paf       485:        String& operator = (const String&) { return *this; }
1.7       paf       486: 
1.1       paf       487: };
1.119     paf       488: 
1.146     paf       489: template<>
                    490: inline size_t get_length<String::Body>(String::Body body) {
                    491:        return body.length();
                    492: }
                    493: 
1.145     paf       494: /// simple hash code of string. used by Hash
1.146     paf       495: inline uint hash_code(const String::Body self) {
1.145     paf       496:        return self.hash_code();
1.119     paf       497: }
1.147     paf       498: 
                    499: 
                    500: /// now that we've declared specialization we can use it
                    501: inline String& String::operator << (const String::Body src) { 
                    502:        langs.append(body, L_AS_IS, src.length());
                    503:        body<<src;
                    504:        return *this;
                    505: }
                    506: 
1.1       paf       507: 
                    508: #endif

E-mail: