Debian Codegraph @

cores/arduino/WString.cpp

raw
1 /*
2 WString.cpp - String library for Wiring & Arduino
3 ...mostly rewritten by Paul Stoffregen...
4 Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
5 Copyright 2011, Paul Stoffregen, [email protected]
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "WString.h"
23 #include <float.h>
24
25 /*********************************************/
26 /* Static Member Initialisation */
27 /*********************************************/
28
29 size_t const String::FLT_MAX_DECIMAL_PLACES = DECIMAL_DIG;
30 size_t const String::DBL_MAX_DECIMAL_PLACES = DECIMAL_DIG;
31
32 /*********************************************/
33 /* Constructors */
34 /*********************************************/
35
36 String::String(const char *cstr)
37 {
38 init();
39 if (cstr) copy(cstr, strlen(cstr));
40 }
41
42 String::String(const String &value)
43 {
44 init();
45 *this = value;
46 }
47
48 String::String(const __FlashStringHelper *pstr)
49 {
50 init();
51 *this = pstr;
52 }
53
54 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
55 String::String(String &&rval)
56 {
57 init();
58 move(rval);
59 }
60 String::String(StringSumHelper &&rval)
61 {
62 init();
63 move(rval);
64 }
65 #endif
66
67 String::String(char c)
68 {
69 init();
70 char buf[2];
71 buf[0] = c;
72 buf[1] = 0;
73 *this = buf;
74 }
75
76 String::String(unsigned char value, unsigned char base)
77 {
78 init();
79 char buf[1 + 8 * sizeof(unsigned char)];
80 utoa(value, buf, base);
81 *this = buf;
82 }
83
84 String::String(int value, unsigned char base)
85 {
86 init();
87 char buf[2 + 8 * sizeof(int)];
88 itoa(value, buf, base);
89 *this = buf;
90 }
91
92 String::String(unsigned int value, unsigned char base)
93 {
94 init();
95 char buf[1 + 8 * sizeof(unsigned int)];
96 utoa(value, buf, base);
97 *this = buf;
98 }
99
100 String::String(long value, unsigned char base)
101 {
102 init();
103 char buf[2 + 8 * sizeof(long)];
104 ltoa(value, buf, base);
105 *this = buf;
106 }
107
108 String::String(unsigned long value, unsigned char base)
109 {
110 init();
111 char buf[1 + 8 * sizeof(unsigned long)];
112 ultoa(value, buf, base);
113 *this = buf;
114 }
115
116 String::String(float value, unsigned char decimalPlaces)
117 {
118 static size_t const FLOAT_BUF_SIZE = (FLT_MAX_10_EXP + 1) + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
119 init();
120 char buf[FLOAT_BUF_SIZE];
121 decimalPlaces = decimalPlaces < FLT_MAX_DECIMAL_PLACES ? decimalPlaces : FLT_MAX_DECIMAL_PLACES;
122 *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
123 }
124
125 String::String(double value, unsigned char decimalPlaces)
126 {
127 static size_t const DOUBLE_BUF_SIZE = (DBL_MAX_10_EXP + 1) + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
128 init();
129 char buf[DOUBLE_BUF_SIZE];
130 decimalPlaces = decimalPlaces < DBL_MAX_DECIMAL_PLACES ? decimalPlaces : DBL_MAX_DECIMAL_PLACES;
131 *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
132 }
133
134 String::~String()
135 {
136 if (buffer) free(buffer);
137 }
138
139 /*********************************************/
140 /* Memory Management */
141 /*********************************************/
142
143 inline void String::init(void)
144 {
145 buffer = NULL;
146 capacity = 0;
147 len = 0;
148 }
149
150 void String::invalidate(void)
151 {
152 if (buffer) free(buffer);
153 buffer = NULL;
154 capacity = len = 0;
155 }
156
157 unsigned char String::reserve(unsigned int size)
158 {
159 if (buffer && capacity >= size) return 1;
160 if (changeBuffer(size)) {
161 if (len == 0) buffer[0] = 0;
162 return 1;
163 }
164 return 0;
165 }
166
167 unsigned char String::changeBuffer(unsigned int maxStrLen)
168 {
169 char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
170 if (newbuffer) {
171 buffer = newbuffer;
172 capacity = maxStrLen;
173 return 1;
174 }
175 return 0;
176 }
177
178 /*********************************************/
179 /* Copy and Move */
180 /*********************************************/
181
182 String & String::copy(const char *cstr, unsigned int length)
183 {
184 if (!reserve(length)) {
185 invalidate();
186 return *this;
187 }
188 len = length;
189 strcpy(buffer, cstr);
190 return *this;
191 }
192
193 String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
194 {
195 if (!reserve(length)) {
196 invalidate();
197 return *this;
198 }
199 len = length;
200 strcpy_P(buffer, (PGM_P)pstr);
201 return *this;
202 }
203
204 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
205 void String::move(String &rhs)
206 {
207 if (buffer) {
208 if (rhs && capacity >= rhs.len) {
209 strcpy(buffer, rhs.buffer);
210 len = rhs.len;
211 rhs.len = 0;
212 return;
213 } else {
214 free(buffer);
215 }
216 }
217 buffer = rhs.buffer;
218 capacity = rhs.capacity;
219 len = rhs.len;
220 rhs.buffer = NULL;
221 rhs.capacity = 0;
222 rhs.len = 0;
223 }
224 #endif
225
226 String & String::operator = (const String &rhs)
227 {
228 if (this == &rhs) return *this;
229
230 if (rhs.buffer) copy(rhs.buffer, rhs.len);
231 else invalidate();
232
233 return *this;
234 }
235
236 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
237 String & String::operator = (String &&rval)
238 {
239 if (this != &rval) move(rval);
240 return *this;
241 }
242
243 String & String::operator = (StringSumHelper &&rval)
244 {
245 if (this != &rval) move(rval);
246 return *this;
247 }
248 #endif
249
250 String & String::operator = (const char *cstr)
251 {
252 if (cstr) copy(cstr, strlen(cstr));
253 else invalidate();
254
255 return *this;
256 }
257
258 String & String::operator = (const __FlashStringHelper *pstr)
259 {
260 if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
261 else invalidate();
262
263 return *this;
264 }
265
266 /*********************************************/
267 /* concat */
268 /*********************************************/
269
270 unsigned char String::concat(const String &s)
271 {
272 return concat(s.buffer, s.len);
273 }
274
275 unsigned char String::concat(const char *cstr, unsigned int length)
276 {
277 unsigned int newlen = len + length;
278 if (!cstr) return 0;
279 if (length == 0) return 1;
280 if (!reserve(newlen)) return 0;
281 strcpy(buffer + len, cstr);
282 len = newlen;
283 return 1;
284 }
285
286 unsigned char String::concat(const char *cstr)
287 {
288 if (!cstr) return 0;
289 return concat(cstr, strlen(cstr));
290 }
291
292 unsigned char String::concat(char c)
293 {
294 char buf[2];
295 buf[0] = c;
296 buf[1] = 0;
297 return concat(buf, 1);
298 }
299
300 unsigned char String::concat(unsigned char num)
301 {
302 char buf[1 + 3 * sizeof(unsigned char)];
303 itoa(num, buf, 10);
304 return concat(buf, strlen(buf));
305 }
306
307 unsigned char String::concat(int num)
308 {
309 char buf[2 + 3 * sizeof(int)];
310 itoa(num, buf, 10);
311 return concat(buf, strlen(buf));
312 }
313
314 unsigned char String::concat(unsigned int num)
315 {
316 char buf[1 + 3 * sizeof(unsigned int)];
317 utoa(num, buf, 10);
318 return concat(buf, strlen(buf));
319 }
320
321 unsigned char String::concat(long num)
322 {
323 char buf[2 + 3 * sizeof(long)];
324 ltoa(num, buf, 10);
325 return concat(buf, strlen(buf));
326 }
327
328 unsigned char String::concat(unsigned long num)
329 {
330 char buf[1 + 3 * sizeof(unsigned long)];
331 ultoa(num, buf, 10);
332 return concat(buf, strlen(buf));
333 }
334
335 unsigned char String::concat(float num)
336 {
337 char buf[20];
338 char* string = dtostrf(num, 4, 2, buf);
339 return concat(string, strlen(string));
340 }
341
342 unsigned char String::concat(double num)
343 {
344 char buf[20];
345 char* string = dtostrf(num, 4, 2, buf);
346 return concat(string, strlen(string));
347 }
348
349 unsigned char String::concat(const __FlashStringHelper * str)
350 {
351 if (!str) return 0;
352 int length = strlen_P((const char *) str);
353 if (length == 0) return 1;
354 unsigned int newlen = len + length;
355 if (!reserve(newlen)) return 0;
356 strcpy_P(buffer + len, (const char *) str);
357 len = newlen;
358 return 1;
359 }
360
361 /*********************************************/
362 /* Concatenate */
363 /*********************************************/
364
365 StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
366 {
367 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
368 if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
369 return a;
370 }
371
372 StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
373 {
374 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
375 if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
376 return a;
377 }
378
379 StringSumHelper & operator + (const StringSumHelper &lhs, char c)
380 {
381 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
382 if (!a.concat(c)) a.invalidate();
383 return a;
384 }
385
386 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
387 {
388 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
389 if (!a.concat(num)) a.invalidate();
390 return a;
391 }
392
393 StringSumHelper & operator + (const StringSumHelper &lhs, int num)
394 {
395 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
396 if (!a.concat(num)) a.invalidate();
397 return a;
398 }
399
400 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
401 {
402 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
403 if (!a.concat(num)) a.invalidate();
404 return a;
405 }
406
407 StringSumHelper & operator + (const StringSumHelper &lhs, long num)
408 {
409 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
410 if (!a.concat(num)) a.invalidate();
411 return a;
412 }
413
414 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
415 {
416 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
417 if (!a.concat(num)) a.invalidate();
418 return a;
419 }
420
421 StringSumHelper & operator + (const StringSumHelper &lhs, float num)
422 {
423 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
424 if (!a.concat(num)) a.invalidate();
425 return a;
426 }
427
428 StringSumHelper & operator + (const StringSumHelper &lhs, double num)
429 {
430 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
431 if (!a.concat(num)) a.invalidate();
432 return a;
433 }
434
435 StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
436 {
437 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
438 if (!a.concat(rhs)) a.invalidate();
439 return a;
440 }
441
442 /*********************************************/
443 /* Comparison */
444 /*********************************************/
445
446 int String::compareTo(const String &s) const
447 {
448 if (!buffer || !s.buffer) {
449 if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
450 if (buffer && len > 0) return *(unsigned char *)buffer;
451 return 0;
452 }
453 return strcmp(buffer, s.buffer);
454 }
455
456 unsigned char String::equals(const String &s2) const
457 {
458 return (len == s2.len && compareTo(s2) == 0);
459 }
460
461 unsigned char String::equals(const char *cstr) const
462 {
463 if (len == 0) return (cstr == NULL || *cstr == 0);
464 if (cstr == NULL) return buffer[0] == 0;
465 return strcmp(buffer, cstr) == 0;
466 }
467
468 unsigned char String::operator<(const String &rhs) const
469 {
470 return compareTo(rhs) < 0;
471 }
472
473 unsigned char String::operator>(const String &rhs) const
474 {
475 return compareTo(rhs) > 0;
476 }
477
478 unsigned char String::operator<=(const String &rhs) const
479 {
480 return compareTo(rhs) <= 0;
481 }
482
483 unsigned char String::operator>=(const String &rhs) const
484 {
485 return compareTo(rhs) >= 0;
486 }
487
488 unsigned char String::equalsIgnoreCase( const String &s2 ) const
489 {
490 if (this == &s2) return 1;
491 if (len != s2.len) return 0;
492 if (len == 0) return 1;
493 const char *p1 = buffer;
494 const char *p2 = s2.buffer;
495 while (*p1) {
496 if (tolower(*p1++) != tolower(*p2++)) return 0;
497 }
498 return 1;
499 }
500
501 unsigned char String::startsWith( const String &s2 ) const
502 {
503 if (len < s2.len) return 0;
504 return startsWith(s2, 0);
505 }
506
507 unsigned char String::startsWith( const String &s2, unsigned int offset ) const
508 {
509 if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
510 return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
511 }
512
513 unsigned char String::endsWith( const String &s2 ) const
514 {
515 if ( len < s2.len || !buffer || !s2.buffer) return 0;
516 return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
517 }
518
519 /*********************************************/
520 /* Character Access */
521 /*********************************************/
522
523 char String::charAt(unsigned int loc) const
524 {
525 return operator[](loc);
526 }
527
528 void String::setCharAt(unsigned int loc, char c)
529 {
530 if (loc < len) buffer[loc] = c;
531 }
532
533 char & String::operator[](unsigned int index)
534 {
535 static char dummy_writable_char;
536 if (index >= len || !buffer) {
537 dummy_writable_char = 0;
538 return dummy_writable_char;
539 }
540 return buffer[index];
541 }
542
543 char String::operator[]( unsigned int index ) const
544 {
545 if (index >= len || !buffer) return 0;
546 return buffer[index];
547 }
548
549 void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
550 {
551 if (!bufsize || !buf) return;
552 if (index >= len) {
553 buf[0] = 0;
554 return;
555 }
556 unsigned int n = bufsize - 1;
557 if (n > len - index) n = len - index;
558 strncpy((char *)buf, buffer + index, n);
559 buf[n] = 0;
560 }
561
562 /*********************************************/
563 /* Search */
564 /*********************************************/
565
566 int String::indexOf(char c) const
567 {
568 return indexOf(c, 0);
569 }
570
571 int String::indexOf( char ch, unsigned int fromIndex ) const
572 {
573 if (fromIndex >= len) return -1;
574 const char* temp = strchr(buffer + fromIndex, ch);
575 if (temp == NULL) return -1;
576 return temp - buffer;
577 }
578
579 int String::indexOf(const String &s2) const
580 {
581 return indexOf(s2, 0);
582 }
583
584 int String::indexOf(const String &s2, unsigned int fromIndex) const
585 {
586 if (fromIndex >= len) return -1;
587 const char *found = strstr(buffer + fromIndex, s2.buffer);
588 if (found == NULL) return -1;
589 return found - buffer;
590 }
591
592 int String::lastIndexOf( char theChar ) const
593 {
594 return lastIndexOf(theChar, len - 1);
595 }
596
597 int String::lastIndexOf(char ch, unsigned int fromIndex) const
598 {
599 if (fromIndex >= len) return -1;
600 char tempchar = buffer[fromIndex + 1];
601 buffer[fromIndex + 1] = '\0';
602 char* temp = strrchr( buffer, ch );
603 buffer[fromIndex + 1] = tempchar;
604 if (temp == NULL) return -1;
605 return temp - buffer;
606 }
607
608 int String::lastIndexOf(const String &s2) const
609 {
610 return lastIndexOf(s2, len - s2.len);
611 }
612
613 int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
614 {
615 if (s2.len == 0 || len == 0 || s2.len > len) return -1;
616 if (fromIndex >= len) fromIndex = len - 1;
617 int found = -1;
618 for (char *p = buffer; p <= buffer + fromIndex; p++) {
619 p = strstr(p, s2.buffer);
620 if (!p) break;
621 if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
622 }
623 return found;
624 }
625
626 String String::substring(unsigned int left, unsigned int right) const
627 {
628 if (left > right) {
629 unsigned int temp = right;
630 right = left;
631 left = temp;
632 }
633 String out;
634 if (left >= len) return out;
635 if (right > len) right = len;
636 char temp = buffer[right]; // save the replaced character
637 buffer[right] = '\0';
638 out = buffer + left; // pointer arithmetic
639 buffer[right] = temp; //restore character
640 return out;
641 }
642
643 /*********************************************/
644 /* Modification */
645 /*********************************************/
646
647 void String::replace(char find, char replace)
648 {
649 if (!buffer) return;
650 for (char *p = buffer; *p; p++) {
651 if (*p == find) *p = replace;
652 }
653 }
654
655 void String::replace(const String& find, const String& replace)
656 {
657 if (len == 0 || find.len == 0) return;
658 int diff = replace.len - find.len;
659 char *readFrom = buffer;
660 char *foundAt;
661 if (diff == 0) {
662 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
663 memcpy(foundAt, replace.buffer, replace.len);
664 readFrom = foundAt + replace.len;
665 }
666 } else if (diff < 0) {
667 char *writeTo = buffer;
668 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
669 unsigned int n = foundAt - readFrom;
670 memcpy(writeTo, readFrom, n);
671 writeTo += n;
672 memcpy(writeTo, replace.buffer, replace.len);
673 writeTo += replace.len;
674 readFrom = foundAt + find.len;
675 len += diff;
676 }
677 strcpy(writeTo, readFrom);
678 } else {
679 unsigned int size = len; // compute size needed for result
680 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
681 readFrom = foundAt + find.len;
682 size += diff;
683 }
684 if (size == len) return;
685 if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
686 int index = len - 1;
687 while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
688 readFrom = buffer + index + find.len;
689 memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
690 len += diff;
691 buffer[len] = 0;
692 memcpy(buffer + index, replace.buffer, replace.len);
693 index--;
694 }
695 }
696 }
697
698 void String::remove(unsigned int index){
699 // Pass the biggest integer as the count. The remove method
700 // below will take care of truncating it at the end of the
701 // string.
702 remove(index, (unsigned int)-1);
703 }
704
705 void String::remove(unsigned int index, unsigned int count){
706 if (index >= len) { return; }
707 if (count <= 0) { return; }
708 if (count > len - index) { count = len - index; }
709 char *writeTo = buffer + index;
710 len = len - count;
711 strncpy(writeTo, buffer + index + count,len - index);
712 buffer[len] = 0;
713 }
714
715 void String::toLowerCase(void)
716 {
717 if (!buffer) return;
718 for (char *p = buffer; *p; p++) {
719 *p = tolower(*p);
720 }
721 }
722
723 void String::toUpperCase(void)
724 {
725 if (!buffer) return;
726 for (char *p = buffer; *p; p++) {
727 *p = toupper(*p);
728 }
729 }
730
731 void String::trim(void)
732 {
733 if (!buffer || len == 0) return;
734 char *begin = buffer;
735 while (isspace(*begin)) begin++;
736 char *end = buffer + len - 1;
737 while (isspace(*end) && end >= begin) end--;
738 len = end + 1 - begin;
739 if (begin > buffer) memcpy(buffer, begin, len);
740 buffer[len] = 0;
741 }
742
743 /*********************************************/
744 /* Parsing / Conversion */
745 /*********************************************/
746
747 long String::toInt(void) const
748 {
749 if (buffer) return atol(buffer);
750 return 0;
751 }
752
753 float String::toFloat(void) const
754 {
755 return float(toDouble());
756 }
757
758 double String::toDouble(void) const
759 {
760 if (buffer) return atof(buffer);
761 return 0;
762 }