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

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

E-mail: