My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

WString.h 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. String(const __FlashStringHelper *str);
  52. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  53. String(String &&rval);
  54. String(StringSumHelper &&rval);
  55. #endif
  56. explicit String(char c);
  57. explicit String(unsigned char, unsigned char base=10);
  58. explicit String(int, unsigned char base=10);
  59. explicit String(unsigned int, unsigned char base=10);
  60. explicit String(long, unsigned char base=10);
  61. explicit String(unsigned long, unsigned char base=10);
  62. explicit String(float, unsigned char decimalPlaces=2);
  63. explicit String(double, unsigned char decimalPlaces=2);
  64. ~String(void);
  65. // memory management
  66. // return true on success, false on failure (in which case, the string
  67. // is left unchanged). reserve(0), if successful, will validate an
  68. // invalid string (i.e., "if (s)" will be true afterwards)
  69. unsigned char reserve(unsigned int size);
  70. inline unsigned int length(void) const {return len;}
  71. // creates a copy of the assigned value. if the value is null or
  72. // invalid, or if the memory allocation fails, the string will be
  73. // marked as invalid ("if (s)" will be false).
  74. String & operator = (const String &rhs);
  75. String & operator = (const char *cstr);
  76. String & operator = (const __FlashStringHelper *str);
  77. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  78. String & operator = (String &&rval);
  79. String & operator = (StringSumHelper &&rval);
  80. #endif
  81. // concatenate (works w/ built-in types)
  82. // returns true on success, false on failure (in which case, the string
  83. // is left unchanged). if the argument is null or invalid, the
  84. // concatenation is considered unsucessful.
  85. unsigned char concat(const String &str);
  86. unsigned char concat(const char *cstr);
  87. unsigned char concat(char c);
  88. unsigned char concat(unsigned char c);
  89. unsigned char concat(int num);
  90. unsigned char concat(unsigned int num);
  91. unsigned char concat(long num);
  92. unsigned char concat(unsigned long num);
  93. unsigned char concat(float num);
  94. unsigned char concat(double num);
  95. unsigned char concat(const __FlashStringHelper * str);
  96. // if there's not enough memory for the concatenated value, the string
  97. // will be left unchanged (but this isn't signalled in any way)
  98. String & operator += (const String &rhs) {concat(rhs); return (*this);}
  99. String & operator += (const char *cstr) {concat(cstr); return (*this);}
  100. String & operator += (char c) {concat(c); return (*this);}
  101. String & operator += (unsigned char num) {concat(num); return (*this);}
  102. String & operator += (int num) {concat(num); return (*this);}
  103. String & operator += (unsigned int num) {concat(num); return (*this);}
  104. String & operator += (long num) {concat(num); return (*this);}
  105. String & operator += (unsigned long num) {concat(num); return (*this);}
  106. String & operator += (float num) {concat(num); return (*this);}
  107. String & operator += (double num) {concat(num); return (*this);}
  108. String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
  109. friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
  110. friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
  111. friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
  112. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
  113. friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
  114. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
  115. friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
  116. friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
  117. friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
  118. friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
  119. friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
  120. // comparison (only works w/ Strings and "strings")
  121. operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
  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 operator == (const String &rhs) const {return equals(rhs);}
  126. unsigned char operator == (const char *cstr) const {return equals(cstr);}
  127. unsigned char operator != (const String &rhs) const {return !equals(rhs);}
  128. unsigned char operator != (const char *cstr) const {return !equals(cstr);}
  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 operator >= (const String &rhs) const;
  133. unsigned char equalsIgnoreCase(const String &s) const;
  134. unsigned char startsWith( const String &prefix) const;
  135. unsigned char startsWith(const String &prefix, unsigned int offset) const;
  136. unsigned char endsWith(const String &suffix) const;
  137. // character acccess
  138. char charAt(unsigned int index) const;
  139. void setCharAt(unsigned int index, char c);
  140. char operator [] (unsigned int index) const;
  141. char& operator [] (unsigned int index);
  142. void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
  143. void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
  144. { getBytes((unsigned char *)buf, bufsize, index); }
  145. const char* c_str() const { return buffer; }
  146. char* begin() { return buffer; }
  147. char* end() { return buffer + length(); }
  148. const char* begin() const { return c_str(); }
  149. const char* end() const { return c_str() + length(); }
  150. // search
  151. int indexOf( char ch ) const;
  152. int indexOf( char ch, unsigned int fromIndex ) const;
  153. int indexOf( const String &str ) const;
  154. int indexOf( const String &str, unsigned int fromIndex ) const;
  155. int lastIndexOf( char ch ) const;
  156. int lastIndexOf( char ch, unsigned int fromIndex ) const;
  157. int lastIndexOf( const String &str ) const;
  158. int lastIndexOf( const String &str, unsigned int fromIndex ) const;
  159. String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
  160. String substring( unsigned int beginIndex, unsigned int endIndex ) const;
  161. // modification
  162. void replace(char find, char replace);
  163. void replace(const String& find, const String& replace);
  164. void remove(unsigned int index);
  165. void remove(unsigned int index, unsigned int count);
  166. void toLowerCase(void);
  167. void toUpperCase(void);
  168. void trim(void);
  169. // parsing/conversion
  170. long toInt(void) const;
  171. float toFloat(void) const;
  172. double toDouble(void) const;
  173. protected:
  174. char *buffer; // the actual char array
  175. unsigned int capacity; // the array length minus one (for the '\0')
  176. unsigned int len; // the String length (not counting the '\0')
  177. protected:
  178. void init(void);
  179. void invalidate(void);
  180. unsigned char changeBuffer(unsigned int maxStrLen);
  181. unsigned char concat(const char *cstr, unsigned int length);
  182. // copy and move
  183. String & copy(const char *cstr, unsigned int length);
  184. String & copy(const __FlashStringHelper *pstr, unsigned int length);
  185. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
  186. void move(String &rhs);
  187. #endif
  188. };
  189. class StringSumHelper : public String
  190. {
  191. public:
  192. StringSumHelper(const String &s) : String(s) {}
  193. StringSumHelper(const char *p) : String(p) {}
  194. StringSumHelper(char c) : String(c) {}
  195. StringSumHelper(unsigned char num) : String(num) {}
  196. StringSumHelper(int num) : String(num) {}
  197. StringSumHelper(unsigned int num) : String(num) {}
  198. StringSumHelper(long num) : String(num) {}
  199. StringSumHelper(unsigned long num) : String(num) {}
  200. StringSumHelper(float num) : String(num) {}
  201. StringSumHelper(double num) : String(num) {}
  202. };
  203. #endif // __cplusplus
  204. #endif // String_class_h