WString.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include <avr/pgmspace.h>
  25. // When compiling programs with this class, the following gcc parameters
  26. // dramatically increase performance and memory (RAM) efficiency, typically
  27. // with little or no increase in code size.
  28. // -felide-constructors
  29. // -std=c++0x
  30. class __FlashStringHelper;
  31. #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  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. // use a function pointer to allow for "if (s)" without the
  39. // complications of an operator bool(). for more information, see:
  40. // http://www.artima.com/cppsource/safebool.html
  41. typedef void (String::*StringIfHelperType)() const;
  42. void StringIfHelper() const {}
  43. public:
  44. // constructors
  45. // creates a copy of the initial value.
  46. // if the initial value is null or invalid, or if memory allocation
  47. // fails, the string will be marked as invalid (i.e. "if (s)" will
  48. // be false).
  49. String(const char *cstr = "");
  50. String(const String &str);
  51. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  52. String(String &&rval);
  53. String(StringSumHelper &&rval);
  54. #endif
  55. explicit String(char c);
  56. explicit String(unsigned char, unsigned char base=10);
  57. explicit String(int, unsigned char base=10);
  58. explicit String(unsigned int, unsigned char base=10);
  59. explicit String(long, unsigned char base=10);
  60. explicit String(unsigned long, unsigned char base=10);
  61. ~String(void);
  62. // memory management
  63. // return true on success, false on failure (in which case, the string
  64. // is left unchanged). reserve(0), if successful, will validate an
  65. // invalid string (i.e., "if (s)" will be true afterwards)
  66. unsigned char reserve(unsigned int size);
  67. inline unsigned int length(void) const {return len;}
  68. // creates a copy of the assigned value. if the value is null or
  69. // invalid, or if the memory allocation fails, the string will be
  70. // marked as invalid ("if (s)" will be false).
  71. String & operator = (const String &rhs);
  72. String & operator = (const char *cstr);
  73. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  74. String & operator = (String &&rval);
  75. String & operator = (StringSumHelper &&rval);
  76. #endif
  77. // concatenate (works w/ built-in types)
  78. // returns true on success, false on failure (in which case, the string
  79. // is left unchanged). if the argument is null or invalid, the
  80. // concatenation is considered unsucessful.
  81. unsigned char concat(const String &str);
  82. unsigned char concat(const char *cstr);
  83. unsigned char concat(char c);
  84. unsigned char concat(unsigned char c);
  85. unsigned char concat(int num);
  86. unsigned char concat(unsigned int num);
  87. unsigned char concat(long num);
  88. unsigned char concat(unsigned long num);
  89. // if there's not enough memory for the concatenated value, the string
  90. // will be left unchanged (but this isn't signalled in any way)
  91. String & operator += (const String &rhs) {concat(rhs); return (*this);}
  92. String & operator += (const char *cstr) {concat(cstr); return (*this);}
  93. String & operator += (char c) {concat(c); return (*this);}
  94. String & operator += (unsigned char num) {concat(num); return (*this);}
  95. String & operator += (int num) {concat(num); return (*this);}
  96. String & operator += (unsigned int num) {concat(num); return (*this);}
  97. String & operator += (long num) {concat(num); return (*this);}
  98. String & operator += (unsigned long num) {concat(num); return (*this);}
  99. friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
  100. friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
  101. friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
  102. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
  103. friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
  104. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
  105. friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
  106. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
  107. // comparison (only works w/ Strings and "strings")
  108. operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
  109. int compareTo(const String &s) const;
  110. unsigned char equals(const String &s) const;
  111. unsigned char equals(const char *cstr) const;
  112. unsigned char operator == (const String &rhs) const {return equals(rhs);}
  113. unsigned char operator == (const char *cstr) const {return equals(cstr);}
  114. unsigned char operator != (const String &rhs) const {return !equals(rhs);}
  115. unsigned char operator != (const char *cstr) const {return !equals(cstr);}
  116. unsigned char operator < (const String &rhs) const;
  117. unsigned char operator > (const String &rhs) const;
  118. unsigned char operator <= (const String &rhs) const;
  119. unsigned char operator >= (const String &rhs) const;
  120. unsigned char equalsIgnoreCase(const String &s) const;
  121. unsigned char startsWith( const String &prefix) const;
  122. unsigned char startsWith(const String &prefix, unsigned int offset) const;
  123. unsigned char endsWith(const String &suffix) const;
  124. // character acccess
  125. char charAt(unsigned int index) const;
  126. void setCharAt(unsigned int index, char c);
  127. char operator [] (unsigned int index) const;
  128. char& operator [] (unsigned int index);
  129. void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
  130. void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
  131. {getBytes((unsigned char *)buf, bufsize, index);}
  132. // search
  133. int indexOf( char ch ) const;
  134. int indexOf( char ch, unsigned int fromIndex ) const;
  135. int indexOf( const String &str ) const;
  136. int indexOf( const String &str, unsigned int fromIndex ) const;
  137. int lastIndexOf( char ch ) const;
  138. int lastIndexOf( char ch, unsigned int fromIndex ) const;
  139. int lastIndexOf( const String &str ) const;
  140. int lastIndexOf( const String &str, unsigned int fromIndex ) const;
  141. String substring( unsigned int beginIndex ) const;
  142. String substring( unsigned int beginIndex, unsigned int endIndex ) const;
  143. // modification
  144. void replace(char find, char replace);
  145. void replace(const String& find, const String& replace);
  146. void toLowerCase(void);
  147. void toUpperCase(void);
  148. void trim(void);
  149. // parsing/conversion
  150. long toInt(void) const;
  151. protected:
  152. char *buffer; // the actual char array
  153. unsigned int capacity; // the array length minus one (for the '\0')
  154. unsigned int len; // the String length (not counting the '\0')
  155. unsigned char flags; // unused, for future features
  156. protected:
  157. void init(void);
  158. void invalidate(void);
  159. unsigned char changeBuffer(unsigned int maxStrLen);
  160. unsigned char concat(const char *cstr, unsigned int length);
  161. // copy and move
  162. String & copy(const char *cstr, unsigned int length);
  163. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  164. void move(String &rhs);
  165. #endif
  166. };
  167. class StringSumHelper : public String
  168. {
  169. public:
  170. StringSumHelper(const String &s) : String(s) {}
  171. StringSumHelper(const char *p) : String(p) {}
  172. StringSumHelper(char c) : String(c) {}
  173. StringSumHelper(unsigned char num) : String(num) {}
  174. StringSumHelper(int num) : String(num) {}
  175. StringSumHelper(unsigned int num) : String(num) {}
  176. StringSumHelper(long num) : String(num) {}
  177. StringSumHelper(unsigned long num) : String(num) {}
  178. };
  179. #endif // __cplusplus
  180. #endif // String_class_h