@@ -35,295 +35,292 @@ class StringSumHelper;
35
35
// an abstract class used as a means to proide a unique pointer type
36
36
// but really has no body
37
37
class __FlashStringHelper ;
38
- #define F (string_literal ) (reinterpret_cast <const __FlashStringHelper *>(PSTR(string_literal)))
38
+ #define FPSTR (pstr_pointer ) (reinterpret_cast <const __FlashStringHelper *>(pstr_pointer))
39
+ #define F (string_literal ) (FPSTR(PSTR(string_literal)))
39
40
40
41
// The string class
41
- class String
42
- {
43
- // use a function pointer to allow for "if (s)" without the
44
- // complications of an operator bool(). for more information, see:
45
- // http://www.artima.com/cppsource/safebool.html
46
- typedef void (String::*StringIfHelperType)() const ;
47
- void StringIfHelper () const
48
- {
49
- }
42
+ class String {
43
+ // use a function pointer to allow for "if (s)" without the
44
+ // complications of an operator bool(). for more information, see:
45
+ // http://www.artima.com/cppsource/safebool.html
46
+ typedef void (String::*StringIfHelperType)() const ;
47
+ void StringIfHelper () const {
48
+ }
50
49
51
- public:
52
- // constructors
53
- // creates a copy of the initial value.
54
- // if the initial value is null or invalid, or if memory allocation
55
- // fails, the string will be marked as invalid (i.e. "if (s)" will
56
- // be false).
57
- String (const char *cstr = " " );
58
- String (const String &str);
59
- String ( const __FlashStringHelper *str) : String(reinterpret_cast < const char *>( str)) {} ;
50
+ public:
51
+ // constructors
52
+ // creates a copy of the initial value.
53
+ // if the initial value is null or invalid, or if memory allocation
54
+ // fails, the string will be marked as invalid (i.e. "if (s)" will
55
+ // be false).
56
+ String (const char *cstr = " " );
57
+ String (const String &str);
58
+ String (const __FlashStringHelper * str);
60
59
#ifdef __GXX_EXPERIMENTAL_CXX0X__
61
- String (String &&rval);
62
- String (StringSumHelper &&rval);
60
+ String (String &&rval);
61
+ String (StringSumHelper &&rval);
63
62
#endif
64
- explicit String (char c);
65
- explicit String (unsigned char , unsigned char base = 10 );
66
- explicit String (int , unsigned char base = 10 );
67
- explicit String (unsigned int , unsigned char base = 10 );
68
- explicit String (long , unsigned char base = 10 );
69
- explicit String (unsigned long , unsigned char base = 10 );
70
- explicit String (float , unsigned char decimalPlaces = 2 );
71
- explicit String (double , unsigned char decimalPlaces = 2 );
72
- ~String (void );
63
+ explicit String (char c);
64
+ explicit String (unsigned char , unsigned char base = 10 );
65
+ explicit String (int , unsigned char base = 10 );
66
+ explicit String (unsigned int , unsigned char base = 10 );
67
+ explicit String (long , unsigned char base = 10 );
68
+ explicit String (unsigned long , unsigned char base = 10 );
69
+ explicit String (float , unsigned char decimalPlaces = 2 );
70
+ explicit String (double , unsigned char decimalPlaces = 2 );
71
+ ~String (void );
73
72
74
- // memory management
75
- // return true on success, false on failure (in which case, the string
76
- // is left unchanged). reserve(0), if successful, will validate an
77
- // invalid string (i.e., "if (s)" will be true afterwards)
78
- unsigned char reserve (unsigned int size);
79
- inline unsigned int length (void ) const
80
- {
81
- if (buffer) {
82
- return len;
83
- } else {
84
- return 0 ;
73
+ // memory management
74
+ // return true on success, false on failure (in which case, the string
75
+ // is left unchanged). reserve(0), if successful, will validate an
76
+ // invalid string (i.e., "if (s)" will be true afterwards)
77
+ unsigned char reserve (unsigned int size);
78
+ inline unsigned int length (void ) const {
79
+ if ( buffer ()) {
80
+ return len ();
81
+ } else {
82
+ return 0 ;
83
+ }
85
84
}
86
- }
87
85
88
- // creates a copy of the assigned value. if the value is null or
89
- // invalid, or if the memory allocation fails, the string will be
90
- // marked as invalid ("if (s)" will be false).
91
- String & operator =(const String &rhs);
92
- String & operator =(const char *cstr);
93
- String & operator = (const __FlashStringHelper *str);
86
+ // creates a copy of the assigned value. if the value is null or
87
+ // invalid, or if the memory allocation fails, the string will be
88
+ // marked as invalid ("if (s)" will be false).
89
+ String & operator =(const String &rhs);
90
+ String & operator =(const char *cstr);
91
+ String & operator = (const __FlashStringHelper *str);
94
92
#ifdef __GXX_EXPERIMENTAL_CXX0X__
95
- String & operator =(String &&rval);
96
- String & operator =(StringSumHelper &&rval);
93
+ String & operator =(String &&rval);
94
+ String & operator =(StringSumHelper &&rval);
97
95
#endif
98
96
99
- // concatenate (works w/ built-in types)
97
+ // concatenate (works w/ built-in types)
98
+
99
+ // returns true on success, false on failure (in which case, the string
100
+ // is left unchanged). if the argument is null or invalid, the
101
+ // concatenation is considered unsucessful.
102
+ unsigned char concat (const String &str);
103
+ unsigned char concat (const char *cstr);
104
+ unsigned char concat (char c);
105
+ unsigned char concat (unsigned char c);
106
+ unsigned char concat (int num);
107
+ unsigned char concat (unsigned int num);
108
+ unsigned char concat (long num);
109
+ unsigned char concat (unsigned long num);
110
+ unsigned char concat (float num);
111
+ unsigned char concat (double num);
112
+ unsigned char concat (const __FlashStringHelper * str);
113
+
114
+ // if there's not enough memory for the concatenated value, the string
115
+ // will be left unchanged (but this isn't signalled in any way)
116
+ String & operator +=(const String &rhs) {
117
+ concat (rhs);
118
+ return (*this );
119
+ }
120
+ String & operator +=(const char *cstr) {
121
+ concat (cstr);
122
+ return (*this );
123
+ }
124
+ String & operator +=(char c) {
125
+ concat (c);
126
+ return (*this );
127
+ }
128
+ String & operator +=(unsigned char num) {
129
+ concat (num);
130
+ return (*this );
131
+ }
132
+ String & operator +=(int num) {
133
+ concat (num);
134
+ return (*this );
135
+ }
136
+ String & operator +=(unsigned int num) {
137
+ concat (num);
138
+ return (*this );
139
+ }
140
+ String & operator +=(long num) {
141
+ concat (num);
142
+ return (*this );
143
+ }
144
+ String & operator +=(unsigned long num) {
145
+ concat (num);
146
+ return (*this );
147
+ }
148
+ String & operator +=(float num) {
149
+ concat (num);
150
+ return (*this );
151
+ }
152
+ String & operator +=(double num) {
153
+ concat (num);
154
+ return (*this );
155
+ }
156
+ String & operator += (const __FlashStringHelper *str){
157
+ concat (str);
158
+ return (*this );
159
+ }
100
160
101
- // returns true on success, false on failure (in which case, the string
102
- // is left unchanged). if the argument is null or invalid, the
103
- // concatenation is considered unsucessful.
104
- unsigned char concat (const String &str);
105
- unsigned char concat (const char *cstr);
106
- unsigned char concat (char c);
107
- unsigned char concat (unsigned char c);
108
- unsigned char concat (int num);
109
- unsigned char concat (unsigned int num);
110
- unsigned char concat (long num);
111
- unsigned char concat (unsigned long num);
112
- unsigned char concat (float num);
113
- unsigned char concat (double num);
114
- unsigned char concat (const __FlashStringHelper * str);
161
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
162
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
163
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
164
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
165
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
166
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
167
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
168
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
169
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
170
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
171
+ friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
115
172
116
- // if there's not enough memory for the concatenated value, the string
117
- // will be left unchanged (but this isn't signalled in any way)
118
- String & operator +=(const String &rhs)
119
- {
120
- concat (rhs);
121
- return (*this );
122
- }
123
- String & operator +=(const char *cstr)
124
- {
125
- concat (cstr);
126
- return (*this );
127
- }
128
- String & operator +=(char c)
129
- {
130
- concat (c);
131
- return (*this );
132
- }
133
- String & operator +=(unsigned char num)
134
- {
135
- concat (num);
136
- return (*this );
137
- }
138
- String & operator +=(int num)
139
- {
140
- concat (num);
141
- return (*this );
142
- }
143
- String & operator +=(unsigned int num)
144
- {
145
- concat (num);
146
- return (*this );
147
- }
148
- String & operator +=(long num)
149
- {
150
- concat (num);
151
- return (*this );
152
- }
153
- String & operator +=(unsigned long num)
154
- {
155
- concat (num);
156
- return (*this );
157
- }
158
- String & operator +=(float num)
159
- {
160
- concat (num);
161
- return (*this );
162
- }
163
- String & operator +=(double num)
164
- {
165
- concat (num);
166
- return (*this );
167
- }
168
- String & operator += (const __FlashStringHelper *str)
169
- {
170
- concat (str);
171
- return (*this );
172
- }
173
+ // comparison (only works w/ Strings and "strings")
174
+ operator StringIfHelperType () const {
175
+ return buffer () ? &String::StringIfHelper : 0 ;
176
+ }
177
+ int compareTo (const String &s) const ;
178
+ unsigned char equals (const String &s) const ;
179
+ unsigned char equals (const char *cstr) const ;
180
+ unsigned char operator ==(const String &rhs) const {
181
+ return equals (rhs);
182
+ }
183
+ unsigned char operator ==(const char *cstr) const {
184
+ return equals (cstr);
185
+ }
186
+ unsigned char operator !=(const String &rhs) const {
187
+ return !equals (rhs);
188
+ }
189
+ unsigned char operator !=(const char *cstr) const {
190
+ return !equals (cstr);
191
+ }
192
+ unsigned char operator <(const String &rhs) const ;
193
+ unsigned char operator >(const String &rhs) const ;
194
+ unsigned char operator <=(const String &rhs) const ;
195
+ unsigned char operator >=(const String &rhs) const ;
196
+ unsigned char equalsIgnoreCase (const String &s) const ;
197
+ unsigned char equalsConstantTime (const String &s) const ;
198
+ unsigned char startsWith (const String &prefix) const ;
199
+ unsigned char startsWith (const String &prefix, unsigned int offset) const ;
200
+ unsigned char endsWith (const String &suffix) const ;
173
201
174
- friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
175
- friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
176
- friend StringSumHelper & operator +(const StringSumHelper &lhs, char c);
177
- friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned char num);
178
- friend StringSumHelper & operator +(const StringSumHelper &lhs, int num);
179
- friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned int num);
180
- friend StringSumHelper & operator +(const StringSumHelper &lhs, long num);
181
- friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
182
- friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
183
- friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
184
- friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
202
+ // character acccess
203
+ char charAt (unsigned int index) const ;
204
+ void setCharAt (unsigned int index, char c);
205
+ char operator [](unsigned int index) const ;
206
+ char & operator [](unsigned int index);
207
+ void getBytes (unsigned char *buf, unsigned int bufsize, unsigned int index = 0 ) const ;
208
+ void toCharArray (char *buf, unsigned int bufsize, unsigned int index = 0 ) const {
209
+ getBytes ((unsigned char *) buf, bufsize, index);
210
+ }
211
+ const char * c_str () const { return buffer (); }
212
+ char * begin () { return wbuffer (); }
213
+ char * end () { return wbuffer () + length (); }
214
+ const char * begin () const { return c_str (); }
215
+ const char * end () const { return c_str () + length (); }
185
216
186
- // comparison (only works w/ Strings and "strings")
187
- operator StringIfHelperType () const
188
- {
189
- return buffer ? &String::StringIfHelper : 0 ;
190
- }
191
- int compareTo (const String &s) const ;
192
- unsigned char equals (const String &s) const ;
193
- unsigned char equals (const char *cstr) const ;
194
- unsigned char operator ==(const String &rhs) const
195
- {
196
- return equals (rhs);
197
- }
198
- unsigned char operator ==(const char *cstr) const
199
- {
200
- return equals (cstr);
201
- }
202
- unsigned char operator !=(const String &rhs) const
203
- {
204
- return !equals (rhs);
205
- }
206
- unsigned char operator !=(const char *cstr) const
207
- {
208
- return !equals (cstr);
209
- }
210
- unsigned char operator <(const String &rhs) const ;
211
- unsigned char operator >(const String &rhs) const ;
212
- unsigned char operator <=(const String &rhs) const ;
213
- unsigned char operator >=(const String &rhs) const ;
214
- unsigned char equalsIgnoreCase (const String &s) const ;
215
- unsigned char equalsConstantTime (const String &s) const ;
216
- unsigned char startsWith (const String &prefix) const ;
217
- unsigned char startsWith (const String &prefix, unsigned int offset) const ;
218
- unsigned char endsWith (const String &suffix) const ;
217
+ // search
218
+ int indexOf (char ch) const ;
219
+ int indexOf (char ch, unsigned int fromIndex) const ;
220
+ int indexOf (const String &str) const ;
221
+ int indexOf (const String &str, unsigned int fromIndex) const ;
222
+ int lastIndexOf (char ch) const ;
223
+ int lastIndexOf (char ch, unsigned int fromIndex) const ;
224
+ int lastIndexOf (const String &str) const ;
225
+ int lastIndexOf (const String &str, unsigned int fromIndex) const ;
226
+ String substring (unsigned int beginIndex) const {
227
+ return substring (beginIndex, len ());
228
+ }
229
+ ;
230
+ String substring (unsigned int beginIndex, unsigned int endIndex) const ;
219
231
220
- // character acccess
221
- char charAt (unsigned int index) const ;
222
- void setCharAt (unsigned int index, char c);
223
- char operator [](unsigned int index) const ;
224
- char & operator [](unsigned int index);
225
- void getBytes (unsigned char *buf, unsigned int bufsize, unsigned int index = 0 ) const ;
226
- void toCharArray (char *buf, unsigned int bufsize, unsigned int index = 0 ) const
227
- {
228
- getBytes ((unsigned char *) buf, bufsize, index);
229
- }
230
- const char * c_str () const
231
- {
232
- return buffer;
233
- }
232
+ // modification
233
+ void replace (char find, char replace);
234
+ void replace (const String& find, const String& replace);
235
+ void remove (unsigned int index);
236
+ void remove (unsigned int index, unsigned int count);
237
+ void toLowerCase (void );
238
+ void toUpperCase (void );
239
+ void trim (void );
234
240
235
- // search
236
- int indexOf (char ch) const ;
237
- int indexOf (char ch, unsigned int fromIndex) const ;
238
- int indexOf (const String &str) const ;
239
- int indexOf (const String &str, unsigned int fromIndex) const ;
240
- int lastIndexOf (char ch) const ;
241
- int lastIndexOf (char ch, unsigned int fromIndex) const ;
242
- int lastIndexOf (const String &str) const ;
243
- int lastIndexOf (const String &str, unsigned int fromIndex) const ;
244
- String substring (unsigned int beginIndex) const
245
- {
246
- return substring (beginIndex, len);
247
- }
248
- ;
249
- String substring (unsigned int beginIndex, unsigned int endIndex) const ;
241
+ // parsing/conversion
242
+ long toInt (void ) const ;
243
+ float toFloat (void ) const ;
244
+ double toDouble (void ) const ;
250
245
251
- // modification
252
- void replace (char find, char replace);
253
- void replace (const String& find, const String& replace);
254
- void remove (unsigned int index);
255
- void remove (unsigned int index, unsigned int count);
256
- void toLowerCase (void );
257
- void toUpperCase (void );
258
- void trim (void );
246
+ protected:
247
+ // Contains the string info when we're not in SSO mode
248
+ struct _ptr {
249
+ char * buff;
250
+ uint16_t cap;
251
+ uint16_t len;
252
+ };
259
253
260
- // parsing/conversion
261
- long toInt (void ) const ;
262
- float toFloat (void ) const ;
263
- double toDouble (void ) const ;
254
+ // SSO is handled by checking the last byte of sso_buff.
255
+ // When not in SSO mode, that byte is set to 0xff, while when in SSO mode it is always 0x00 (so it can serve as the string terminator as well as a flag)
256
+ // This allows strings up up to 12 (11 + \0 termination) without any extra space.
257
+ enum { SSOSIZE = sizeof (struct _ptr ) + 4 }; // Characters to allocate space for SSO, must be 12 or more
258
+ enum { CAPACITY_MAX = 65535 }; // If size of capacity changed, be sure to update this enum
259
+ union {
260
+ struct _ptr ptr;
261
+ char sso_buf[SSOSIZE];
262
+ };
263
+ // Accessor functions
264
+ inline bool sso () const { return sso_buf[SSOSIZE - 1 ] == 0 ; }
265
+ inline unsigned int len () const { return sso () ? strlen (sso_buf) : ptr.len ; }
266
+ inline unsigned int capacity () const { return sso () ? SSOSIZE - 1 : ptr.cap ; }
267
+ inline void setSSO (bool sso) { sso_buf[SSOSIZE - 1 ] = sso ? 0x00 : 0xff ; }
268
+ inline void setLen (int len) { if (!sso ()) ptr.len = len; }
269
+ inline void setCapacity (int cap) { if (!sso ()) ptr.cap = cap; }
270
+ inline void setBuffer (char *buff) { if (!sso ()) ptr.buff = buff; }
271
+ // Buffer accessor functions
272
+ inline const char *buffer () const { return (const char *)(sso () ? sso_buf : ptr.buff ); }
273
+ inline char *wbuffer () const { return sso () ? const_cast <char *>(sso_buf) : ptr.buff ; } // Writable version of buffer
264
274
265
- protected:
266
- char *buffer; // the actual char array
267
- unsigned int capacity; // the array length minus one (for the '\0')
268
- unsigned int len; // the String length (not counting the '\0')
269
- protected:
270
- void init (void );
271
- void invalidate (void );
272
- unsigned char changeBuffer (unsigned int maxStrLen);
273
- unsigned char concat (const char *cstr, unsigned int length);
275
+ protected:
276
+ void init (void );
277
+ void invalidate (void );
278
+ unsigned char changeBuffer (unsigned int maxStrLen);
279
+ unsigned char concat (const char *cstr, unsigned int length);
274
280
275
- // copy and move
276
- String & copy (const char *cstr, unsigned int length);
277
- String & copy (const __FlashStringHelper *pstr, unsigned int length);
281
+ // copy and move
282
+ String & copy (const char *cstr, unsigned int length);
283
+ String & copy (const __FlashStringHelper *pstr, unsigned int length);
278
284
#ifdef __GXX_EXPERIMENTAL_CXX0X__
279
- void move (String &rhs);
285
+ void move (String &rhs);
280
286
#endif
281
287
};
282
288
283
- class StringSumHelper : public String
284
- {
285
- public:
286
- StringSumHelper (const String &s) :
287
- String (s)
288
- {
289
- }
290
- StringSumHelper (const char *p) :
291
- String (p)
292
- {
293
- }
294
- StringSumHelper (char c) :
295
- String (c)
296
- {
297
- }
298
- StringSumHelper (unsigned char num) :
299
- String (num)
300
- {
301
- }
302
- StringSumHelper (int num) :
303
- String (num)
304
- {
305
- }
306
- StringSumHelper (unsigned int num) :
307
- String (num)
308
- {
309
- }
310
- StringSumHelper (long num) :
311
- String (num)
312
- {
313
- }
314
- StringSumHelper (unsigned long num) :
315
- String (num)
316
- {
317
- }
318
- StringSumHelper (float num) :
319
- String (num)
320
- {
321
- }
322
- StringSumHelper (double num) :
323
- String (num)
324
- {
325
- }
289
+ class StringSumHelper : public String {
290
+ public:
291
+ StringSumHelper (const String &s) :
292
+ String (s) {
293
+ }
294
+ StringSumHelper (const char *p) :
295
+ String (p) {
296
+ }
297
+ StringSumHelper (char c) :
298
+ String (c) {
299
+ }
300
+ StringSumHelper (unsigned char num) :
301
+ String (num) {
302
+ }
303
+ StringSumHelper (int num) :
304
+ String (num) {
305
+ }
306
+ StringSumHelper (unsigned int num) :
307
+ String (num) {
308
+ }
309
+ StringSumHelper (long num) :
310
+ String (num) {
311
+ }
312
+ StringSumHelper (unsigned long num) :
313
+ String (num) {
314
+ }
315
+ StringSumHelper (float num) :
316
+ String (num) {
317
+ }
318
+ StringSumHelper (double num) :
319
+ String (num) {
320
+ }
326
321
};
327
322
323
+ extern const String emptyString;
324
+
328
325
#endif // __cplusplus
329
326
#endif // String_class_h
0 commit comments