WString.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. WString.h - String library for Wiring & Arduino
  3. ...mostly rewritten by Paul Stoffregen...
  4. Copyright (c) 2009-10 Hernando Barragan. All right 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. #ifndef String_class_h
  19. #define String_class_h
  20. #ifdef __cplusplus
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. // When compiling programs with this class, the following gcc parameters
  25. // dramatically increase performance and memory (RAM) efficiency, typically
  26. // with little or no increase in code size.
  27. // -felide-constructors
  28. // -std=c++0x
  29. // Brian Cook's "no overhead" Flash String type (message on Dec 14, 2010)
  30. // modified by Mikal Hart for his FlashString library
  31. class __FlashStringHelper;
  32. // An inherited class for holding the result of a concatenation. These
  33. // result objects are assumed to be writable by subsequent concatenations.
  34. class StringSumHelper;
  35. // The string class
  36. class String
  37. {
  38. public:
  39. // constructors
  40. String(const char *cstr = (const char *)NULL);
  41. String(const __FlashStringHelper *pgmstr);
  42. String(const String &str);
  43. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  44. String(String &&rval);
  45. String(StringSumHelper &&rval);
  46. #endif
  47. String(char c);
  48. String(unsigned char c);
  49. String(int, unsigned char base=10);
  50. String(unsigned int, unsigned char base=10);
  51. String(long, unsigned char base=10);
  52. String(unsigned long, unsigned char base=10);
  53. String(long long, unsigned char base=10);
  54. String(unsigned long long, unsigned char base=10);
  55. String(float num, unsigned char digits=2);
  56. String(double num, unsigned char digits=2) : String((float)num, digits) {}
  57. ~String(void);
  58. // memory management
  59. unsigned char reserve(unsigned int size);
  60. inline unsigned int length(void) const {return len;}
  61. // copy and move
  62. String & copy(const char *cstr, unsigned int length);
  63. String & copy(const __FlashStringHelper *s) { return copy((const char *)s, strlen((const char *)s)); }
  64. void move(String &rhs);
  65. String & operator = (const String &rhs);
  66. String & operator = (const char *cstr);
  67. String & operator = (const __FlashStringHelper *pgmstr);
  68. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  69. String & operator = (String &&rval);
  70. String & operator = (StringSumHelper &&rval);
  71. #endif
  72. String & operator = (char c);
  73. // append
  74. String & append(const String &str);
  75. String & append(const char *cstr);
  76. String & append(const __FlashStringHelper *s) {return append((const char *)s, strlen((const char *)s)); }
  77. String & append(char c);
  78. String & append(unsigned char c) {return append((int)c);}
  79. String & append(int num);
  80. String & append(unsigned int num);
  81. String & append(long num);
  82. String & append(unsigned long num);
  83. String & append(long long num);
  84. String & append(unsigned long long num);
  85. String & append(float num);
  86. String & append(double num) {return append((float)num);}
  87. String & operator += (const String &rhs) {return append(rhs);}
  88. String & operator += (const char *cstr) {return append(cstr);}
  89. String & operator += (const __FlashStringHelper *pgmstr) {return append(pgmstr);}
  90. String & operator += (char c) {return append(c);}
  91. String & operator += (unsigned char c) {return append((int)c);}
  92. String & operator += (int num) {return append(num);}
  93. String & operator += (unsigned int num) {return append(num);}
  94. String & operator += (long num) {return append(num);}
  95. String & operator += (unsigned long num) {return append(num);}
  96. String & operator += (long long num) {return append(num);}
  97. String & operator += (unsigned long long num) {return append(num);}
  98. String & operator += (float num) {return append(num);}
  99. String & operator += (double num) {return append(num);}
  100. // concatenate
  101. friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
  102. friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
  103. friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *pgmstr);
  104. friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
  105. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c);
  106. friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
  107. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
  108. friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
  109. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
  110. friend StringSumHelper & operator + (const StringSumHelper &lhs, long long num);
  111. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long long num);
  112. friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
  113. friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
  114. String & concat(const String &str) {return append(str);}
  115. String & concat(const char *cstr) {return append(cstr);}
  116. String & concat(const __FlashStringHelper *pgmstr) {return append(pgmstr);}
  117. String & concat(char c) {return append(c);}
  118. String & concat(unsigned char c) {return append((int)c);}
  119. String & concat(int num) {return append(num);}
  120. String & concat(unsigned int num) {return append(num);}
  121. String & concat(long num) {return append(num);}
  122. String & concat(unsigned long num) {return append(num);}
  123. String & concat(long long num) {return append(num);}
  124. String & concat(unsigned long long num) {return append(num);}
  125. String & concat(float num) {return append(num);}
  126. String & concat(double num) {return append(num);}
  127. // comparison
  128. int compareTo(const String &s) const;
  129. unsigned char equals(const String &s) const;
  130. unsigned char equals(const char *cstr) const;
  131. //unsigned char equals(const __FlashStringHelper *pgmstr) const;
  132. unsigned char operator == (const String &rhs) const {return equals(rhs);}
  133. unsigned char operator == (const char *cstr) const {return equals(cstr);}
  134. unsigned char operator != (const String &rhs) const {return !equals(rhs);}
  135. unsigned char operator != (const char *cstr) const {return !equals(cstr);}
  136. unsigned char operator < (const String &rhs) const;
  137. unsigned char operator > (const String &rhs) const;
  138. unsigned char operator <= (const String &rhs) const;
  139. unsigned char operator >= (const String &rhs) const;
  140. unsigned char equalsIgnoreCase(const String &s) const;
  141. unsigned char startsWith( const String &prefix) const;
  142. unsigned char startsWith(const String &prefix, unsigned int offset) const;
  143. unsigned char endsWith(const String &suffix) const;
  144. // character acccess
  145. char charAt(unsigned int index) const;
  146. void setCharAt(unsigned int index, char c);
  147. char operator [] (unsigned int index) const;
  148. char& operator [] (unsigned int index);
  149. void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
  150. void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
  151. {getBytes((unsigned char *)buf, bufsize, index);}
  152. const char * c_str() const {
  153. if (!buffer) return &zerotermination; // https://forum.pjrc.com/threads/63842
  154. return buffer;
  155. }
  156. char * begin() {
  157. if (!buffer) reserve(20);
  158. return buffer;
  159. }
  160. char * end() { return begin() + length(); }
  161. const char * begin() const { return c_str(); }
  162. const char * end() const { return c_str() + length(); }
  163. // search
  164. int indexOf( char ch ) const;
  165. int indexOf( char ch, unsigned int fromIndex ) const;
  166. int indexOf( const String &str ) const;
  167. int indexOf( const String &str, unsigned int fromIndex ) const;
  168. int lastIndexOf( char ch ) const;
  169. int lastIndexOf( char ch, unsigned int fromIndex ) const;
  170. int lastIndexOf( const String &str ) const;
  171. int lastIndexOf( const String &str, unsigned int fromIndex ) const;
  172. String substring( unsigned int beginIndex ) const;
  173. String substring( unsigned int beginIndex, unsigned int endIndex ) const;
  174. // modification
  175. String & replace(char find, char replace);
  176. String & replace(const String& find, const String& replace);
  177. String & remove(unsigned int index);
  178. String & remove(unsigned int index, unsigned int count);
  179. String & toLowerCase(void);
  180. String & toUpperCase(void);
  181. String & trim(void);
  182. // parsing/conversion
  183. long toInt(void) const;
  184. float toFloat(void) const;
  185. protected:
  186. char *buffer; // the actual char array
  187. unsigned int capacity; // the array length minus one (for the '\0')
  188. unsigned int len; // the String length (not counting the '\0')
  189. unsigned char flags; // unused, for future features
  190. protected:
  191. void init(void);
  192. unsigned char changeBuffer(unsigned int maxStrLen);
  193. String & append(const char *cstr, unsigned int length);
  194. private:
  195. // allow for "if (s)" without the complications of an operator bool().
  196. // for more information http://www.artima.com/cppsource/safebool.html
  197. typedef void (String::*StringIfHelperType)() const;
  198. void StringIfHelper() const {}
  199. static const char zerotermination;
  200. public:
  201. operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
  202. };
  203. class StringSumHelper : public String
  204. {
  205. public:
  206. StringSumHelper(const String &s) : String(s) {}
  207. StringSumHelper(const char *p) : String(p) {}
  208. StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {}
  209. StringSumHelper(char c) : String(c) {}
  210. StringSumHelper(unsigned char c) : String(c) {}
  211. StringSumHelper(int num) : String(num, 10) {}
  212. StringSumHelper(unsigned int num) : String(num, 10) {}
  213. StringSumHelper(long num) : String(num, 10) {}
  214. StringSumHelper(unsigned long num) : String(num, 10) {}
  215. StringSumHelper(long long num) : String(num, 10) {}
  216. StringSumHelper(unsigned long long num) : String(num, 10) {}
  217. };
  218. #endif // __cplusplus
  219. #endif // String_class_h