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

1.1       paf         1: /*
1.29      paf         2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.30      paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.29      paf         5: 
1.31    ! paf         6:        $Id: pa_string.h,v 1.30 2001/03/11 08:16:33 paf Exp $
1.1       paf         7: */
                      8: 
                      9: /*
                     10: 
                     11:        String                          Chunk0
                     12:        ======                          ========
1.16      paf        13:        head--------------->[ptr, size, ...]
                     14:        append_here-------->[ptr, size, ...]
                     15:                                                .
                     16:                                                .
                     17:                                                [ptr, size, ...]
                     18:        link_row----------->[link to the next chunk]
1.1       paf        19: 
                     20: */
                     21: 
                     22: #ifndef PA_STRING_H
                     23: #define PA_STRING_H
                     24: 
1.9       paf        25: #ifdef HAVE_CONFIG_H
                     26: #include "pa_config.h"
                     27: #endif
                     28: 
1.1       paf        29: #include <stddef.h>
                     30: 
1.15      paf        31: #include "pa_pool.h"
1.4       paf        32: #include "pa_types.h"
                     33: 
1.31    ! paf        34: #define UNTAINT_TIMES_BIGGER 10
        !            35: 
1.9       paf        36: #ifndef NO_STRING_ORIGIN
1.27      paf        37: #      define STRING_APPEND_PARAMS const char *src, size_t size, bool tainted, char *file, uint line
                     38: #      define APPEND(src, size, file, line) real_append(src, size, false, file, line)
                     39: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true, file, line)
1.9       paf        40: #else
1.27      paf        41: #      define STRING_APPEND_PARAMS const char *src, size_t size, bool tainted
                     42: #      define APPEND(src, size, file, line) real_append(src, size, false)
                     43: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true)
1.9       paf        44: #endif
1.23      paf        45: #define        APPEND_CONST(src) APPEND(src, 0, 0, 0)
1.9       paf        46: 
1.15      paf        47: class String : public Pooled {
1.1       paf        48: public:
                     49:        enum {
                     50:                CR_PREALLOCATED_COUNT=5,
                     51:                CR_GROW_PERCENT=60
                     52:        };
                     53: 
1.27      paf        54:        enum Untaint_lang {
1.31    ! paf        55:                UNKNOWN=0, // when get by name fails
1.27      paf        56:                NO, // clean
                     57:                YES,  // tainted, untaint language as assigned later 
                     58:                // untaint languages. assigned by ^untaint[lang]{...}
1.28      paf        59:                PASS_APPENDED,
                     60:                        // leave language built into string being appended
                     61:                        // just a flag, that value not stored
1.27      paf        62:                AS_IS,
                     63:                SQL,
                     64:                JS,
                     65:                TABLE,
                     66:                HTML,
                     67:                HTML_TYPO
                     68:        };
                     69: 
1.8       paf        70: public:
                     71: 
1.11      paf        72:        String(Pool& apool);
1.14      paf        73:        String(const String& src);
                     74:        size_t size() const { return fsize; }
                     75:        int used_rows() const { return fused_rows; }
                     76:        char *cstr() const;
1.9       paf        77:        String& real_append(STRING_APPEND_PARAMS);
1.26      paf        78:        int cmp (const String& src) const;
                     79:        bool operator < (const String& src) const {     return cmp(src)<0; }
                     80:        bool operator > (const String& src) const {     return cmp(src)>0; }
                     81:        bool operator <= (const String& src) const { return cmp(src)<=0; }
                     82:        bool operator >= (const String& src) const { return cmp(src)>=0; }
                     83:        bool operator == (const String& src) const {
                     84:                if(size()!=src.size()) // can speed up in trivial case
                     85:                        return false;
                     86:                return cmp(src)==0;
                     87:        }
                     88:        bool operator != (const String& src) const { return cmp(src)!=0; }
                     89: 
1.24      paf        90:        bool operator == (char* src) const;
1.27      paf        91:        String& append(const String& src, Untaint_lang lang);
1.8       paf        92: 
1.14      paf        93:        uint hash_code() const;
1.13      paf        94: 
                     95:        const Origin& origin() const { return head.rows[0].item.origin; }
1.8       paf        96: 
1.10      paf        97: private:
                     98: 
1.1       paf        99:        struct Chunk {
1.6       paf       100:                // the number of rows in chunk
1.1       paf       101:                int count;
                    102:                union Row {
1.27      paf       103:                        // fragment
                    104:                        struct { 
                    105:                                const char *ptr;  // pointer to the start
                    106:                                size_t size;  // length
                    107:                                Untaint_lang lang; // untaint flag, later untaint language
                    108: #ifndef NO_STRING_ORIGIN
                    109:                                Origin origin;  // origin
                    110: #endif
1.1       paf       111:                        } item;
                    112:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       113:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       114:                // next rows are here
                    115:                Chunk *preallocated_link;
                    116:        }
                    117:                head;  // the head chunk of the chunk chain
                    118: 
                    119:        // next append would write to this record
                    120:        Chunk::Row *append_here;
                    121:        
                    122:        // the address of place where lies address 
                    123:        // of the link to the next chunk to allocate
                    124:        Chunk::Row *link_row;
                    125: 
1.5       paf       126: private:
1.25      paf       127:        // last chunk
                    128:        Chunk *last_chunk;
1.5       paf       129: 
                    130:        // string size
                    131:        size_t fsize;
                    132: 
                    133:        // used rows in all chunks
                    134:        int fused_rows;
                    135: 
                    136: private:
1.1       paf       137: 
                    138:        bool chunk_is_full() {
                    139:                return append_here == link_row;
                    140:        }
                    141:        void expand();
1.27      paf       142:        void set_lang(Chunk::Row *row, Untaint_lang lang, size_t size);
1.7       paf       143: 
                    144: private: //disabled
                    145: 
1.12      paf       146:        String& operator = (const String&) { return *this; }
1.7       paf       147: 
1.1       paf       148: };
                    149: 
                    150: #endif

E-mail: