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

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.33    ! paf         6:        $Id: pa_string.h,v 1.32 2001/03/12 09:08:48 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.33    ! paf        37: #      define STRING_APPEND_PARAMS \
        !            38:                const char *src, size_t size,  \
        !            39:                bool tainted, \
        !            40:                const char *file, uint line
1.27      paf        41: #      define APPEND(src, size, file, line) real_append(src, size, false, file, line)
                     42: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true, file, line)
1.9       paf        43: #else
1.33    ! paf        44: #      define STRING_APPEND_PARAMS \
        !            45:                const char *src, \
        !            46:                size_t size, \
        !            47:                bool tainted
1.27      paf        48: #      define APPEND(src, size, file, line) real_append(src, size, false)
                     49: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true)
1.9       paf        50: #endif
1.23      paf        51: #define        APPEND_CONST(src) APPEND(src, 0, 0, 0)
1.9       paf        52: 
1.15      paf        53: class String : public Pooled {
1.1       paf        54: public:
                     55:        enum {
                     56:                CR_PREALLOCATED_COUNT=5,
                     57:                CR_GROW_PERCENT=60
                     58:        };
                     59: 
1.27      paf        60:        enum Untaint_lang {
1.31      paf        61:                UNKNOWN=0, // when get by name fails
1.27      paf        62:                NO, // clean
                     63:                YES,  // tainted, untaint language as assigned later 
                     64:                // untaint languages. assigned by ^untaint[lang]{...}
1.28      paf        65:                PASS_APPENDED,
                     66:                        // leave language built into string being appended
                     67:                        // just a flag, that value not stored
1.27      paf        68:                AS_IS,
1.33    ! paf        69:                TABLE,
1.27      paf        70:                SQL,
                     71:                JS,
                     72:                HTML,
                     73:                HTML_TYPO
                     74:        };
                     75: 
1.8       paf        76: public:
                     77: 
1.11      paf        78:        String(Pool& apool);
1.14      paf        79:        String(const String& src);
                     80:        size_t size() const { return fsize; }
                     81:        int used_rows() const { return fused_rows; }
                     82:        char *cstr() const;
1.9       paf        83:        String& real_append(STRING_APPEND_PARAMS);
1.26      paf        84:        int cmp (const String& src) const;
                     85:        bool operator < (const String& src) const {     return cmp(src)<0; }
                     86:        bool operator > (const String& src) const {     return cmp(src)>0; }
                     87:        bool operator <= (const String& src) const { return cmp(src)<=0; }
                     88:        bool operator >= (const String& src) const { return cmp(src)>=0; }
                     89:        bool operator == (const String& src) const {
                     90:                if(size()!=src.size()) // can speed up in trivial case
                     91:                        return false;
                     92:                return cmp(src)==0;
                     93:        }
                     94:        bool operator != (const String& src) const { return cmp(src)!=0; }
                     95: 
1.24      paf        96:        bool operator == (char* src) const;
1.27      paf        97:        String& append(const String& src, Untaint_lang lang);
1.8       paf        98: 
1.14      paf        99:        uint hash_code() const;
1.13      paf       100: 
                    101:        const Origin& origin() const { return head.rows[0].item.origin; }
1.8       paf       102: 
1.10      paf       103: private:
                    104: 
1.1       paf       105:        struct Chunk {
1.6       paf       106:                // the number of rows in chunk
1.1       paf       107:                int count;
                    108:                union Row {
1.27      paf       109:                        // fragment
                    110:                        struct { 
                    111:                                const char *ptr;  // pointer to the start
                    112:                                size_t size;  // length
                    113:                                Untaint_lang lang; // untaint flag, later untaint language
                    114: #ifndef NO_STRING_ORIGIN
                    115:                                Origin origin;  // origin
                    116: #endif
1.1       paf       117:                        } item;
                    118:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       119:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       120:                // next rows are here
                    121:                Chunk *preallocated_link;
                    122:        }
                    123:                head;  // the head chunk of the chunk chain
                    124: 
                    125:        // next append would write to this record
                    126:        Chunk::Row *append_here;
                    127:        
                    128:        // the address of place where lies address 
                    129:        // of the link to the next chunk to allocate
                    130:        Chunk::Row *link_row;
                    131: 
1.5       paf       132: private:
1.25      paf       133:        // last chunk
                    134:        Chunk *last_chunk;
1.5       paf       135: 
                    136:        // string size
                    137:        size_t fsize;
                    138: 
                    139:        // used rows in all chunks
                    140:        int fused_rows;
                    141: 
                    142: private:
1.1       paf       143: 
                    144:        bool chunk_is_full() {
                    145:                return append_here == link_row;
                    146:        }
                    147:        void expand();
1.27      paf       148:        void set_lang(Chunk::Row *row, Untaint_lang lang, size_t size);
1.7       paf       149: 
                    150: private: //disabled
                    151: 
1.12      paf       152:        String& operator = (const String&) { return *this; }
1.7       paf       153: 
1.1       paf       154: };
                    155: 
                    156: #endif

E-mail: