My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WString.h 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. class __FlashStringHelper;
  30. #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  31. // An inherited class for holding the result of a concatenation. These
  32. // result objects are assumed to be writable by subsequent concatenations.
  33. class StringSumHelper;
  34. // The string class
  35. class String
  36. {
  37. // use a function pointer to allow for "if (s)" without the
  38. // complications of an operator bool(). for more information, see:
  39. // http://www.artima.com/cppsource/safebool.html
  40. typedef void (String::*StringIfHelperType)() const;
  41. void StringIfHelper() const {}
  42. public:
  43. // constructors
  44. // creates a copy of the initial value.
  45. // if the initial value is null or invalid, or if memory allocation
  46. // fails, the string will be marked as invalid (i.e. "if (s)" will
  47. // be false).
  48. String(const char *cstr = "");
  49. String(const String &str);
  50. String(const __FlashStringHelper *str);
  51. #if __cplusplus >= 201103L || defined(__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. explicit String(float, unsigned char decimalPlaces=2);
  62. explicit String(double, unsigned char decimalPlaces=2);
  63. ~String(void);
  64. // memory management
  65. // return true on success, false on failure (in which case, the string
  66. // is left unchanged). reserve(0), if successful, will validate an
  67. // invalid string (i.e., "if (s)" will be true afterwards)
  68. unsigned char reserve(unsigned int size);
  69. inline unsigned int length(void) const {return len;}
  70. // creates a copy of the assigned value. if the value is null or
  71. // invalid, or if the memory allocation fails, the string will be
  72. // marked as invalid ("if (s)" will be false).
  73. String & operator = (const String &rhs);
  74. String & operator = (const char *cstr);
  75. String & operator = (const __FlashStringHelper *str);
  76. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  77. String & operator = (String &&rval);
  78. String & operator = (StringSumHelper &&rval);
  79. #endif
  80. // concatenate (works w/ built-in types)
  81. // returns true on success, false on failure (in which case, the string
  82. // is left unchanged). if the argument is null or invalid, the
  83. // concatenation is considered unsucessful.
  84. unsigned char concat(const String &str);
  85. unsigned char concat(const char *cstr);
  86. unsigned char concat(char c);
  87. unsigned char concat(unsigned char c);
  88. unsigned char concat(int num);
  89. unsigned char concat(unsigned int num);
  90. unsigned char concat(long num);
  91. unsigned char concat(unsigned long num);
  92. unsigned char concat(float num);
  93. unsigned char concat(double num);
  94. unsigned char concat(const __FlashStringHelper * str);
  95. // if there's not enough memory for the concatenated value, the string
  96. // will be left unchanged (but this isn't signalled in any way)
  97. String & operator += (const String &rhs) {concat(rhs); return (*this);}
  98. String & operator += (const char *cstr) {concat(cstr); return (*this);}
  99. String & operator += (char c) {concat(c); return (*this);}
  100. String & operator += (unsigned char num) {concat(num); return (*this);}
  101. String & operator += (int num) {concat(num); return (*this);}
  102. String & operator += (unsigned int num) {concat(num); return (*this);}
  103. String & operator += (long num) {concat(num); return (*this);}
  104. String & operator += (unsigned long num) {concat(num); return (*this);}
  105. String & operator += (float num) {concat(num); return (*this);}
  106. String & operator += (double num) {concat(num); return (*this);}
  107. String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
  108. friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
  109. friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
  110. friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
  111. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
  112. friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
  113. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
  114. friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
  115. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
  116. friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
  117. friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
  118. friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
  119. // comparison (only works w/ Strings and "strings")
  120. operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
  121. int compareTo(const String &s) const;
  122. unsigned char equals(const String &s) const;
  123. unsigned char equals(const char *cstr) const;
  124. unsigned char operator == (const String &rhs) const {return equals(rhs);}
  125. unsigned char operator == (const char *cstr) const {return equals(cstr);}
  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 String &rhs) const;
  129. unsigned char operator > (const String &rhs) const;
  130. unsigned char operator <= (const String &rhs) const;
  131. unsigned char operator >= (const String &rhs) const;
  132. unsigned char equalsIgnoreCase(const String &s) const;
  133. unsigned char startsWith( const String &prefix) const;
  134. unsigned char startsWith(const String &prefix, unsigned int offset) const;
  135. unsigned char endsWith(const String &suffix) const;
  136. // character acccess
  137. char charAt(unsigned int index) const;
  138. void setCharAt(unsigned int index, char c);
  139. char operator [] (unsigned int index) const;
  140. char& operator [] (unsigned int index);
  141. void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
  142. void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
  143. { getBytes((unsigned char *)buf, bufsize, index); }
  144. const char* c_str() const { return buffer; }
  145. char* begin() { return buffer; }
  146. char* end() { return buffer + length(); }
  147. const char* begin() const { return c_str(); }
  148. const char* end() const { return c_str() + length(); }
  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 { return substring(beginIndex, len); };
  159. String substring( unsigned int beginIndex, unsigned int endIndex ) const;
  160. // modification
  161. void replace(char find, char replace);
  162. void replace(const String& find, const String& replace);
  163. void remove(unsigned int index);
  164. void remove(unsigned int index, unsigned int count);
  165. void toLowerCase(void);
  166. void toUpperCase(void);
  167. void trim(void);
  168. // parsing/conversion
  169. long toInt(void) const;
  170. float toFloat(void) const;
  171. double toDouble(void) const;
  172. protected:
  173. char *buffer; // the actual char array
  174. unsigned int capacity; // the array length minus one (for the '\0')
  175. unsigned int len; // the String length (not counting the '\0')
  176. protected:
  177. void init(void);
  178. void invalidate(void);
  179. unsigned char changeBuffer(unsigned int maxStrLen);
  180. unsigned char concat(const char *cstr, unsigned int length);
  181. // copy and move
  182. String & copy(const char *cstr, unsigned int length);
  183. String & copy(const __FlashStringHelper *pstr, unsigned int length);
  184. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  185. void move(String &rhs);
  186. #endif
  187. };
  188. class StringSumHelper : public String
  189. {
  190. public:
  191. StringSumHelper(const String &s) : String(s) {}
  192. StringSumHelper(const char *p) : String(p) {}
  193. StringSumHelper(char c) : String(c) {}
  194. StringSumHelper(unsigned char num) : String(num) {}
  195. StringSumHelper(int num) : String(num) {}
  196. StringSumHelper(unsigned int num) : String(num) {}
  197. StringSumHelper(long num) : String(num) {}
  198. StringSumHelper(unsigned long num) : String(num) {}
  199. StringSumHelper(float num) : String(num) {}
  200. StringSumHelper(double num) : String(num) {}
  201. };
  202. #endif // __cplusplus
  203. #endif // String_class_h