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

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

E-mail: