You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 11 година
пре 10 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // 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. // Brian Cook's "no overhead" Flash String type (message on Dec 14, 2010)
  31. // modified by Mikal Hart for his FlashString library
  32. class __FlashStringHelper;
  33. #ifndef F
  34. #define F(string_literal) ((const __FlashStringHelper *)(string_literal))
  35. #endif
  36. // An inherited class for holding the result of a concatenation. These
  37. // result objects are assumed to be writable by subsequent concatenations.
  38. class StringSumHelper;
  39. // The string class
  40. class String
  41. {
  42. public:
  43. // constructors
  44. String(const char *cstr = (const char *)NULL);
  45. String(const __FlashStringHelper *pgmstr);
  46. String(const String &str);
  47. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  48. String(String &&rval);
  49. String(StringSumHelper &&rval);
  50. #endif
  51. String(char c);
  52. String(unsigned char c);
  53. String(int, unsigned char base=10);
  54. String(unsigned int, unsigned char base=10);
  55. String(long, unsigned char base=10);
  56. String(unsigned long, unsigned char base=10);
  57. String(float num, unsigned char digits=2);
  58. String(double num, unsigned char digits=2) : String((float)num, digits) {}
  59. ~String(void);
  60. // memory management
  61. unsigned char reserve(unsigned int size);
  62. inline unsigned int length(void) const {return len;}
  63. // copy and move
  64. String & copy(const char *cstr, unsigned int length);
  65. String & copy(const __FlashStringHelper *s) { return copy((const char *)s, strlen((const char *)s)); }
  66. void move(String &rhs);
  67. String & operator = (const String &rhs);
  68. String & operator = (const char *cstr);
  69. String & operator = (const __FlashStringHelper *pgmstr);
  70. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  71. String & operator = (String &&rval);
  72. String & operator = (StringSumHelper &&rval);
  73. #endif
  74. String & operator = (char c);
  75. // append
  76. String & append(const String &str);
  77. String & append(const char *cstr);
  78. String & append(const __FlashStringHelper *s) {return append((const char *)s, strlen((const char *)s)); }
  79. String & append(char c);
  80. String & append(unsigned char c) {return append((int)c);}
  81. String & append(int num);
  82. String & append(unsigned int num);
  83. String & append(long num);
  84. String & append(unsigned 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 += (float num) {return append(num);}
  97. String & operator += (double num) {return append(num);}
  98. // concatenate
  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, const __FlashStringHelper *pgmstr);
  102. friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
  103. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c);
  104. friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
  105. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
  106. friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
  107. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
  108. friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
  109. friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
  110. String & concat(const String &str) {return append(str);}
  111. String & concat(const char *cstr) {return append(cstr);}
  112. String & concat(const __FlashStringHelper *pgmstr) {return append(pgmstr);}
  113. String & concat(char c) {return append(c);}
  114. String & concat(unsigned char c) {return append((int)c);}
  115. String & concat(int num) {return append(num);}
  116. String & concat(unsigned int num) {return append(num);}
  117. String & concat(long num) {return append(num);}
  118. String & concat(unsigned long num) {return append(num);}
  119. String & concat(float num) {return append(num);}
  120. String & concat(double num) {return append(num);}
  121. // comparison
  122. int compareTo(const String &s) const;
  123. unsigned char equals(const String &s) const;
  124. unsigned char equals(const char *cstr) const;
  125. //unsigned char equals(const __FlashStringHelper *pgmstr) const;
  126. unsigned char operator == (const String &rhs) const {return equals(rhs);}
  127. unsigned char operator == (const char *cstr) const {return equals(cstr);}
  128. unsigned char operator == (const __FlashStringHelper *s) const {return equals((const char *)s);}
  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(s);}
  132. unsigned char operator < (const String &rhs) const;
  133. unsigned char operator > (const String &rhs) const;
  134. unsigned char operator <= (const String &rhs) const;
  135. unsigned char operator >= (const String &rhs) const;
  136. unsigned char equalsIgnoreCase(const String &s) const;
  137. unsigned char startsWith( const String &prefix) const;
  138. unsigned char startsWith(const String &prefix, unsigned int offset) const;
  139. unsigned char endsWith(const String &suffix) const;
  140. // character acccess
  141. char charAt(unsigned int index) const;
  142. void setCharAt(unsigned int index, char c);
  143. char operator [] (unsigned int index) const;
  144. char& operator [] (unsigned int index);
  145. void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
  146. void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
  147. {getBytes((unsigned char *)buf, bufsize, index);}
  148. const char * c_str() const { return buffer; }
  149. // search
  150. int indexOf( char ch ) const;
  151. int indexOf( char ch, unsigned int fromIndex ) const;
  152. int indexOf( const String &str ) const;
  153. int indexOf( const String &str, unsigned int fromIndex ) const;
  154. int lastIndexOf( char ch ) const;
  155. int lastIndexOf( char ch, unsigned int fromIndex ) const;
  156. int lastIndexOf( const String &str ) const;
  157. int lastIndexOf( const String &str, unsigned int fromIndex ) const;
  158. String substring( unsigned int beginIndex ) const;
  159. String substring( unsigned int beginIndex, unsigned int endIndex ) const;
  160. // modification
  161. String & replace(char find, char replace);
  162. String & replace(const String& find, const String& replace);
  163. String & remove(unsigned int index);
  164. String & remove(unsigned int index, unsigned int count);
  165. String & toLowerCase(void);
  166. String & toUpperCase(void);
  167. String & trim(void);
  168. // parsing/conversion
  169. long toInt(void) const;
  170. float toFloat(void) const;
  171. protected:
  172. char *buffer; // the actual char array
  173. unsigned int capacity; // the array length minus one (for the '\0')
  174. unsigned int len; // the String length (not counting the '\0')
  175. unsigned char flags; // unused, for future features
  176. protected:
  177. void init(void);
  178. unsigned char changeBuffer(unsigned int maxStrLen);
  179. String & append(const char *cstr, unsigned int length);
  180. };
  181. class StringSumHelper : public String
  182. {
  183. public:
  184. StringSumHelper(const String &s) : String(s) {}
  185. StringSumHelper(const char *p) : String(p) {}
  186. StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {}
  187. StringSumHelper(char c) : String(c) {}
  188. StringSumHelper(unsigned char c) : String(c) {}
  189. StringSumHelper(int num) : String(num, 10) {}
  190. StringSumHelper(unsigned int num) : String(num, 10) {}
  191. StringSumHelper(long num) : String(num, 10) {}
  192. StringSumHelper(unsigned long num) : String(num, 10) {}
  193. };
  194. #endif // __cplusplus
  195. #endif // String_class_h