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

1.1       paf         1: /*
1.26    ! paf         2:   $Id: pa_string.h,v 1.25 2001/02/22 08:16:09 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.22      paf        31: #      define STRING_APPEND_PARAMS const char *src, size_t size, char *file, uint line
                     32: #      define APPEND(src, size, file, line) real_append(src, size, file, line)
1.9       paf        33: #else
1.22      paf        34: #      define STRING_APPEND_PARAMS const char *src, size_t size
                     35: #      define APPEND(src, size, file, line) real_append(src, size)
1.9       paf        36: #endif
1.23      paf        37: #define        APPEND_CONST(src) APPEND(src, 0, 0, 0)
1.9       paf        38: 
1.15      paf        39: class String : public Pooled {
1.1       paf        40: public:
                     41:        enum {
                     42:                CR_PREALLOCATED_COUNT=5,
                     43:                CR_GROW_PERCENT=60
                     44:        };
                     45: 
1.8       paf        46: public:
                     47: 
1.11      paf        48:        String(Pool& apool);
1.14      paf        49:        String(const String& src);
                     50:        size_t size() const { return fsize; }
                     51:        int used_rows() const { return fused_rows; }
                     52:        char *cstr() const;
1.9       paf        53:        String& real_append(STRING_APPEND_PARAMS);
1.26    ! paf        54:        int cmp (const String& src) const;
        !            55:        bool operator < (const String& src) const {     return cmp(src)<0; }
        !            56:        bool operator > (const String& src) const {     return cmp(src)>0; }
        !            57:        bool operator <= (const String& src) const { return cmp(src)<=0; }
        !            58:        bool operator >= (const String& src) const { return cmp(src)>=0; }
        !            59:        bool operator == (const String& src) const {
        !            60:                if(size()!=src.size()) // can speed up in trivial case
        !            61:                        return false;
        !            62:                return cmp(src)==0;
        !            63:        }
        !            64:        bool operator != (const String& src) const { return cmp(src)!=0; }
        !            65: 
1.24      paf        66:        bool operator == (char* src) const;
1.25      paf        67:        String& operator += (const String& src);
1.8       paf        68: 
1.14      paf        69:        uint hash_code() const;
1.13      paf        70: 
                     71:        const Origin& origin() const { return head.rows[0].item.origin; }
1.8       paf        72: 
1.10      paf        73: private:
                     74: 
1.1       paf        75:        struct Chunk {
1.6       paf        76:                // the number of rows in chunk
1.1       paf        77:                int count;
                     78:                union Row {
                     79:                        // chunk item
                     80:                        struct {
1.12      paf        81:                                const char *ptr;  // pointer to the start of string fragment
1.1       paf        82:                                size_t size;  // length of the fragment
1.10      paf        83:                                Origin origin;  // origin of this fragment
1.1       paf        84:                        } item;
                     85:                        Chunk *link;  // link to the next chunk in chain
1.2       paf        86:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf        87:                // next rows are here
                     88:                Chunk *preallocated_link;
                     89:        }
                     90:                head;  // the head chunk of the chunk chain
                     91: 
                     92:        // next append would write to this record
                     93:        Chunk::Row *append_here;
                     94:        
                     95:        // the address of place where lies address 
                     96:        // of the link to the next chunk to allocate
                     97:        Chunk::Row *link_row;
                     98: 
1.5       paf        99: private:
1.25      paf       100:        // last chunk
                    101:        Chunk *last_chunk;
1.5       paf       102: 
                    103:        // string size
                    104:        size_t fsize;
                    105: 
                    106:        // used rows in all chunks
                    107:        int fused_rows;
                    108: 
                    109: private:
1.1       paf       110: 
                    111:        bool chunk_is_full() {
                    112:                return append_here == link_row;
                    113:        }
                    114:        void expand();
1.7       paf       115: 
                    116: private: //disabled
                    117: 
1.12      paf       118:        String& operator = (const String&) { return *this; }
1.7       paf       119: 
1.1       paf       120: };
                    121: 
                    122: #endif

E-mail: