JM9 XRCU Board 0.1.1
Libraries API Reference
Loading...
Searching...
No Matches
WString.h
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
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#ifndef String_class_h
23#define String_class_h
24#ifdef __cplusplus
25
26#include <stdlib.h>
27#include <string.h>
28#include <ctype.h>
29#include <avr/pgmspace.h>
30
31// When compiling programs with this class, the following gcc parameters
32// dramatically increase performance and memory (RAM) efficiency, typically
33// with little or no increase in code size.
34// -felide-constructors
35// -std=c++0x
36
37class __FlashStringHelper;
38#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
39
40// An inherited class for holding the result of a concatenation. These
41// result objects are assumed to be writable by subsequent concatenations.
42class StringSumHelper;
43
44// The string class
45class String {
46 // use a function pointer to allow for "if (s)" without the
47 // complications of an operator bool(). for more information, see:
48 // http://www.artima.com/cppsource/safebool.html
49 typedef void (String::*StringIfHelperType)() const;
50 void StringIfHelper() const {}
51
52 public:
53 // constructors
54 // creates a copy of the initial value.
55 // if the initial value is null or invalid, or if memory allocation
56 // fails, the string will be marked as invalid (i.e. "if (s)" will
57 // be false).
58 String(const char *cstr = "");
59 String(const String &str);
60 String(const __FlashStringHelper *str);
61#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
62 String(String &&rval);
63 String(StringSumHelper &&rval);
64#endif
65 explicit String(char c);
66 explicit String(unsigned char, unsigned char base = 10);
67 explicit String(int, unsigned char base = 10);
68 explicit String(unsigned int, unsigned char base = 10);
69 explicit String(long, unsigned char base = 10);
70 explicit String(unsigned long, unsigned char base = 10);
71 explicit String(float, unsigned char decimalPlaces = 2);
72 explicit String(double, unsigned char decimalPlaces = 2);
73 ~String(void);
74
75 // memory management
76 // return true on success, false on failure (in which case, the string
77 // is left unchanged). reserve(0), if successful, will validate an
78 // invalid string (i.e., "if (s)" will be true afterwards)
79 unsigned char reserve(unsigned int size);
80 inline unsigned int length(void) const
81 {
82 return len;
83 }
84
85 // creates a copy of the assigned value. if the value is null or
86 // invalid, or if the memory allocation fails, the string will be
87 // marked as invalid ("if (s)" will be false).
88 String &operator = (const String &rhs);
89 String &operator = (const char *cstr);
90 String &operator = (const __FlashStringHelper *str);
91#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
92 String &operator = (String &&rval);
93 String &operator = (StringSumHelper &&rval);
94#endif
95
96 // concatenate (works w/ built-in types)
97
98 // returns true on success, false on failure (in which case, the string
99 // is left unchanged). if the argument is null or invalid, the
100 // concatenation is considered unsuccessful.
101 unsigned char concat(const String &str);
102 unsigned char concat(const char *cstr);
103 unsigned char concat(char c);
104 unsigned char concat(unsigned char c);
105 unsigned char concat(int num);
106 unsigned char concat(unsigned int num);
107 unsigned char concat(long num);
108 unsigned char concat(unsigned long num);
109 unsigned char concat(float num);
110 unsigned char concat(double num);
111 unsigned char concat(const __FlashStringHelper *str);
112
113 // if there's not enough memory for the concatenated value, the string
114 // will be left unchanged (but this isn't signalled in any way)
115 String &operator += (const String &rhs)
116 {
117 concat(rhs);
118 return (*this);
119 }
120 String &operator += (const char *cstr)
121 {
122 concat(cstr);
123 return (*this);
124 }
125 String &operator += (char c)
126 {
127 concat(c);
128 return (*this);
129 }
130 String &operator += (unsigned char num)
131 {
132 concat(num);
133 return (*this);
134 }
135 String &operator += (int num)
136 {
137 concat(num);
138 return (*this);
139 }
140 String &operator += (unsigned int num)
141 {
142 concat(num);
143 return (*this);
144 }
145 String &operator += (long num)
146 {
147 concat(num);
148 return (*this);
149 }
150 String &operator += (unsigned long num)
151 {
152 concat(num);
153 return (*this);
154 }
155 String &operator += (float num)
156 {
157 concat(num);
158 return (*this);
159 }
160 String &operator += (double num)
161 {
162 concat(num);
163 return (*this);
164 }
165 String &operator += (const __FlashStringHelper *str)
166 {
167 concat(str);
168 return (*this);
169 }
170
171 friend StringSumHelper &operator + (const StringSumHelper &lhs, const String &rhs);
172 friend StringSumHelper &operator + (const StringSumHelper &lhs, const char *cstr);
173 friend StringSumHelper &operator + (const StringSumHelper &lhs, char c);
174 friend StringSumHelper &operator + (const StringSumHelper &lhs, unsigned char num);
175 friend StringSumHelper &operator + (const StringSumHelper &lhs, int num);
176 friend StringSumHelper &operator + (const StringSumHelper &lhs, unsigned int num);
177 friend StringSumHelper &operator + (const StringSumHelper &lhs, long num);
178 friend StringSumHelper &operator + (const StringSumHelper &lhs, unsigned long num);
179 friend StringSumHelper &operator + (const StringSumHelper &lhs, float num);
180 friend StringSumHelper &operator + (const StringSumHelper &lhs, double num);
181 friend StringSumHelper &operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
182
183 // comparison (only works w/ Strings and "strings")
184 operator StringIfHelperType() const
185 {
186 return buffer ? &String::StringIfHelper : 0;
187 }
188 int compareTo(const String &s) const;
189 unsigned char equals(const String &s) const;
190 unsigned char equals(const char *cstr) const;
191 unsigned char operator == (const String &rhs) const
192 {
193 return equals(rhs);
194 }
195 unsigned char operator == (const char *cstr) const
196 {
197 return equals(cstr);
198 }
199 unsigned char operator != (const String &rhs) const
200 {
201 return !equals(rhs);
202 }
203 unsigned char operator != (const char *cstr) const
204 {
205 return !equals(cstr);
206 }
207 unsigned char operator < (const String &rhs) const;
208 unsigned char operator > (const String &rhs) const;
209 unsigned char operator <= (const String &rhs) const;
210 unsigned char operator >= (const String &rhs) const;
211 unsigned char equalsIgnoreCase(const String &s) const;
212 unsigned char startsWith(const String &prefix) const;
213 unsigned char startsWith(const String &prefix, unsigned int offset) const;
214 unsigned char endsWith(const String &suffix) const;
215
216 // character access
217 char charAt(unsigned int index) const;
218 void setCharAt(unsigned int index, char c);
219 char operator [](unsigned int index) const;
220 char &operator [](unsigned int index);
221 void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
222 void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const
223 {
224 getBytes((unsigned char *)buf, bufsize, index);
225 }
226 const char *c_str() const
227 {
228 return buffer;
229 }
230 char *begin()
231 {
232 return buffer;
233 }
234 char *end()
235 {
236 return buffer + length();
237 }
238 const char *begin() const
239 {
240 return c_str();
241 }
242 const char *end() const
243 {
244 return c_str() + length();
245 }
246
247 // search
248 int indexOf(char ch) const;
249 int indexOf(char ch, unsigned int fromIndex) const;
250 int indexOf(const String &str) const;
251 int indexOf(const String &str, unsigned int fromIndex) const;
252 int lastIndexOf(char ch) const;
253 int lastIndexOf(char ch, unsigned int fromIndex) const;
254 int lastIndexOf(const String &str) const;
255 int lastIndexOf(const String &str, unsigned int fromIndex) const;
256 String substring(unsigned int beginIndex) const
257 {
258 return substring(beginIndex, len);
259 };
260 String substring(unsigned int beginIndex, unsigned int endIndex) const;
261
262 // modification
263 void replace(char find, char replace);
264 void replace(const String &find, const String &replace);
265 void remove(unsigned int index);
266 void remove(unsigned int index, unsigned int count);
267 void toLowerCase(void);
268 void toUpperCase(void);
269 void trim(void);
270
271 // parsing/conversion
272 long toInt(void) const;
273 float toFloat(void) const;
274 double toDouble(void) const;
275
276 protected:
277 char *buffer; // the actual char array
278 unsigned int capacity; // the array length minus one (for the '\0')
279 unsigned int len; // the String length (not counting the '\0')
280 protected:
281 void init(void);
282 void invalidate(void);
283 unsigned char changeBuffer(unsigned int maxStrLen);
284 unsigned char concat(const char *cstr, unsigned int length);
285
286 // copy and move
287 String &copy(const char *cstr, unsigned int length);
288 String &copy(const __FlashStringHelper *pstr, unsigned int length);
289#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
290 void move(String &rhs);
291#endif
292};
293
294class StringSumHelper : public String {
295 public:
296 StringSumHelper(const String &s) : String(s) {}
297 StringSumHelper(const char *p) : String(p) {}
298 StringSumHelper(char c) : String(c) {}
299 StringSumHelper(unsigned char num) : String(num) {}
300 StringSumHelper(int num) : String(num) {}
301 StringSumHelper(unsigned int num) : String(num) {}
302 StringSumHelper(long num) : String(num) {}
303 StringSumHelper(unsigned long num) : String(num) {}
304 StringSumHelper(float num) : String(num) {}
305 StringSumHelper(double num) : String(num) {}
306};
307
308#endif // __cplusplus
309#endif // String_class_h