您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. #if __cplusplus >= 201103L || defined(__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()
  96. {
  97. free(buffer);
  98. }
  99. /*********************************************/
  100. /* Memory Management */
  101. /*********************************************/
  102. inline void String::init(void)
  103. {
  104. buffer = NULL;
  105. capacity = 0;
  106. len = 0;
  107. flags = 0;
  108. }
  109. unsigned char String::reserve(unsigned int size)
  110. {
  111. if (capacity >= size) return 1;
  112. if (changeBuffer(size)) {
  113. if (len == 0) buffer[0] = 0;
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. unsigned char String::changeBuffer(unsigned int maxStrLen)
  119. {
  120. char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
  121. if (newbuffer) {
  122. buffer = newbuffer;
  123. capacity = maxStrLen;
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. /*********************************************/
  129. /* Copy and Move */
  130. /*********************************************/
  131. String & String::copy(const char *cstr, unsigned int length)
  132. {
  133. if (length == 0) {
  134. if (buffer) buffer[0] = 0;
  135. len = 0;
  136. return *this;
  137. }
  138. if (!reserve(length)) {
  139. if (buffer) {
  140. free(buffer);
  141. buffer = NULL;
  142. }
  143. len = capacity = 0;
  144. return *this;
  145. }
  146. len = length;
  147. strcpy(buffer, cstr);
  148. return *this;
  149. }
  150. void String::move(String &rhs)
  151. {
  152. if (buffer) {
  153. if (capacity >= rhs.len) {
  154. strcpy(buffer, rhs.buffer);
  155. len = rhs.len;
  156. rhs.len = 0;
  157. return;
  158. } else {
  159. free(buffer);
  160. }
  161. }
  162. buffer = rhs.buffer;
  163. capacity = rhs.capacity;
  164. len = rhs.len;
  165. rhs.buffer = NULL;
  166. rhs.capacity = 0;
  167. rhs.len = 0;
  168. }
  169. String & String::operator = (const String &rhs)
  170. {
  171. if (this == &rhs) return *this;
  172. return copy(rhs.buffer, rhs.len);
  173. }
  174. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  175. String & String::operator = (String &&rval)
  176. {
  177. if (this != &rval) move(rval);
  178. return *this;
  179. }
  180. String & String::operator = (StringSumHelper &&rval)
  181. {
  182. if (this != &rval) move(rval);
  183. return *this;
  184. }
  185. #endif
  186. String & String::operator = (const char *cstr)
  187. {
  188. if (cstr) {
  189. copy(cstr, strlen(cstr));
  190. } else {
  191. len = 0;
  192. }
  193. return *this;
  194. }
  195. String & String::operator = (const __FlashStringHelper *pgmstr)
  196. {
  197. copy(pgmstr);
  198. return *this;
  199. }
  200. String & String::operator = (char c)
  201. {
  202. char buf[2];
  203. buf[0] = c;
  204. buf[1] = 0;
  205. return copy(buf, 1);
  206. }
  207. /*********************************************/
  208. /* Append */
  209. /*********************************************/
  210. String & String::append(const String &s)
  211. {
  212. return append(s.buffer, s.len);
  213. }
  214. String & String::append(const char *cstr, unsigned int length)
  215. {
  216. unsigned int newlen = len + length;
  217. if (length == 0 || !reserve(newlen)) return *this;
  218. strcpy(buffer + len, cstr);
  219. len = newlen;
  220. return *this;
  221. }
  222. String & String::append(const char *cstr)
  223. {
  224. if (cstr) append(cstr, strlen(cstr));
  225. return *this;
  226. }
  227. String & String::append(char c)
  228. {
  229. char buf[2];
  230. buf[0] = c;
  231. buf[1] = 0;
  232. append(buf, 1);
  233. return *this;
  234. }
  235. String & String::append(int num)
  236. {
  237. char buf[12];
  238. ltoa((long)num, buf, 10);
  239. append(buf, strlen(buf));
  240. return *this;
  241. }
  242. String & String::append(unsigned int num)
  243. {
  244. char buf[11];
  245. ultoa((unsigned long)num, buf, 10);
  246. append(buf, strlen(buf));
  247. return *this;
  248. }
  249. String & String::append(long num)
  250. {
  251. char buf[12];
  252. ltoa(num, buf, 10);
  253. append(buf, strlen(buf));
  254. return *this;
  255. }
  256. String & String::append(unsigned long num)
  257. {
  258. char buf[11];
  259. ultoa(num, buf, 10);
  260. append(buf, strlen(buf));
  261. return *this;
  262. }
  263. String & String::append(float num)
  264. {
  265. char buf[30];
  266. dtostrf(num, 4, 2, buf);
  267. append(buf, strlen(buf));
  268. return *this;
  269. }
  270. /*********************************************/
  271. /* Concatenate */
  272. /*********************************************/
  273. StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
  274. {
  275. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  276. a.append(rhs.buffer, rhs.len);
  277. return a;
  278. }
  279. StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
  280. {
  281. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  282. if (cstr) a.append(cstr, strlen(cstr));
  283. return a;
  284. }
  285. StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *pgmstr)
  286. {
  287. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  288. a.append(pgmstr);
  289. return a;
  290. }
  291. StringSumHelper & operator + (const StringSumHelper &lhs, char c)
  292. {
  293. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  294. a.append(c);
  295. return a;
  296. }
  297. StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c)
  298. {
  299. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  300. a.append(c);
  301. return a;
  302. }
  303. StringSumHelper & operator + (const StringSumHelper &lhs, int num)
  304. {
  305. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  306. a.append((long)num);
  307. return a;
  308. }
  309. StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
  310. {
  311. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  312. a.append((unsigned long)num);
  313. return a;
  314. }
  315. StringSumHelper & operator + (const StringSumHelper &lhs, long num)
  316. {
  317. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  318. a.append(num);
  319. return a;
  320. }
  321. StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
  322. {
  323. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  324. a.append(num);
  325. return a;
  326. }
  327. StringSumHelper & operator + (const StringSumHelper &lhs, float num)
  328. {
  329. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  330. a.append(num);
  331. return a;
  332. }
  333. StringSumHelper & operator + (const StringSumHelper &lhs, double num)
  334. {
  335. StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
  336. a.append(num);
  337. return a;
  338. }
  339. /*********************************************/
  340. /* Comparison */
  341. /*********************************************/
  342. int String::compareTo(const String &s) const
  343. {
  344. if (!buffer || !s.buffer) {
  345. if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
  346. if (buffer && len > 0) return *(unsigned char *)buffer;
  347. return 0;
  348. }
  349. return strcmp(buffer, s.buffer);
  350. }
  351. unsigned char String::equals(const String &s2) const
  352. {
  353. return (len == s2.len && compareTo(s2) == 0);
  354. }
  355. unsigned char String::equals(const char *cstr) const
  356. {
  357. if (len == 0) return (cstr == NULL || *cstr == 0);
  358. if (cstr == NULL) return buffer[0] == 0;
  359. return strcmp(buffer, cstr) == 0;
  360. }
  361. unsigned char String::operator<(const String &rhs) const
  362. {
  363. return compareTo(rhs) < 0;
  364. }
  365. unsigned char String::operator>(const String &rhs) const
  366. {
  367. return compareTo(rhs) > 0;
  368. }
  369. unsigned char String::operator<=(const String &rhs) const
  370. {
  371. return compareTo(rhs) <= 0;
  372. }
  373. unsigned char String::operator>=(const String &rhs) const
  374. {
  375. return compareTo(rhs) >= 0;
  376. }
  377. unsigned char String::equalsIgnoreCase( const String &s2 ) const
  378. {
  379. if (this == &s2) return 1;
  380. if (len != s2.len) return 0;
  381. if (len == 0) return 1;
  382. const char *p1 = buffer;
  383. const char *p2 = s2.buffer;
  384. while (*p1) {
  385. if (tolower(*p1++) != tolower(*p2++)) return 0;
  386. }
  387. return 1;
  388. }
  389. unsigned char String::startsWith( const String &s2 ) const
  390. {
  391. if (len < s2.len) return 0;
  392. return startsWith(s2, 0);
  393. }
  394. unsigned char String::startsWith( const String &s2, unsigned int offset ) const
  395. {
  396. if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
  397. return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
  398. }
  399. unsigned char String::endsWith( const String &s2 ) const
  400. {
  401. if ( len < s2.len || !buffer || !s2.buffer) return 0;
  402. return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
  403. }
  404. /*********************************************/
  405. /* Character Access */
  406. /*********************************************/
  407. char String::charAt(unsigned int loc) const
  408. {
  409. return operator[](loc);
  410. }
  411. void String::setCharAt(unsigned int loc, char c)
  412. {
  413. if (loc < len) buffer[loc] = c;
  414. }
  415. char & String::operator[](unsigned int index)
  416. {
  417. static char dummy_writable_char;
  418. if (index >= len || !buffer) {
  419. dummy_writable_char = 0;
  420. return dummy_writable_char;
  421. }
  422. return buffer[index];
  423. }
  424. char String::operator[]( unsigned int index ) const
  425. {
  426. if (index >= len || !buffer) return 0;
  427. return buffer[index];
  428. }
  429. void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
  430. {
  431. if (!bufsize || !buf) return;
  432. if (index >= len) {
  433. buf[0] = 0;
  434. return;
  435. }
  436. unsigned int n = bufsize - 1;
  437. if (n > len - index) n = len - index;
  438. strncpy((char *)buf, buffer + index, n);
  439. buf[n] = 0;
  440. }
  441. /*********************************************/
  442. /* Search */
  443. /*********************************************/
  444. int String::indexOf(char c) const
  445. {
  446. return indexOf(c, 0);
  447. }
  448. int String::indexOf( char ch, unsigned int fromIndex ) const
  449. {
  450. if (fromIndex >= len) return -1;
  451. const char* temp = strchr(buffer + fromIndex, ch);
  452. if (temp == NULL) return -1;
  453. return temp - buffer;
  454. }
  455. int String::indexOf(const String &s2) const
  456. {
  457. return indexOf(s2, 0);
  458. }
  459. int String::indexOf(const String &s2, unsigned int fromIndex) const
  460. {
  461. if (fromIndex >= len) return -1;
  462. const char *found = strstr(buffer + fromIndex, s2.buffer);
  463. if (found == NULL) return -1;
  464. return found - buffer;
  465. }
  466. int String::lastIndexOf( char theChar ) const
  467. {
  468. return lastIndexOf(theChar, len - 1);
  469. }
  470. int String::lastIndexOf(char ch, unsigned int fromIndex) const
  471. {
  472. if (fromIndex >= len || fromIndex < 0) return -1;
  473. char tempchar = buffer[fromIndex + 1];
  474. buffer[fromIndex + 1] = '\0';
  475. char* temp = strrchr( buffer, ch );
  476. buffer[fromIndex + 1] = tempchar;
  477. if (temp == NULL) return -1;
  478. return temp - buffer;
  479. }
  480. int String::lastIndexOf(const String &s2) const
  481. {
  482. return lastIndexOf(s2, len - s2.len);
  483. }
  484. int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
  485. {
  486. if (s2.len == 0 || len == 0 || s2.len > len || fromIndex < 0) return -1;
  487. if (fromIndex >= len) fromIndex = len - 1;
  488. int found = -1;
  489. for (char *p = buffer; p <= buffer + fromIndex; p++) {
  490. p = strstr(p, s2.buffer);
  491. if (!p) break;
  492. if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
  493. }
  494. return found;
  495. }
  496. String String::substring( unsigned int left ) const
  497. {
  498. return substring(left, len);
  499. }
  500. String String::substring(unsigned int left, unsigned int right) const
  501. {
  502. if (left > right) {
  503. unsigned int temp = right;
  504. right = left;
  505. left = temp;
  506. }
  507. String out;
  508. if (left > len) return out;
  509. if (right > len) right = len;
  510. char temp = buffer[right]; // save the replaced character
  511. buffer[right] = '\0';
  512. out = buffer + left; // pointer arithmetic
  513. buffer[right] = temp; //restore character
  514. return out;
  515. }
  516. /*********************************************/
  517. /* Modification */
  518. /*********************************************/
  519. String & String::replace(char find, char replace)
  520. {
  521. if (!buffer) return *this;
  522. for (char *p = buffer; *p; p++) {
  523. if (*p == find) *p = replace;
  524. }
  525. return *this;
  526. }
  527. String & String::replace(const String& find, const String& replace)
  528. {
  529. if (len == 0 || find.len == 0) return *this;
  530. int diff = replace.len - find.len;
  531. char *readFrom = buffer;
  532. char *foundAt;
  533. if (diff == 0) {
  534. while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
  535. memcpy(foundAt, replace.buffer, replace.len);
  536. readFrom = foundAt + replace.len;
  537. }
  538. } else if (diff < 0) {
  539. char *writeTo = buffer;
  540. while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
  541. unsigned int n = foundAt - readFrom;
  542. memcpy(writeTo, readFrom, n);
  543. writeTo += n;
  544. memcpy(writeTo, replace.buffer, replace.len);
  545. writeTo += replace.len;
  546. readFrom = foundAt + find.len;
  547. len += diff;
  548. }
  549. strcpy(writeTo, readFrom);
  550. } else {
  551. unsigned int size = len; // compute size needed for result
  552. while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
  553. readFrom = foundAt + find.len;
  554. size += diff;
  555. }
  556. if (size == len) return *this;
  557. if (size > capacity && !changeBuffer(size)) return *this;
  558. int index = len - 1;
  559. while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
  560. readFrom = buffer + index + find.len;
  561. memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
  562. len += diff;
  563. buffer[len] = 0;
  564. memcpy(buffer + index, replace.buffer, replace.len);
  565. index--;
  566. }
  567. }
  568. return *this;
  569. }
  570. String & String::remove(unsigned int index)
  571. {
  572. if (index < len) {
  573. len = index;
  574. buffer[len] = 0;
  575. }
  576. return *this;
  577. }
  578. String & String::remove(unsigned int index, unsigned int count)
  579. {
  580. if (index < len && count > 0) {
  581. if (index + count > len) count = len - index;
  582. len = len - count;
  583. memmove(buffer + index, buffer + index + count, len - index);
  584. buffer[len] = 0;
  585. }
  586. return *this;
  587. }
  588. String & String::toLowerCase(void)
  589. {
  590. if (!buffer) return *this;
  591. for (char *p = buffer; *p; p++) {
  592. *p = tolower(*p);
  593. }
  594. return *this;
  595. }
  596. String & String::toUpperCase(void)
  597. {
  598. if (!buffer) return *this;
  599. for (char *p = buffer; *p; p++) {
  600. *p = toupper(*p);
  601. }
  602. return *this;
  603. }
  604. String & String::trim(void)
  605. {
  606. if (!buffer || len == 0) return *this;
  607. char *begin = buffer;
  608. while (isspace(*begin)) begin++;
  609. char *end = buffer + len - 1;
  610. while (isspace(*end) && end >= begin) end--;
  611. len = end + 1 - begin;
  612. if (begin > buffer) memcpy(buffer, begin, len);
  613. buffer[len] = 0;
  614. return *this;
  615. }
  616. /*********************************************/
  617. /* Parsing / Conversion */
  618. /*********************************************/
  619. long String::toInt(void) const
  620. {
  621. if (buffer) return atol(buffer);
  622. return 0;
  623. }
  624. float String::toFloat(void) const
  625. {
  626. if (buffer) return strtof(buffer, (char **)NULL);
  627. return 0.0;
  628. }