JsonParserGeneratorRK
spark_wiring_string.h
1 
29 #ifndef String_class_h
30 #define String_class_h
31 //#ifdef __cplusplus
32 
33 #include <stdarg.h>
34 #include "spark_wiring_print.h" // for HEX, DEC ... constants
35 #include "spark_wiring_printable.h"
36 
37 // When compiling programs with this class, the following gcc parameters
38 // dramatically increase performance and memory (RAM) efficiency, typically
39 // with little or no increase in code size.
40 // -felide-constructors
41 // -std=c++0x
42 
43 class __FlashStringHelper;
44 #define F(X) (X)
45 
46 // An inherited class for holding the result of a concatenation. These
47 // result objects are assumed to be writable by subsequent concatenations.
48 class StringSumHelper;
49 
50 
54 class String
55 {
56  // use a function pointer to allow for "if (s)" without the
57  // complications of an operator bool(). for more information, see:
58  // http://www.artima.com/cppsource/safebool.html
59  typedef void (String::*StringIfHelperType)() const;
60  void StringIfHelper() const {}
61 
62 public:
63  // constructors
64  // creates a copy of the initial value.
65  // if the initial value is null or invalid, or if memory allocation
66  // fails, the string will be marked as invalid (i.e. "if (s)" will
67  // be false).
68 
74  String(const char *cstr = "");
75 
83  String(const char *cstr, unsigned int length);
84 
90  String(const String &str);
91 
92 #ifndef DOXYGEN_DO_NOT_DOCUMENT
93  String(const __FlashStringHelper *pstr);
94 #endif
95 
105  String(const Printable& printable);
106  #ifdef __GXX_EXPERIMENTAL_CXX0X__
107  String(String &&rval);
108  String(StringSumHelper &&rval);
109  #endif
110 
116  explicit String(char c);
117 
125  explicit String(unsigned char b, unsigned char base=10);
126 
134  explicit String(int value, unsigned char base=10);
135 
143  explicit String(unsigned int value, unsigned char base=10);
144 
152  explicit String(long value, unsigned char base=10);
153 
161  explicit String(unsigned long value, unsigned char base=10);
162 
170  explicit String(float value, int decimalPlaces=6);
171 
179  explicit String(double value, int decimalPlaces=6);
180 
184  ~String(void);
185 
186  // memory management
187  // return true on success, false on failure (in which case, the string
188  // is left unchanged). reserve(0), if successful, will validate an
189  // invalid string (i.e., "if (s)" will be true afterwards)
190 
201  unsigned char reserve(unsigned int size);
202 
208  inline unsigned int length(void) const {return len;}
209 
210  // creates a copy of the assigned value. if the value is null or
211  // invalid, or if the memory allocation fails, the string will be
212  // marked as invalid ("if (s)" will be false).
213 
219  String & operator = (const String &rhs);
220 
226  String & operator = (const char *cstr);
227 
228 #ifndef DOXYGEN_DO_NOT_DOCUMENT
229  String & operator = (const __FlashStringHelper *pstr);
230 #endif
231  #ifdef __GXX_EXPERIMENTAL_CXX0X__
232  String & operator = (String &&rval);
234  #endif
235 
241  operator const char*() const { return c_str(); }
242 
243  // concatenate (works w/ built-in types)
244 
245  // returns true on success, false on failure (in which case, the string
246  // is left unchanged). if the argument is null or invalid, the
247  // concatenation is considered unsucessful.
248 
256  unsigned char concat(const String &str);
257 
265  unsigned char concat(const char *cstr);
266 #ifndef DOXYGEN_DO_NOT_DOCUMENT
267  unsigned char concat(const __FlashStringHelper * str);
268 #endif
269 
276  unsigned char concat(char c);
277 
285  unsigned char concat(unsigned char c);
286 
294  unsigned char concat(int num);
295 
303  unsigned char concat(unsigned int num);
304 
312  unsigned char concat(long num);
313 
321  unsigned char concat(unsigned long num);
322 
330  unsigned char concat(float num);
331 
339  unsigned char concat(double num);
340 
341  // if there's not enough memory for the concatenated value, the string
342  // will be left unchanged (but this isn't signalled in any way)
343 
352  String & operator += (const String &rhs) {concat(rhs); return (*this);}
353 
362  String & operator += (const char *cstr) {concat(cstr); return (*this);}
363 
372  String & operator += (char c) {concat(c); return (*this);}
373 
382  String & operator += (unsigned char num) {concat(num); return (*this);}
383 
392  String & operator += (int num) {concat(num); return (*this);}
393 
402  String & operator += (unsigned int num) {concat(num); return (*this);}
403 
412  String & operator += (long num) {concat(num); return (*this);}
413 
422  String & operator += (unsigned long num) {concat(num); return (*this);}
423 
433  friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
434 
444  friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
445 
455  friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
456 
466  friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
467 
477  friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
478 
488  friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
489 
499  friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
500 
510  friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
511 
521  friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
522 
532  friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
533 
534 #ifndef DOXYGEN_DO_NOT_DOCUMENT
535  // comparison (only works w/ Strings and "strings")
536  operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
537 #endif
538 
549  int compareTo(const String &s) const;
550 
561  unsigned char equals(const String &s) const;
562 
573  unsigned char equals(const char *cstr) const;
574 
585  unsigned char operator == (const String &rhs) const {return equals(rhs);}
586 
597  unsigned char operator == (const char *cstr) const {return equals(cstr);}
598 
599 
610  unsigned char operator != (const String &rhs) const {return !equals(rhs);}
611 
622  unsigned char operator != (const char *cstr) const {return !equals(cstr);}
623 
634  unsigned char operator < (const String &rhs) const;
635 
646  unsigned char operator > (const String &rhs) const;
647 
658  unsigned char operator <= (const String &rhs) const;
659 
670  unsigned char operator >= (const String &rhs) const;
671 
682  unsigned char equalsIgnoreCase(const String &s) const;
683 
692  unsigned char startsWith( const String &prefix) const;
693 
704  unsigned char startsWith(const String &prefix, unsigned int offset) const;
705 
714  unsigned char endsWith(const String &suffix) const;
715 
716  // character acccess
717 
718 
726  char charAt(unsigned int index) const;
727 
738  void setCharAt(unsigned int index, char c);
739 
747  char operator [] (unsigned int index) const;
748 
760  char& operator [] (unsigned int index);
761 
776  void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
777 
792  void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
793  {getBytes((unsigned char *)buf, bufsize, index);}
794 
819  const char * c_str() const { return buffer; }
820 
821  // search
822 
833  int indexOf( char ch ) const;
834 
847  int indexOf( char ch, unsigned int fromIndex ) const;
848 
859  int indexOf( const String &str ) const;
860 
873  int indexOf( const String &str, unsigned int fromIndex ) const;
874 
885  int lastIndexOf( char ch ) const;
886 
899  int lastIndexOf( char ch, unsigned int fromIndex ) const;
900 
911  int lastIndexOf( const String &str ) const;
912 
913 
926  int lastIndexOf( const String &str, unsigned int fromIndex ) const;
927 
928 
939  String substring( unsigned int beginIndex ) const;
940 
953  String substring( unsigned int beginIndex, unsigned int endIndex ) const;
954 
955  // modification
956 
966  String& replace(char find, char replace);
967 
977  String& replace(const String& find, const String& replace);
978 
987  String& remove(unsigned int index);
988 
1000  String& remove(unsigned int index, unsigned int count);
1001 
1010  String& toLowerCase(void);
1011 
1020  String& toUpperCase(void);
1021 
1029  String& trim(void);
1030 
1031  // parsing/conversion
1037  long toInt(void) const;
1038 
1044  float toFloat(void) const;
1045 
1055  static String format(const char* format, ...);
1056 
1057 protected:
1058  char *buffer;
1059  unsigned int capacity;
1060  unsigned int len;
1061  unsigned char flags;
1062 protected:
1063 #ifndef DOXYGEN_DO_NOT_DOCUMENT
1064  void init(void);
1065  void invalidate(void);
1066  unsigned char changeBuffer(unsigned int maxStrLen);
1067  unsigned char concat(const char *cstr, unsigned int length);
1068 
1069  // copy and move
1070  String & copy(const char *cstr, unsigned int length);
1071  String & copy(const __FlashStringHelper *pstr, unsigned int length);
1072 #endif
1073 
1074  #ifdef __GXX_EXPERIMENTAL_CXX0X__
1075  void move(String &rhs);
1076  #endif
1077 
1078  friend class StringPrintableHelper;
1079 
1080 };
1081 
1085 class StringSumHelper : public String
1086 {
1087 public:
1095  StringSumHelper(const String &s) : String(s) {}
1096 
1104  StringSumHelper(const char *p) : String(p) {}
1105 
1113  StringSumHelper(char c) : String(c) {}
1114 
1122  StringSumHelper(unsigned char num) : String(num) {}
1123 
1131  StringSumHelper(int num) : String(num) {}
1132 
1140  StringSumHelper(unsigned int num) : String(num) {}
1141 
1149  StringSumHelper(long num) : String(num) {}
1150 
1158  StringSumHelper(unsigned long num) : String(num) {}
1159 };
1160 
1161 #ifndef DOXYGEN_DO_NOT_DOCUMENT
1162 #include <ostream>
1163 std::ostream& operator << ( std::ostream& os, const String& value );
1164 #endif
1165 
1166 
1167 //#endif // __cplusplus
1168 #endif // String_class_h
unsigned char operator==(const String &rhs) const
Returns true if this string is equal to another string (case-sensitive)
Definition: spark_wiring_string.h:585
unsigned char equalsIgnoreCase(const String &s) const
Returns true if this string equals another string (case-insensitive)
String substring(unsigned int beginIndex) const
Returns a String object with a copy of the characters starting at beginIndex through the end of the s...
String & toUpperCase(void)
Converts this String to upper case, modifying it in place.
unsigned char flags
Unused, for future features.
Definition: spark_wiring_string.h:1061
StringSumHelper(unsigned char num)
Append a byte as a decimal number 0 - 255.
Definition: spark_wiring_string.h:1122
int lastIndexOf(char ch) const
Search this string for a given character, starting at the end.
StringSumHelper(unsigned long num)
Append a 32-bit unsigned long as a decimal number.
Definition: spark_wiring_string.h:1158
float toFloat(void) const
Converts this string to a float (single precision floating point value)
void setCharAt(unsigned int index, char c)
Set the character at offset index.
char charAt(unsigned int index) const
Gets the character at offset index.
unsigned char startsWith(const String &prefix) const
Returns true if this string starts with prefix (case-sensitive)
friend StringSumHelper & operator+(const StringSumHelper &lhs, const String &rhs)
Append (concatenate) a String to the end of lhs.
String & trim(void)
Removes leading an trailing white spaces from this string, modifying it in place. ...
unsigned char reserve(unsigned int size)
Reserves a buffer of size.
unsigned int capacity
The capacity of the buffer. The longest string is one byte less than this.
Definition: spark_wiring_string.h:1059
StringSumHelper(unsigned int num)
Append a 32-bit unsigned integer as a decimal number.
Definition: spark_wiring_string.h:1140
String(const char *cstr="")
Construct a String object from a c-string (null-terminated)
unsigned char operator>(const String &rhs) const
Returns true if this string is greater than to another string (case-sensitive)
char * buffer
The buffer containing the data. It is always null-terminated.
Definition: spark_wiring_string.h:1058
int compareTo(const String &s) const
Compares this string to another string using strcmp (case-sensitive)
String & operator=(const String &rhs)
Assigns this string to have a copy of String rhs.
StringSumHelper(const char *p)
Append a const char * (c-string, null terminated)
Definition: spark_wiring_string.h:1104
unsigned char concat(const String &str)
Append (concatenate) a String object to the end of this String, modifying this string in place...
char operator[](unsigned int index) const
Gets the character at offset index.
Class used when appending mutiple String and other values using +.
Definition: spark_wiring_string.h:1085
unsigned char operator<(const String &rhs) const
Returns true if this string is less than to another string (case-sensitive)
String & toLowerCase(void)
Converts this String to lower case, modifying it in place.
The Printable class provides a way for new classes to allow themselves to be printed.
Definition: spark_wiring_printable.h:41
StringSumHelper(const String &s)
Append a String object.
Definition: spark_wiring_string.h:1095
StringSumHelper(int num)
Append a 32-bit signed integer as a decimal number.
Definition: spark_wiring_string.h:1131
long toInt(void) const
Converts this string to a signed integer (32-bit)
int indexOf(char ch) const
Search this string for a given character.
StringSumHelper(char c)
Append a single character.
Definition: spark_wiring_string.h:1113
StringSumHelper(long num)
Append a 32-bit long integer as a decimal number.
Definition: spark_wiring_string.h:1149
unsigned int length(void) const
Returns the length of the string in bytes.
Definition: spark_wiring_string.h:208
Header for spark_wiring_printable.cpp module.
String & replace(char find, char replace)
Replaces every occurrence of a character in the string with another character, modifying it in place...
Header for spark_wiring_print.c module.
static String format(const char *format,...)
Uses sprintf-style formatting to build a String object [static].
unsigned char operator!=(const String &rhs) const
Returns true if this string is greater than to another string (case-sensitive)
Definition: spark_wiring_string.h:610
const char * c_str() const
Returns a c-string (null-terminated)
Definition: spark_wiring_string.h:819
unsigned char equals(const String &s) const
Returns true if this string is equal to another string (case-sensitive)
unsigned char operator<=(const String &rhs) const
Returns true if this string is less than or equal to another string (case-sensitive) ...
~String(void)
Destructor. Also deletes the underlying dynamically allocated string.
unsigned int len
The String length (not counting the null terminator).
Definition: spark_wiring_string.h:1060
unsigned char endsWith(const String &suffix) const
Returns true if this string ends with suffix (case-sensitive)
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
Copy the data out of this String into another buffer.
Definition: spark_wiring_string.h:792
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const
Copy the data out of this String into another buffer.
Wiring String: A class to hold and manipulate a dynamically allocated string.
Definition: spark_wiring_string.h:54
String & operator+=(const String &rhs)
Appends (concatenate) a String object to the end of this String, modifying this string in place...
Definition: spark_wiring_string.h:352
unsigned char operator>=(const String &rhs) const
Returns true if this string is greater than or equal to another string (case-sensitive) ...