PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

762 lines
16KB

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