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

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

E-mail: