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

1.1       paf         1: /*
1.16    ! paf         2:   $Id: pa_string.h,v 1.15 2001/02/11 11:27:24 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.12      paf        31: #      define STRING_APPEND_PARAMS const char *src, char *file, uint line
1.10      paf        32: #      define APPEND(src, file, line) real_append(src, file, line)
1.9       paf        33: #else
1.12      paf        34: #      define STRING_APPEND_PARAMS const char *src
1.10      paf        35: #      define APPEND(src, file, line) real_append(src)
1.9       paf        36: #endif
                     37: 
1.16    ! paf        38: class String_iterator;
        !            39: 
1.15      paf        40: class String : public Pooled {
1.16    ! paf        41:        friend String_iterator;
1.1       paf        42: public:
                     43:        enum {
                     44:                CR_PREALLOCATED_COUNT=5,
                     45:                CR_GROW_PERCENT=60
                     46:        };
                     47: 
1.8       paf        48: public:
                     49: 
1.11      paf        50:        String(Pool& apool);
1.14      paf        51:        String(const String& src);
                     52:        size_t size() const { return fsize; }
                     53:        int used_rows() const { return fused_rows; }
                     54:        char *cstr() const;
1.9       paf        55:        String& real_append(STRING_APPEND_PARAMS);
1.14      paf        56:        bool operator == (const String& src) const;
1.16    ! paf        57:        String& append(const String_iterator& begin, const String_iterator& end);
1.8       paf        58: 
1.14      paf        59:        uint hash_code() const;
1.13      paf        60: 
                     61:        const Origin& origin() const { return head.rows[0].item.origin; }
1.8       paf        62: 
1.10      paf        63: private:
                     64: 
1.1       paf        65:        struct Chunk {
1.6       paf        66:                // the number of rows in chunk
1.1       paf        67:                int count;
                     68:                union Row {
                     69:                        // chunk item
                     70:                        struct {
1.12      paf        71:                                const char *ptr;  // pointer to the start of string fragment
1.1       paf        72:                                size_t size;  // length of the fragment
1.10      paf        73:                                Origin origin;  // origin of this fragment
1.1       paf        74:                        } item;
                     75:                        Chunk *link;  // link to the next chunk in chain
1.2       paf        76:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf        77:                // next rows are here
                     78:                Chunk *preallocated_link;
                     79:        }
                     80:                head;  // the head chunk of the chunk chain
                     81: 
                     82:        // next append would write to this record
                     83:        Chunk::Row *append_here;
                     84:        
                     85:        // the address of place where lies address 
                     86:        // of the link to the next chunk to allocate
                     87:        Chunk::Row *link_row;
                     88: 
1.5       paf        89: private:
                     90:        // last chank allocated count
                     91:        int curr_chunk_rows;
                     92: 
                     93:        // string size
                     94:        size_t fsize;
                     95: 
                     96:        // used rows in all chunks
                     97:        int fused_rows;
                     98: 
                     99: private:
1.1       paf       100: 
                    101:        bool chunk_is_full() {
                    102:                return append_here == link_row;
                    103:        }
                    104:        void expand();
1.7       paf       105: 
                    106: private: //disabled
                    107: 
1.12      paf       108:        String& operator = (const String&) { return *this; }
1.7       paf       109: 
1.1       paf       110: };
1.15      paf       111: 
1.16    ! paf       112: 
        !           113: class Char_set {
        !           114: public:
        !           115:        Char_set();
        !           116:        void include(char c) { bools[static_cast<unsigned int>(c)]=true; }
        !           117:        bool in(char c) { return bools[static_cast<unsigned int>(c)]; }
        !           118: private:       
        !           119:        bool bools[0x100];
        !           120: };
        !           121: 
        !           122: class String_iterator {
        !           123: public:
        !           124:        String_iterator(String& astring);
        !           125: 
        !           126:        void operator ++() { skip(); }
        !           127:        void operator ++(int) { skip(); }
        !           128: 
        !           129:        void skip_to(Char_set& set);
        !           130:        void skip_to(char c);
        !           131: 
        !           132:        bool eof() { return feof; }
        !           133: 
        !           134:        // current char
        !           135:        char operator() const;
        !           136: 
        !           137: protected:
        !           138:        // home string
        !           139:        String& string;
        !           140:        // the row in which we are
        !           141:        Chunk::Row *read_here;
        !           142:        // position in that row's string fragment
        !           143:        int offset;
        !           144:        // when read_here reaches this row, move to the next chunk
        !           145:        Chunk::Row *link_row;
        !           146: 
        !           147:        bool feof;
        !           148: 
        !           149: protected:
        !           150: 
        !           151:        // advances position on one char
        !           152:        void skip() {
        !           153: 
        !           154:        }
        !           155: };
1.1       paf       156: 
                    157: #endif

E-mail: