blob: fe0b8e307bc3bdd0b5672b5b1c3be245d0e61b03 (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
#ifndef STRINGUTIL_HH
#define STRINGUTIL_HH
#include <assert.h>
#if !defined(NDEBUG)
#define NDEBUG BLONDE
#endif
class String_handle;
/// Internal String struct
class StringData {
// GNU malloc: storage overhead is 8 bytes anyway.
const int INITIALMAX =8; // how to do this in ANSI C++ ?
friend class String_handle;
int maxlen; // maxlen is arraysize-1
int length;
char* string;
int references;
/// init to ""
StringData() {
references=0;
maxlen = INITIALMAX;
string = new char[maxlen + 1];
string[0] = 0;
length = 0;
}
/// init from src. Conservative allocation.
StringData(StringData const &src) {
references=0;
maxlen = length = src.length;
string = new char[maxlen+1]; // should calc GNU 8byte overhead.
strcpy(string, src.string);
}
~StringData() {
assert(references == 0);
delete[] string;
}
void setmax(int j) {
OKW();
if (j > maxlen) {
delete string;
maxlen = j;
string = new char[maxlen + 1];
string[0] = 0;
length = 0;
}
}
/** POST: maxlen >= j.
IN: j, maximum stringlength.
contents thrown away.
*/
///
void remax(int j) {
OKW();
if (j > maxlen) {
maxlen = j;
char *p = new char[maxlen + 1];
strcpy(p,string);
delete[] string;
string = p;
// length = strlen(string);
}
}
/** POST: maxlen >= j.
IN: j, maximum stringlength.
contents are kept if it grows.
*/
/// check if writeable.
void OKW() {
assert (references == 1);
}
/// check state.
void OK() {
assert(strlen(string) == size_t(length));
assert(maxlen >= length);
assert(bool(string));
assert(references >= 1);
}
// needed?
void update() {
length = strlen (string);
}
/// reduce memory usage.
void tighten() { // should be dec'd const
maxlen = length;
char *p = new char[maxlen + 1];
strcpy(p,string);
delete[] string;
string = p;
}
// assignment.
void set(const char *s) {
OKW();
assert(s);
length = strlen (s);
remax(length);
strcpy(string,s);
}
/// concatenation.
void operator += (const char *s) {
OK();
OKW();
int old = length;
length += strlen(s);
remax (length);
strcpy(string + old, s);
}
/// the array itself
operator const char *() const { return string; }
// idem, non const
char *array_for_modify() {
OKW();
return string;
}
void trunc(int j) {
OKW();
assert(j >= 0 && j <= length);
string[j] = 0;
length = j;
}
/** not really safe. Can alter length without StringData knowing it.
*/
char &operator [](int j) {
assert(j >= 0 && j <= length);
return string[j] ;
}
char operator [](int j) const {
assert(j >= 0 && j <= length);
return string[j];
}
};
/**
the data itself. Handles simple tasks (resizing, resetting)
*/
/****************************************************************/
/// ref. counting for strings
class String_handle {
StringData* data;
/// decrease ref count. Named kind of like a Tanenbaum semafore
void down() { if (!(--data->references)) delete data; data = 0; }
/// increase ref count
void up(StringData *d) { data=d; data->references ++; }
/** make sure data has only one reference.
POST: data->references == 1
*/
void copy() {
if (data->references !=1){
StringData *newdata = new StringData(*data);
down();
up(newdata);
}
}
public:
String_handle() {
up(new StringData);
}
~String_handle() {
down();
}
String_handle(String_handle const & src) {
up(src.data);
}
/// retrieve the actual array.
operator const char *() const { return *data; }
char *array_for_modify() {
copy();
return data->array_for_modify();
}
void operator =(String_handle const &src) {
if (this == &src)
return;
down();
up(src.data);
}
void operator += (const char *s) {
copy();
*data += s;
}
char operator[](int j) const { return (*data)[j]; }
// !NOT SAFE!
// don't use this for loops. Use array_for_modify()
char &operator[](int j) {
copy(); // hmm. Not efficient
return data->array_for_modify()[j];
}
void operator = (char const *p) {
copy();
data->set(p);
}
void trunc(int j) { copy(); data->trunc(j); }
int len() const { return data->length; }
};
/**
handles ref. counting, and provides a very thin
interface using char *
*/
#ifdef NDEBUG
#if (NDEBUG == BLONDE)
#undef NDEBUG
#endif
#endif
#endif // STRINGUTIL_HH
|