@@ -69,7 +69,9 @@ class String {
69
69
explicit String (unsigned long , unsigned char base = 10 );
70
70
explicit String (float , unsigned char decimalPlaces = 2 );
71
71
explicit String (double , unsigned char decimalPlaces = 2 );
72
- ~String (void );
72
+ ~String () {
73
+ invalidate ();
74
+ }
73
75
74
76
// memory management
75
77
// return true on success, false on failure (in which case, the string
@@ -218,7 +220,9 @@ class String {
218
220
}
219
221
220
222
// character access
221
- char charAt (unsigned int index) const ;
223
+ char charAt (unsigned int index) const {
224
+ return operator [](index );
225
+ }
222
226
void setCharAt (unsigned int index, char c);
223
227
char operator [](unsigned int index) const ;
224
228
char & operator [](unsigned int index);
@@ -287,19 +291,19 @@ class String {
287
291
enum { SSOSIZE = sizeof (struct _ptr ) + 4 - 1 }; // Characters to allocate space for SSO, must be 12 or more
288
292
struct _sso {
289
293
char buff[SSOSIZE];
290
- unsigned char len : 7 ; // Ensure only one byte is allocated by GCC for the bitfields
291
- unsigned char isSSO : 1 ;
294
+ unsigned char len : 7 ; // Ensure only one byte is allocated by GCC for the bitfields
295
+ unsigned char isHeap : 1 ;
292
296
} __attribute__((packed)); // Ensure that GCC doesn't expand the flag byte to a 32-bit word for alignment issues
293
297
enum { CAPACITY_MAX = 65535 }; // If typeof(cap) changed from uint16_t, be sure to update this enum to the max value storable in the type
294
298
union {
295
299
struct _ptr ptr;
296
300
struct _sso sso;
297
301
};
298
302
// Accessor functions
299
- inline bool isSSO () const { return sso.isSSO ; }
303
+ inline bool isSSO () const { return ! sso.isHeap ; }
300
304
inline unsigned int len () const { return isSSO () ? sso.len : ptr.len ; }
301
305
inline unsigned int capacity () const { return isSSO () ? (unsigned int )SSOSIZE - 1 : ptr.cap ; } // Size of max string not including terminal NUL
302
- inline void setSSO (bool set) { sso.isSSO = set; }
306
+ inline void setSSO (bool set) { sso.isHeap = ! set; }
303
307
inline void setLen (int len) { if (isSSO ()) sso.len = len; else ptr.len = len; }
304
308
inline void setCapacity (int cap) { if (!isSSO ()) ptr.cap = cap; }
305
309
inline void setBuffer (char *buff) { if (!isSSO ()) ptr.buff = buff; }
@@ -308,7 +312,11 @@ class String {
308
312
inline char *wbuffer () const { return isSSO () ? const_cast <char *>(sso.buff ) : ptr.buff ; } // Writable version of buffer
309
313
310
314
protected:
311
- void init (void );
315
+ void init (void ) {
316
+ sso.isHeap = 0 ;
317
+ sso.len = 0 ;
318
+ sso.buff [0 ] = 0 ;
319
+ }
312
320
void invalidate (void );
313
321
unsigned char changeBuffer (unsigned int maxStrLen);
314
322
0 commit comments