@@ -112,6 +112,13 @@ inline void String::init(void)
112
112
flags = 0 ;
113
113
}
114
114
115
+ void String::invalidate (void )
116
+ {
117
+ if (buffer) free (buffer);
118
+ buffer = NULL ;
119
+ capacity = len = 0 ;
120
+ }
121
+
115
122
unsigned char String::reserve (unsigned int size)
116
123
{
117
124
if (buffer && capacity >= size) return 1 ;
@@ -140,11 +147,7 @@ unsigned char String::changeBuffer(unsigned int maxStrLen)
140
147
String & String::copy (const char *cstr, unsigned int length)
141
148
{
142
149
if (!reserve (length)) {
143
- if (buffer) {
144
- free (buffer);
145
- buffer = NULL ;
146
- }
147
- len = capacity = 0 ;
150
+ invalidate ();
148
151
return *this ;
149
152
}
150
153
len = length;
@@ -177,7 +180,11 @@ void String::move(String &rhs)
177
180
String & String::operator = (const String &rhs)
178
181
{
179
182
if (this == &rhs) return *this ;
180
- return copy (rhs.buffer , rhs.len );
183
+
184
+ if (rhs.buffer ) copy (rhs.buffer , rhs.len );
185
+ else invalidate ();
186
+
187
+ return *this ;
181
188
}
182
189
183
190
#ifdef __GXX_EXPERIMENTAL_CXX0X__
@@ -196,16 +203,9 @@ String & String::operator = (StringSumHelper &&rval)
196
203
197
204
String & String::operator = (const char *cstr)
198
205
{
199
- if (cstr) {
200
- copy (cstr, strlen (cstr));
201
- } else {
202
- if (buffer) {
203
- free (buffer);
204
- capacity = 0 ;
205
- buffer = NULL ;
206
- }
207
- len = 0 ;
208
- }
206
+ if (cstr) copy (cstr, strlen (cstr));
207
+ else invalidate ();
208
+
209
209
return *this ;
210
210
}
211
211
@@ -229,7 +229,8 @@ unsigned char String::concat(const String &s)
229
229
unsigned char String::concat (const char *cstr, unsigned int length)
230
230
{
231
231
unsigned int newlen = len + length;
232
- if (!cstr || length == 0 ) return 1 ; // nothing to append = success
232
+ if (!cstr) return 0 ;
233
+ if (length == 0 ) return 1 ;
233
234
if (!reserve (newlen)) return 0 ;
234
235
strcpy (buffer + len, cstr);
235
236
len = newlen;
@@ -238,7 +239,7 @@ unsigned char String::concat(const char *cstr, unsigned int length)
238
239
239
240
unsigned char String::concat (const char *cstr)
240
241
{
241
- if (!cstr) return 1 ; // nothing to append = success
242
+ if (!cstr) return 0 ;
242
243
return concat (cstr, strlen (cstr));
243
244
}
244
245
@@ -285,56 +286,56 @@ unsigned char String::concat(unsigned long num)
285
286
StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
286
287
{
287
288
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
288
- a.concat (rhs.buffer , rhs.len );
289
+ if (! a.concat (rhs.buffer , rhs.len )) a. invalidate ( );
289
290
return a;
290
291
}
291
292
292
293
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
293
294
{
294
295
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
295
- if (cstr) a.concat (cstr, strlen (cstr));
296
+ if (! cstr || ! a.concat (cstr, strlen (cstr))) a. invalidate ( );
296
297
return a;
297
298
}
298
299
299
300
StringSumHelper & operator + (const StringSumHelper &lhs, char c)
300
301
{
301
302
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
302
- a.concat (c);
303
+ if (! a.concat (c)) a. invalidate ( );
303
304
return a;
304
305
}
305
306
306
307
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c)
307
308
{
308
309
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
309
- a.concat (c);
310
+ if (! a.concat (c)) a. invalidate ( );
310
311
return a;
311
312
}
312
313
313
314
StringSumHelper & operator + (const StringSumHelper &lhs, int num)
314
315
{
315
316
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
316
- a.concat (num);
317
+ if (! a.concat (num)) a. invalidate ( );
317
318
return a;
318
319
}
319
320
320
321
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
321
322
{
322
323
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
323
- a.concat (num);
324
+ if (! a.concat (num)) a. invalidate ( );
324
325
return a;
325
326
}
326
327
327
328
StringSumHelper & operator + (const StringSumHelper &lhs, long num)
328
329
{
329
330
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
330
- a.concat (num);
331
+ if (! a.concat (num)) a. invalidate ( );
331
332
return a;
332
333
}
333
334
334
335
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
335
336
{
336
337
StringSumHelper &a = const_cast <StringSumHelper&>(lhs);
337
- a.concat (num);
338
+ if (! a.concat (num)) a. invalidate ( );
338
339
return a;
339
340
}
340
341
0 commit comments