選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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