Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 {
  152. if (!buffer) return &zerotermination; // https://forum.pjrc.com/threads/63842
  153. return buffer;
  154. }
  155. // search
  156. int indexOf( char ch ) const;
  157. int indexOf( char ch, unsigned int fromIndex ) const;
  158. int indexOf( const String &str ) const;
  159. int indexOf( const String &str, unsigned int fromIndex ) const;
  160. int lastIndexOf( char ch ) const;
  161. int lastIndexOf( char ch, unsigned int fromIndex ) const;
  162. int lastIndexOf( const String &str ) const;
  163. int lastIndexOf( const String &str, unsigned int fromIndex ) const;
  164. String substring( unsigned int beginIndex ) const;
  165. String substring( unsigned int beginIndex, unsigned int endIndex ) const;
  166. // modification
  167. String & replace(char find, char replace);
  168. String & replace(const String& find, const String& replace);
  169. String & remove(unsigned int index);
  170. String & remove(unsigned int index, unsigned int count);
  171. String & toLowerCase(void);
  172. String & toUpperCase(void);
  173. String & trim(void);
  174. // parsing/conversion
  175. long toInt(void) const;
  176. float toFloat(void) const;
  177. protected:
  178. char *buffer; // the actual char array
  179. unsigned int capacity; // the array length minus one (for the '\0')
  180. unsigned int len; // the String length (not counting the '\0')
  181. unsigned char flags; // unused, for future features
  182. protected:
  183. void init(void);
  184. unsigned char changeBuffer(unsigned int maxStrLen);
  185. String & append(const char *cstr, unsigned int length);
  186. private:
  187. // allow for "if (s)" without the complications of an operator bool().
  188. // for more information http://www.artima.com/cppsource/safebool.html
  189. typedef void (String::*StringIfHelperType)() const;
  190. void StringIfHelper() const {}
  191. static const char zerotermination;
  192. public:
  193. operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
  194. };
  195. class StringSumHelper : public String
  196. {
  197. public:
  198. StringSumHelper(const String &s) : String(s) {}
  199. StringSumHelper(const char *p) : String(p) {}
  200. StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {}
  201. StringSumHelper(char c) : String(c) {}
  202. StringSumHelper(unsigned char c) : String(c) {}
  203. StringSumHelper(int num) : String(num, 10) {}
  204. StringSumHelper(unsigned int num) : String(num, 10) {}
  205. StringSumHelper(long num) : String(num, 10) {}
  206. StringSumHelper(unsigned long num) : String(num, 10) {}
  207. };
  208. #endif // __cplusplus
  209. #endif // String_class_h