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

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

E-mail: