Annotation of parser3/src/include/pa_string.h, revision 1.19
1.1 paf 1: /*
1.19 ! paf 2: $Id: pa_string.h,v 1.18 2001/02/13 10:30:22 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:
1.18 paf 113: class Char_types {
1.16 paf 114: public:
1.18 paf 115: Char_types();
116: void set(char c, int type) {
117: types[static_cast<unsigned int>(c)]=static_cast<char>(type);
1.17 paf 118: }
119: int get(char c) {
1.18 paf 120: return static_cast<int>(types[static_cast<unsigned int>(c)]);
1.17 paf 121: }
1.16 paf 122: private:
1.19 ! paf 123: char types[0x100];
1.16 paf 124: };
125:
126: class String_iterator {
127: public:
128: String_iterator(String& astring);
129:
130: void operator ++() { skip(); }
131: void operator ++(int) { skip(); }
132:
1.19 ! paf 133: int skip_to(Char_types& types);
1.17 paf 134: bool skip_to(char c);
1.16 paf 135:
1.19 ! paf 136: bool eof() { return position==0; }
1.16 paf 137:
138: // current char
1.19 ! paf 139: char operator() () const;
1.16 paf 140:
141: protected:
142: // home string
143: String& string;
144: // the row in which we are
1.19 ! paf 145: String::Chunk::Row *read_here;
1.18 paf 146: // position in text, eof when 0
1.19 ! paf 147: const char *position;
1.16 paf 148: // when read_here reaches this row, move to the next chunk
1.19 ! paf 149: String::Chunk::Row *link_row;
1.16 paf 150:
151: protected:
152:
1.17 paf 153: // advances position by one char
154: void skip();
1.16 paf 155: };
1.1 paf 156:
157: #endif
E-mail: