arduino-emulator
Loading...
Searching...
No Matches
String.h
1/*
2 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#ifdef __cplusplus
23
24#ifndef __ARDUINO_STRINGS__
25#define __ARDUINO_STRINGS__
26
27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#if defined(__AVR__)
31#include "avr/pgmspace.h"
32#else
33#include "deprecated-avr-comp/avr/pgmspace.h"
34#endif
35
36namespace arduino {
37
38// When compiling programs with this class, the following gcc parameters
39// dramatically increase performance and memory (RAM) efficiency, typically
40// with little or no increase in code size.
41// -felide-constructors
42// -std=c++0x
43
44class __FlashStringHelper;
45#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
46
47// An inherited class for holding the result of a concatenation. These
48// result objects are assumed to be writable by subsequent concatenations.
49class StringSumHelper;
50
51// The string class
52class String
53{
54 friend class StringSumHelper;
55 // use a function pointer to allow for "if (s)" without the
56 // complications of an operator bool(). for more information, see:
57 // http://www.artima.com/cppsource/safebool.html
58 typedef void (String::*StringIfHelperType)() const;
59 void StringIfHelper() const {}
60
61 static size_t const FLT_MAX_DECIMAL_PLACES = 10;
62 static size_t const DBL_MAX_DECIMAL_PLACES = FLT_MAX_DECIMAL_PLACES;
63
64public:
65 // constructors
66 // creates a copy of the initial value.
67 // if the initial value is null or invalid, or if memory allocation
68 // fails, the string will be marked as invalid (i.e. "if (s)" will
69 // be false).
70 String(const char *cstr = "");
71 String(const char *cstr, unsigned int length);
72 String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {}
73 String(const String &str);
74 String(const __FlashStringHelper *str);
76 explicit String(char c);
77 explicit String(unsigned char, unsigned char base=10);
78 explicit String(int, unsigned char base=10);
79 explicit String(unsigned int, unsigned char base=10);
80 explicit String(long, unsigned char base=10);
81 explicit String(unsigned long, unsigned char base=10);
82 explicit String(float, unsigned char decimalPlaces=2);
83 explicit String(double, unsigned char decimalPlaces=2);
84 ~String(void);
85
86 // memory management
87 // return true on success, false on failure (in which case, the string
88 // is left unchanged). reserve(0), if successful, will validate an
89 // invalid string (i.e., "if (s)" will be true afterwards)
90 bool reserve(unsigned int size);
91 inline unsigned int length(void) const {return len;}
92 inline bool isEmpty(void) const { return length() == 0; }
93
94 // creates a copy of the assigned value. if the value is null or
95 // invalid, or if the memory allocation fails, the string will be
96 // marked as invalid ("if (s)" will be false).
97 String & operator = (const String &rhs);
98 String & operator = (const char *cstr);
99 String & operator = (const __FlashStringHelper *str);
101
102 // concatenate (works w/ built-in types)
103
104 // returns true on success, false on failure (in which case, the string
105 // is left unchanged). if the argument is null or invalid, the
106 // concatenation is considered unsuccessful.
107 bool concat(const String &str);
108 bool concat(const char *cstr);
109 bool concat(const char *cstr, unsigned int length);
110 bool concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
111 bool concat(char c);
112 bool concat(unsigned char num);
113 bool concat(int num);
114 bool concat(unsigned int num);
115 bool concat(long num);
116 bool concat(unsigned long num);
117 bool concat(float num);
118 bool concat(double num);
119 bool concat(const __FlashStringHelper * str);
120
121 // if there's not enough memory for the concatenated value, the string
122 // will be left unchanged (but this isn't signalled in any way)
123 String & operator += (const String &rhs) {concat(rhs); return (*this);}
124 String & operator += (const char *cstr) {concat(cstr); return (*this);}
125 String & operator += (char c) {concat(c); return (*this);}
126 String & operator += (unsigned char num) {concat(num); return (*this);}
127 String & operator += (int num) {concat(num); return (*this);}
128 String & operator += (unsigned int num) {concat(num); return (*this);}
129 String & operator += (long num) {concat(num); return (*this);}
130 String & operator += (unsigned long num) {concat(num); return (*this);}
131 String & operator += (float num) {concat(num); return (*this);}
132 String & operator += (double num) {concat(num); return (*this);}
133 String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
134
135 friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
136 friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
137 friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
138 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
139 friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
140 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
141 friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
142 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
143 friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
144 friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
145 friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
146
147 // comparison (only works w/ Strings and "strings")
148 operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
149 int compareTo(const String &s) const;
150 int compareTo(const char *cstr) const;
151 bool equals(const String &s) const;
152 bool equals(const char *cstr) const;
153
154 friend bool operator == (const String &a, const String &b) { return a.equals(b); }
155 friend bool operator == (const String &a, const char *b) { return a.equals(b); }
156 friend bool operator == (const char *a, const String &b) { return b == a; }
157 friend bool operator < (const String &a, const String &b) { return a.compareTo(b) < 0; }
158 friend bool operator < (const String &a, const char *b) { return a.compareTo(b) < 0; }
159 friend bool operator < (const char *a, const String &b) { return b.compareTo(a) > 0; }
160
161 friend bool operator != (const String &a, const String &b) { return !(a == b); }
162 friend bool operator != (const String &a, const char *b) { return !(a == b); }
163 friend bool operator != (const char *a, const String &b) { return !(a == b); }
164 friend bool operator > (const String &a, const String &b) { return b < a; }
165 friend bool operator > (const String &a, const char *b) { return b < a; }
166 friend bool operator > (const char *a, const String &b) { return b < a; }
167 friend bool operator <= (const String &a, const String &b) { return !(b < a); }
168 friend bool operator <= (const String &a, const char *b) { return !(b < a); }
169 friend bool operator <= (const char *a, const String &b) { return !(b < a); }
170 friend bool operator >= (const String &a, const String &b) { return !(a < b); }
171 friend bool operator >= (const String &a, const char *b) { return !(a < b); }
172 friend bool operator >= (const char *a, const String &b) { return !(a < b); }
173
174 bool equalsIgnoreCase(const String &s) const;
175 bool startsWith( const String &prefix) const;
176 bool startsWith(const String &prefix, unsigned int offset) const;
177 bool endsWith(const String &suffix) const;
178
179 // character access
180 char charAt(unsigned int index) const;
181 void setCharAt(unsigned int index, char c);
182 char operator [] (unsigned int index) const;
183 char& operator [] (unsigned int index);
184 void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
185 void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
186 { getBytes((unsigned char *)buf, bufsize, index); }
187 const char* c_str() const { return buffer; }
188 char* begin() { return buffer; }
189 char* end() { return buffer + length(); }
190 const char* begin() const { return c_str(); }
191 const char* end() const { return c_str() + length(); }
192
193 // search
194 int indexOf( char ch ) const;
195 int indexOf( char ch, unsigned int fromIndex ) const;
196 int indexOf( const String &str ) const;
197 int indexOf( const String &str, unsigned int fromIndex ) const;
198 int lastIndexOf( char ch ) const;
199 int lastIndexOf( char ch, unsigned int fromIndex ) const;
200 int lastIndexOf( const String &str ) const;
201 int lastIndexOf( const String &str, unsigned int fromIndex ) const;
202 String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
203 String substring( unsigned int beginIndex, unsigned int endIndex ) const;
204
205 // modification
206 void replace(char find, char replace);
207 void replace(const String& find, const String& replace);
208 void remove(unsigned int index);
209 void remove(unsigned int index, unsigned int count);
210 void toLowerCase(void);
211 void toUpperCase(void);
212 void trim(void);
213
214 // parsing/conversion
215 long toInt(void) const;
216 float toFloat(void) const;
217 double toDouble(void) const;
218
219protected:
220 char *buffer; // the actual char array
221 unsigned int capacity; // the array length minus one (for the '\0')
222 unsigned int len; // the String length (not counting the '\0')
223protected:
224 void init(void);
225 void invalidate(void);
226 bool changeBuffer(unsigned int maxStrLen);
227
228 // copy and move
229 String & copy(const char *cstr, unsigned int length);
230 String & copy(const __FlashStringHelper *pstr, unsigned int length);
231 void move(String &rhs);
232};
233
235{
236public:
237 StringSumHelper(const String &s) : String(s) {}
238 StringSumHelper(const char *p) : String(p) {}
239 StringSumHelper(char c) : String(c) {}
240 StringSumHelper(unsigned char num) : String(num) {}
241 StringSumHelper(int num) : String(num) {}
242 StringSumHelper(unsigned int num) : String(num) {}
243 StringSumHelper(long num) : String(num) {}
244 StringSumHelper(unsigned long num) : String(num) {}
245 StringSumHelper(float num) : String(num) {}
246 StringSumHelper(double num) : String(num) {}
247};
248
249} // namespace arduino
250
251using arduino::__FlashStringHelper;
252using arduino::String;
253
254#endif // __cplusplus
255#endif // __ARDUINO_STRINGS__
Definition DMAPool.h:103
Definition String.h:53
Definition String.h:235
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31