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

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

E-mail: