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

1.41      paf         1: /** @file
1.43      paf         2:        Parser: string class decl.
                      3: 
1.123     paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.124     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.29      paf         6: 
1.137   ! paf         7:        $Id: pa_string.h,v 1.136 2002/04/19 08:28:35 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #ifndef PA_STRING_H
                     11: #define PA_STRING_H
                     12: 
1.15      paf        13: #include "pa_pool.h"
1.4       paf        14: #include "pa_types.h"
                     15: 
1.9       paf        16: #ifndef NO_STRING_ORIGIN
1.33      paf        17: #      define STRING_APPEND_PARAMS \
                     18:                const char *src, size_t size,  \
1.119     paf        19:                uchar lang, \
1.33      paf        20:                const char *file, uint line
1.54      paf        21: /// appends piece to String  @see String::real_append
                     22: #      define APPEND(src, size, lang, file, line) \
                     23:                real_append(src, size, lang, file, line)
1.9       paf        24: #else
1.33      paf        25: #      define STRING_APPEND_PARAMS \
                     26:                const char *src, \
                     27:                size_t size, \
1.119     paf        28:                uchar lang
1.54      paf        29: /// appends piece to String  @see String::real_append
                     30: #      define APPEND(src, size, lang, file, line) \
                     31:                real_append(src, size, lang)
                     32: #endif
1.83      paf        33: /// appends clean piece to String @see String::real_append
1.54      paf        34: #define APPEND_CLEAN(src, size, file, line) \
1.62      paf        35:        APPEND(src, size, String::UL_CLEAN, file, line)
1.83      paf        36: /// appends piece to String as-is @see String::real_append
                     37: #define APPEND_AS_IS(src, size, file, line) \
                     38:        APPEND(src, size, String::UL_AS_IS, file, line)
1.48      paf        39: /// appends tainted piece to String  @see String::real_append
1.54      paf        40: #define APPEND_TAINTED(src, size, file, line) \
1.62      paf        41:        APPEND(src, size, String::UL_TAINTED, file, line)
1.48      paf        42: /// handy: appends const char* piece to String  @see String::real_append
1.83      paf        43: #define        APPEND_CONST(src) APPEND_AS_IS(src, 0, 0, 0)
1.9       paf        44: 
1.135     paf        45: class Table;
1.62      paf        46: class Array;
1.71      paf        47: class SQL_Connection;
1.101     parser     48: class Dictionary;
1.62      paf        49: 
1.42      paf        50: /** 
1.41      paf        51:        Pooled string.
                     52: 
1.47      paf        53:        Internal structure:
                     54:        @verbatim
                     55:                String                          Chunk0
                     56:                ======                          ========
                     57:                head--------------->[ptr, size, ...]
                     58:                append_here-------->[ptr, size, ...]
                     59:                                                        .
                     60:                                                        .
                     61:                                                        [ptr, size, ...]
                     62:                link_row----------->[link to the next chunk]
1.41      paf        63:        @endverbatim
                     64: 
                     65:        All pieces remember 
                     66:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     67:        - whether they are tainted or not, 
                     68:          and the language which should be used to detaint them
                     69: */
1.114     paf        70: #include "pa_pragma_pack_begin.h"
1.15      paf        71: class String : public Pooled {
1.1       paf        72: public:
1.48      paf        73: 
1.1       paf        74:        enum {
1.89      parser     75:                CR_PREALLOCATED_COUNT=2, ///< default preallocated item count
1.125     paf        76:                CR_GROW_COUNT=1 ///< each time the String chunk_is_full() string expanded()
1.1       paf        77:        };
                     78: 
1.106     parser     79:        /** piece is tainted or not. the language to use when detaint
                     80:                remember to change String_Untaint_lang_name @ untaint.C along
                     81:        */
1.27      paf        82:        enum Untaint_lang {
1.72      paf        83:                UL_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum
1.122     paf        84:                // these two must go before others, there are checks for >UL_AS_IS
1.62      paf        85:                UL_CLEAN, ///< clean
1.122     paf        86:                UL_AS_IS,     ///< leave all characters intact
                     87: 
1.48      paf        88:                UL_PASS_APPENDED,
1.41      paf        89:                        /**<
                     90:                                leave language built into string being appended.
                     91:                                just a flag, that value not stored
                     92:                        */
1.122     paf        93:                UL_TAINTED,  ///< tainted, untaint language as assigned later 
                     94:                // untaint languages. assigned by ^untaint[lang]{...}
1.103     parser     95:                UL_FILE_SPEC, ///< file specification
1.74      paf        96:                UL_HTTP_HEADER,    ///< text in HTTP response header
                     97:                UL_MAIL_HEADER,    ///< text in mail header
1.48      paf        98:                UL_URI,       ///< text in uri
                     99:                UL_TABLE,     ///< ^table:set body
                    100:                UL_SQL,       ///< ^table:sql body
                    101:                UL_JS,        ///< JavaScript code
1.102     parser    102:                UL_XML,         ///< ^dom:set xml
1.48      paf       103:                UL_HTML,      ///< HTML code (for editing)
1.119     paf       104:                UL_OPTIMIZE_BIT = 0x80  ///< flag, requiring cstr whitespace optimization
1.27      paf       105:        };
                    106: 
1.8       paf       107: public:
                    108: 
1.77      paf       109:        String(Pool& apool, const char *src=0, size_t src_size=0, bool tainted=false);
1.14      paf       110:        String(const String& src);
1.135     paf       111:        bool is_empty() const { return append_here==head.chunk.rows; }
1.117     paf       112:        size_t size() const;
1.69      paf       113:        /// convert to C string. if 'lang' known, forcing 'lang' to it
1.109     paf       114:        char *cstr(Untaint_lang lang=UL_AS_IS, 
1.82      paf       115:                SQL_Connection *connection=0,
1.133     paf       116:                Charset *cstr_charset=0, const char *cstr_charset_name=0) const {
1.88      parser    117: 
1.121     paf       118:                char *result=(char *)malloc(cstr_bufsize(lang, connection, cstr_charset));
1.133     paf       119:                char *eol=store_to(result, lang, connection, cstr_charset, cstr_charset_name);
1.50      paf       120:                *eol=0;
                    121:                return result;
                    122:        }
1.117     paf       123:        char *cstr_debug_origins() const;
1.108     parser    124:        /// puts pieces to buf
                    125:        void serialize(size_t prolog_size,  void *& buf, size_t& buf_size) const;
                    126:        /// appends pieces from buf to self
1.132     paf       127:        bool deserialize(size_t prolog_size, void *buf, size_t buf_size, const char *file);
1.46      paf       128:        /** append fragment
1.83      paf       129:                @see APPEND_AS_IS, APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46      paf       130:        */
1.9       paf       131:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       132:        /// @return <0 ==0 or >0 depending on comparison result
1.62      paf       133:        int cmp (int& partial, const String& src, 
1.72      paf       134:                size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.56      paf       135:        bool operator < (const String& src) const {     int p; return cmp(p, src)<0; }
                    136:        bool operator > (const String& src) const {     int p; return cmp(p, src)>0; }
                    137:        bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
                    138:        bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26      paf       139:        bool operator == (const String& src) const {
                    140:                if(size()!=src.size()) // can speed up in trivial case
                    141:                        return false;
1.56      paf       142:                int p; return cmp(p, src)==0;
1.26      paf       143:        }
1.56      paf       144:        bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26      paf       145: 
1.50      paf       146:        /**
                    147:                 @param partial 
                    148:                        returns partial match status. 
1.51      paf       149:                        - -1: strings too different
                    150:                        -  0: full match
                    151:                        -  1: means @c this starts @c src
                    152:                        -  2: means @src starts @this
1.50      paf       153:        */
1.61      paf       154:        int cmp(int& partial, const char* src_ptr, size_t src_size=0, 
1.72      paf       155:                size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.50      paf       156:        bool operator == (const char* src_ptr) const { 
                    157:                size_t src_size=src_ptr?strlen(src_ptr):0;
                    158:                if(size() != src_size)
                    159:                        return false;
                    160:                int partial; // unused
1.56      paf       161:                return cmp(partial, src_ptr, src_size)==0; 
1.50      paf       162:        }
1.80      paf       163:        bool operator != (const char* src_ptr) const { 
                    164:                int partial; // unused
                    165:                return cmp(partial, src_ptr, 0)!=0; 
                    166:        }
                    167: 
1.119     paf       168:        String& append(const String& src, uchar lang, bool forced=false);
1.76      paf       169:        String& operator << (const String& src) { return append(src, UL_PASS_APPENDED); }
                    170:        String& operator << (const char *src) { return APPEND_CONST(src); }
1.8       paf       171: 
1.41      paf       172:        /// simple hash code of string. used by Hash
1.14      paf       173:        uint hash_code() const;
1.100     parser    174: 
                    175:        /// extracts first char of a string
                    176:        char first_char() const;
1.54      paf       177: 
                    178:        /// extracts [start, finish) piece of string
1.70      paf       179:        String& mid(size_t start, size_t finish) const;
1.55      paf       180: 
1.59      paf       181:        /// @return position of substr in string, -1 means "not found" [String version]
1.62      paf       182:        int pos(const String& substr, 
1.112     paf       183:                int this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.59      paf       184:        /// @return position of substr in string, -1 means "not found" [const char* version]
1.97      parser    185:        int pos(const char *substr, size_t substr_size=0, 
1.112     paf       186:                int this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.62      paf       187: 
                    188:        void split(Array& result, 
                    189:                size_t *pos_after_ref, 
                    190:                const char *delim, size_t delim_size, 
1.86      paf       191:                Untaint_lang lang=UL_UNSPECIFIED, int limit=-1) const;
1.62      paf       192:        void split(Array& result, 
                    193:                size_t *pos_after_ref, 
                    194:                const String& delim, 
1.86      paf       195:                Untaint_lang lang=UL_UNSPECIFIED, int limit=-1) const;
1.62      paf       196: 
1.136     paf       197:        typedef void (*Row_action)(Table& table, Array *row, 
                    198:                int prestart, int prefinish, 
                    199:                int poststart, int postfinish,
1.68      paf       200:                void *info);
1.87      parser    201:        /**
                    202:                @return true if fills table.
                    203:                table format is defined and fixed[can be used by others]: 
                    204:                @verbatim
                    205:                        prematch/match/postmatch/1/2/3/...
                    206:                @endverbatim
                    207:        */
1.137   ! paf       208:        bool match(const char *acstr,
1.81      paf       209:                const String *aorigin,          
1.64      paf       210:                const String& regexp, 
1.65      paf       211:                const String *options,
1.66      paf       212:                Table **table,
1.99      parser    213:                Row_action row_action, void *info,
                    214:                bool *was_global=0) const;
1.87      parser    215:        enum Change_case_kind {
                    216:                CC_UPPER,
                    217:                CC_LOWER
                    218:        };
1.120     paf       219:        String& change_case(Pool& pool,
1.87      parser    220:                Change_case_kind kind) const;
1.101     parser    221:        String& replace(Pool& pool, Dictionary& dict) const;
1.96      parser    222:        double as_double() const;
                    223:        int as_int() const;
1.137   ! paf       224: 
        !           225:        bool is_join_chains_profitable() const;
        !           226:        String& join_chains(Pool& pool, char** cstr) const;
1.13      paf       227: 
1.41      paf       228: #ifndef NO_STRING_ORIGIN
                    229:        /// origin of string. calculated by first row
1.43      paf       230:        const Origin& origin() const;
1.41      paf       231: #endif
1.8       paf       232: 
1.129     paf       233: private:
1.10      paf       234: 
1.128     paf       235:        /** several String fragments
                    236:        */
1.135     paf       237:        struct Chunk {
1.125     paf       238:                typedef uchar count_type;
                    239:                count_type count; ///< the number of rows in chunk
1.135     paf       240:                // here could be some padding bytes
1.84      paf       241:                /// string fragment or a link to next chunk union
1.135     paf       242:                typedef union Row {
1.125     paf       243:                        typedef uchar item_size_type;
1.84      paf       244:                        /// fragment
1.27      paf       245:                        struct { 
1.84      paf       246:                                const char *ptr;  ///< pointer to the start
1.125     paf       247:                                item_size_type size;  ///< length
                    248:                                uchar/*Untaint_lang*/ lang; ///< untaint flag, later untaint language
1.27      paf       249: #ifndef NO_STRING_ORIGIN
1.84      paf       250:                                Origin origin;  ///< origin
1.27      paf       251: #endif
1.1       paf       252:                        } item;
1.135     paf       253:                        // we are using the fact that there's no padding before this field!
1.84      paf       254:                        Chunk *link;  ///< link to the next chunk in chain
1.135     paf       255:                } rows_type[CR_PREALLOCATED_COUNT];
                    256:                rows_type rows;
                    257:        };
                    258:        /**
                    259:                'mutable' because can write after it's end, after it was appended to somebody 
                    260:                @see String::append
                    261:        */
                    262:        mutable struct {
                    263:                Chunk chunk;
                    264:                Chunk *link_storage;
                    265:        } head;  ///< the head chunk of the chunk chain
1.1       paf       266: 
1.84      paf       267:        /// next append would write to this record
1.1       paf       268:        Chunk::Row *append_here;
                    269:        
1.5       paf       270: private:
1.110     paf       271:        /// last chunk
1.125     paf       272:        mutable Chunk *last_chunk;
1.5       paf       273: 
                    274: private:
1.1       paf       275: 
                    276:        bool chunk_is_full() {
1.127     paf       277:                return append_here == last_chunk->rows+last_chunk->count;
1.1       paf       278:        }
1.129     paf       279:        uint used_rows() const;
1.1       paf       280:        void expand();
1.125     paf       281:        
                    282:        Untaint_lang lang_of(size_t offset) const;
1.79      paf       283: 
1.116     paf       284:        size_t cstr_bufsize(Untaint_lang lang,
                    285:                SQL_Connection *connection,
1.121     paf       286:                Charset *buf_charset) const;
1.79      paf       287:        /// convert to C string, store to 'dest' which must be big enough for proper untaint
1.82      paf       288:        char *store_to(char *dest, Untaint_lang lang=UL_UNSPECIFIED, 
                    289:                SQL_Connection *connection=0,
1.133     paf       290:                Charset *store_to_charset=0,
                    291:                const char *store_to_charset_name=0) const;
1.107     parser    292: 
                    293:        void join_chain(Pool& pool, 
1.130     paf       294:                                           const Chunk*& achunk, const Chunk::Row*& arow, uint& acountdown, 
1.119     paf       295:                                           uchar& joined_lang, const char *& joined_ptr, size_t& joined_size) const;
1.7       paf       296: 
                    297: private: //disabled
                    298: 
1.12      paf       299:        String& operator = (const String&) { return *this; }
1.7       paf       300: 
1.1       paf       301: };
1.114     paf       302: #include "pa_pragma_pack_end.h"
1.119     paf       303: 
1.130     paf       304: #define STRING_PREPARED_FOREACH_ROW(self, body) \
                    305:        while(row!=(self).append_here) { \
                    306:                if(countdown==0) { \
                    307:                        chunk=row->link; \
                    308:                        row=chunk->rows; \
                    309:                        countdown=chunk->count; \
                    310:                }; \
                    311:                { body } \
1.131     paf       312:                row++; countdown--; \
1.130     paf       313:        } 
                    314: 
                    315: #define STRING_PREFIX_FOREACH_ROW(self, body) { \
1.135     paf       316:        const Chunk *chunk=&(self).head.chunk;  \
1.130     paf       317:        const Chunk::Row *row=chunk->rows; \
                    318:        uint countdown=chunk->count; \
                    319:        STRING_PREPARED_FOREACH_ROW(self, body) \
1.119     paf       320: }
                    321: 
                    322: #define STRING_FOREACH_ROW(body) STRING_PREFIX_FOREACH_ROW(*this, body)
                    323: #define STRING_SRC_FOREACH_ROW(body) STRING_PREFIX_FOREACH_ROW(src, body)
1.1       paf       324: 
                    325: #endif

E-mail: