Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

228 linhas
9.5KB

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