Arduino Core for STM32  1.0
WString.h
Go to the documentation of this file.
1 /*
2  WString.h - String library for Wiring & Arduino
3 
4  ...mostly rewritten by Paul Stoffregen...
5 
6  Copyright (c) 2009-10 Hernando Barragan. All right reserved.
7 
8  Copyright 2011, Paul Stoffregen, paul@pjrc.com
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24 
25 #ifndef String_class_h
26 #define String_class_h
27 #ifdef __cplusplus
28 
29 
30 #include <string.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include "ftoa.h"
34 
35 // An inherited class for holding the result of a concatenation. These
36 // result objects are assumed to be writable by subsequent concatenations.
37 class StringSumHelper;
38 
39 // The string class
40 class String
41 {
42  // use a function pointer to allow for "if (s)" without the
43  // complications of an operator bool(). for more information, see:
44  // http://www.artima.com/cppsource/safebool.html
45  typedef void (String::*StringIfHelperType)() const;
46  void StringIfHelper() const {}
47 
48 public:
49  // constructors
50  // creates a copy of the initial value.
51  // if the initial value is null or invalid, or if memory allocation
52  // fails, the string will be marked as invalid (i.e. "if (s)" will
53  // be false).
54  String(const char *cstr = "");
55  String(const String &str);
56  #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
57  String(String &&rval);
58  String(StringSumHelper &&rval);
59  #endif
60  explicit String(char c);
61  explicit String(unsigned char, unsigned char base=10);
62  explicit String(int, unsigned char base=10);
63  explicit String(unsigned int, unsigned char base=10);
64  explicit String(long, unsigned char base=10);
65  explicit String(unsigned long, unsigned char base=10);
66  explicit String(float, unsigned char decimalPlaces=2);
67  explicit String(double, unsigned char decimalPlaces=2);
68  ~String(void);
69 
70  // memory management
71  // return true on success, false on failure (in which case, the string
72  // is left unchanged). reserve(0), if successful, will validate an
73  // invalid string (i.e., "if (s)" will be true afterwards)
74  unsigned char reserve(unsigned int size);
75  inline unsigned int length(void) const {return len;}
76 
77  // creates a copy of the assigned value. if the value is null or
78  // invalid, or if the memory allocation fails, the string will be
79  // marked as invalid ("if (s)" will be false).
80  String & operator = (const String &rhs);
81  String & operator = (const char *cstr);
82  #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
83  String & operator = (String &&rval);
84  String & operator = (StringSumHelper &&rval);
85  #endif
86 
87  // concatenate (works w/ built-in types)
88 
89  // returns true on success, false on failure (in which case, the string
90  // is left unchanged). if the argument is null or invalid, the
91  // concatenation is considered unsucessful.
92  unsigned char concat(const String &str);
93  unsigned char concat(const char *cstr);
94  unsigned char concat(char c);
95  unsigned char concat(unsigned char c);
96  unsigned char concat(int num);
97  unsigned char concat(unsigned int num);
98  unsigned char concat(long num);
99  unsigned char concat(unsigned long num);
100  unsigned char concat(float num);
101  unsigned char concat(double num);
102 
103  // if there's not enough memory for the concatenated value, the string
104  // will be left unchanged (but this isn't signalled in any way)
105  String & operator += (const String &rhs) {concat(rhs); return (*this);}
106  String & operator += (const char *cstr) {concat(cstr); return (*this);}
107  String & operator += (char c) {concat(c); return (*this);}
108  String & operator += (unsigned char num) {concat(num); return (*this);}
109  String & operator += (int num) {concat(num); return (*this);}
110  String & operator += (unsigned int num) {concat(num); return (*this);}
111  String & operator += (long num) {concat(num); return (*this);}
112  String & operator += (unsigned long num) {concat(num); return (*this);}
113  String & operator += (float num) {concat(num); return (*this);}
114  String & operator += (double num) {concat(num); return (*this);}
115 
116  friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
117  friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
118  friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
119  friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
120  friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
121  friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
122  friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
123  friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
124  friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
125  friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
126 
127  // comparison (only works w/ Strings and "strings")
128  operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
129  int compareTo(const String &s) const;
130  unsigned char equals(const String &s) const;
131  unsigned char equals(const char *cstr) const;
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 String &rhs) const {return !equals(rhs);}
135  unsigned char operator != (const char *cstr) const {return !equals(cstr);}
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 operator >= (const String &rhs) const;
140  unsigned char equalsIgnoreCase(const String &s) const;
141  unsigned char startsWith( const String &prefix) const;
142  unsigned char startsWith(const String &prefix, unsigned int offset) const;
143  unsigned char endsWith(const String &suffix) const;
144 
145  // character acccess
146  char charAt(unsigned int index) const;
147  void setCharAt(unsigned int index, char c);
148  char operator [] (unsigned int index) const;
149  char& operator [] (unsigned int index);
150  void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
151  void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
152  { getBytes((unsigned char *)buf, bufsize, index); }
153  const char* c_str() const { return buffer; }
154  char* begin() { return buffer; }
155  char* end() { return buffer + length(); }
156  const char* begin() const { return c_str(); }
157  const char* end() const { return c_str() + length(); }
158 
159  // search
160  int indexOf( char ch ) const;
161  int indexOf( char ch, unsigned int fromIndex ) const;
162  int indexOf( const String &str ) const;
163  int indexOf( const String &str, unsigned int fromIndex ) const;
164  int lastIndexOf( char ch ) const;
165  int lastIndexOf( char ch, unsigned int fromIndex ) const;
166  int lastIndexOf( const String &str ) const;
167  int lastIndexOf( const String &str, unsigned int fromIndex ) const;
168  String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
169  String substring( unsigned int beginIndex, unsigned int endIndex ) const;
170 
171  // modification
172  void replace(char find, char replace);
173  void replace(const String& find, const String& replace);
174  void remove(unsigned int index);
175  void remove(unsigned int index, unsigned int count);
176  void toLowerCase(void);
177  void toUpperCase(void);
178  void trim(void);
179 
180  // parsing/conversion
181  long toInt(void) const;
182  float toFloat(void) const;
183  double toDouble(void) const;
184 
185 protected:
186  char *buffer; // the actual char array
187  unsigned int capacity; // the array length minus one (for the '\0')
188  unsigned int len; // the String length (not counting the '\0')
189 protected:
190  void init(void);
191  void invalidate(void);
192  unsigned char changeBuffer(unsigned int maxStrLen);
193  unsigned char concat(const char *cstr, unsigned int length);
194 
195  // copy and move
196  String & copy(const char *cstr, unsigned int length);
197  #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
198  void move(String &rhs);
199  #endif
200 };
201 
202 class StringSumHelper : public String
203 {
204 public:
205  StringSumHelper(const String &s) : String(s) {}
206  StringSumHelper(const char *p) : String(p) {}
207  StringSumHelper(char c) : String(c) {}
208  StringSumHelper(unsigned char num) : String(num) {}
209  StringSumHelper(int num) : String(num) {}
210  StringSumHelper(unsigned int num) : String(num) {}
211  StringSumHelper(long num) : String(num) {}
212  StringSumHelper(unsigned long num) : String(num) {}
213  StringSumHelper(float num) : String(num) {}
214  StringSumHelper(double num) : String(num) {}
215 };
216 
217 #endif // __cplusplus
218 #endif // String_class_h
operator+
StringSumHelper & operator+(const StringSumHelper &lhs, const String &rhs)
Definition: WString.cpp:317
ftoa.h
Library that contain function, that can convert floating point number to array.