PIDUINO
Loading...
Searching...
No Matches
string.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 * This file is part of the Piduino Library.
3 *
4 * The Piduino Library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
8 *
9 * The Piduino Library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <string.h>
20
29extern size_t strlcpy (char * dest, const char * src, size_t siz);
30
41extern size_t strlcat (char *dest, const char *src, size_t siz);
42
43#ifdef __cplusplus
44#include <string>
45#include <algorithm>
46
53namespace Piduino {
54
55 class String : public std::string {
56
57 public:
58 // Constructors
59 String() : std::string() {}
60 String (const String & s, size_t pos, size_t len = std::string::npos) : std::string (s, pos, len) {}
61 String (const char * s) : std::string (s) {}
62 String (const char * s, size_t n) : std::string (s, n) {}
63 String (size_t n, char c) : std::string (n, c) {}
64 String (const std::string &s) : std::string (s) {}
65 String (const std::string & s, size_t pos, size_t len = std::string::npos) : std::string (s, pos, len) {}
66
67 explicit String (unsigned char n, unsigned char base = 10) : String (toString (n, base)) {}
68 explicit String (int n, unsigned char base = 10) : String (toString (n, base)) {}
69 explicit String (unsigned int n, unsigned char base = 10) : String (toString (n, base)) {}
70 explicit String (long n, unsigned char base = 10) : String (toString (n, base)) {}
71 explicit String (unsigned long n, unsigned char base = 10) : String (toString (n, base)) {}
72 //------------------------------------------------------------------------
73 explicit String (char c) : String() {
74
75 std::string::push_back (c);
76 }
77
78 // Capacity
79 inline void reserve (unsigned int size);
80
81 // Modifiers
82
83 void reverse();
84 void replace (const String& str1, const String& str2);
85 inline void replace (char c1, char c2);
86 void toLowerCase ();
87 void toUpperCase ();
88
89 inline String & operator += (const String& str);
90 inline String & operator += (const char * str);
91 inline String & operator += (char c);
92 inline String & operator += (unsigned char num);
93 inline String & operator += (int num);
94 inline String & operator += (unsigned int num);
95 inline String & operator += (long num);
96 inline String & operator += (unsigned long num);
97
98 inline void concat (const String &str);
99 inline void concat (const char *cstr);
100 inline void concat (char c);
101 inline void concat (unsigned char num);
102 inline void concat (int num);
103 inline void concat (unsigned int num);
104 inline void concat (long num);
105 inline void concat (unsigned long num);
106 inline void ltrim ();
107 inline void rtrim ();
108 inline void trim ();
109
110 // Relationnals/Conditionnals
111
112 bool equalsIgnoreCase (const String &s) const;
113 inline int compareTo (const String &s) const;
114 inline bool equals (const String &s) const;
115 inline bool equals (const char *cstr) const;
116 inline bool startsWith (const String &prefix, unsigned int offset = 0) const;
117 inline bool startsWith (const std::string &prefix, unsigned int offset = 0) const;
118 inline bool startsWith (char prefix, unsigned int offset = 0) const;
119 inline bool startsWith (const char * prefix, unsigned int offset = 0) const;
120 inline bool endsWith (const String &suffix) const;
121 inline bool endsWith (const std::string &suffix) const;
122 inline bool endsWith (char suffix) const;
123 inline bool endsWith (const char * suffix) const;
124
125 // Element access
126
127 void getBytes (unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
128 inline char charAt (unsigned int index) const;
129 inline void setCharAt (unsigned int index, char c);
130 inline void toCharArray (char *buf, unsigned int bufsize, unsigned int index = 0) const;
131 inline int indexOf (char ch, unsigned int fromIndex = 0) const;
132 inline int indexOf (const char * str, unsigned int fromIndex = 0) const;
133 inline int indexOf (const String &str, unsigned int fromIndex = 0) const;
134 inline int lastIndexOf (char ch, unsigned int fromIndex = -1) const;
135 inline int lastIndexOf (const char * str, unsigned int fromIndex = -1) const;
136 inline int lastIndexOf (const String &str, unsigned int fromIndex = -1) const;
137 inline String substring (unsigned int beginIndex) const;
138 inline String substring (unsigned int beginIndex, unsigned int endIndex) const;
139 long toInt () const;
140
141 static String toString (long num, unsigned char base = 10);
142 static String toString (unsigned long num, unsigned char base = 10);
143 static inline String toString (int num, unsigned char base = 10);
144 static inline String toString (short num, unsigned char base = 10);
145 static inline String toString (unsigned int num, unsigned char base = 10);
146 static inline String toString (unsigned short num, unsigned char base = 10);
147 static inline String toString (unsigned char num, unsigned char base = 10);
148 };
149
150 //------------------------------------------------------------------------
151 inline String & String::operator += (const String& str) {
152
153 concat (str);
154 return (*this);
155 }
156 //------------------------------------------------------------------------
157 inline String & String::operator += (const char * str) {
158
159 concat (str);
160 return (*this);
161 }
162 //------------------------------------------------------------------------
163 inline String & String::operator += (char c) {
164
165 concat (c);
166 return (*this);
167 }
168 //------------------------------------------------------------------------
169 inline String & String::operator += (unsigned char num) {
170
171 concat (num);
172 return (*this);
173 }
174 //------------------------------------------------------------------------
175 inline String & String::operator += (int num) {
176
177 concat (num);
178 return (*this);
179 }
180 //------------------------------------------------------------------------
181 inline String & String::operator += (unsigned int num) {
182
183 concat (num);
184 return (*this);
185 }
186 //------------------------------------------------------------------------
187 inline String & String::operator += (long num) {
188
189 concat (num);
190 return (*this);
191 }
192 //------------------------------------------------------------------------
193 inline String & String::operator += (unsigned long num) {
194
195 concat (num);
196 return (*this);
197 }
198 //------------------------------------------------------------------------
199 inline void String::reserve (unsigned int size) {
200
201 std::string::reserve (size);
202 }
203 //------------------------------------------------------------------------
204 inline void String::replace (char c1, char c2) {
205
206 std::replace (begin(), end(), c1, c2);
207 }
208 //------------------------------------------------------------------------
209 inline void String::concat (const String &str) {
210
211 std::string::append (str);
212 }
213 //------------------------------------------------------------------------
214 inline void String::concat (const char *cstr) {
215
216 std::string::append (cstr);
217 }
218 //------------------------------------------------------------------------
219 inline void String::concat (char c) {
220
221 std::string::push_back (c);
222 }
223 //------------------------------------------------------------------------
224 inline void String::concat (unsigned char num) {
225
226 std::string::append (toString (num));
227 }
228 //------------------------------------------------------------------------
229 inline void String::concat (int num) {
230
231 std::string::append (toString (num));
232 }
233 //------------------------------------------------------------------------
234 inline void String::concat (unsigned int num) {
235
236 std::string::append (toString (num));
237 }
238 //------------------------------------------------------------------------
239 inline void String::concat (long num) {
240
241 std::string::append (toString (num));
242 }
243 //------------------------------------------------------------------------
244 inline void String::concat (unsigned long num) {
245
246 std::string::append (toString (num));
247 }
248 //------------------------------------------------------------------------
249 inline void String::ltrim () {
250
251 std::string::erase (0, find_first_not_of (std::string ("\t\n\v\f\r ")));
252 }
253 //------------------------------------------------------------------------
254 inline void String::rtrim () {
255
256 std::string::erase (find_last_not_of (std::string ("\t\n\v\f\r ")) + 1);
257 }
258 //------------------------------------------------------------------------
259 inline void String::trim () {
260
261 ltrim ();
262 rtrim();
263 }
264 //------------------------------------------------------------------------
265 inline int String::compareTo (const String &s) const {
266
267 return std::string::compare (s);
268 }
269 //------------------------------------------------------------------------
270 inline bool String::equals (const String &s) const {
271
272 return reinterpret_cast<const std::string &> (s) == reinterpret_cast<const std::string &> (*this);
273 }
274 //------------------------------------------------------------------------
275 inline bool String::equals (const char *cstr) const {
276
277 return cstr == (*this);
278 }
279 //------------------------------------------------------------------------
280 inline bool String::startsWith (const String &prefix, unsigned int offset) const {
281
282 return std::string::find (prefix, offset) == 0;
283 }
284 //------------------------------------------------------------------------
285 inline bool String::startsWith (const std::string &prefix, unsigned int offset) const {
286
287 return std::string::find (prefix, offset) == 0;
288 }
289
290 //------------------------------------------------------------------------
291 inline bool String::startsWith (char prefix, unsigned int offset) const {
292
293 return std::string::find (prefix, offset) == 0;
294 }
295
296 //------------------------------------------------------------------------
297 inline bool String::startsWith (const char * prefix, unsigned int offset) const {
298
299 return std::string::find (prefix, offset) == 0;
300 }
301
302 //------------------------------------------------------------------------
303 inline bool String::endsWith (const String &suffix) const {
304
305 return std::string::rfind (suffix) == (length() - suffix.length());
306 }
307
308 //------------------------------------------------------------------------
309 inline bool String::endsWith (const std::string &suffix) const {
310
311 return std::string::rfind (suffix) == (length() - suffix.length());
312 }
313
314 //------------------------------------------------------------------------
315 inline bool String::endsWith (char suffix) const {
316
317 return std::string::rfind (suffix) == (length() - 1);
318 }
319
320 //------------------------------------------------------------------------
321 inline bool String::endsWith (const char * suffix) const {
322
323 return std::string::rfind (suffix) == (length() - strlen (suffix));
324 }
325
326
327 //------------------------------------------------------------------------
328 inline char String::charAt (unsigned int index) const {
329
330 return at (index);
331 }
332
333 //------------------------------------------------------------------------
334 inline void String::setCharAt (unsigned int index, char c) {
335
336 std::string::replace (index, 1, &c);
337 }
338
339 //------------------------------------------------------------------------
340 inline void String::toCharArray (char *buf, unsigned int bufsize, unsigned int index) const {
341
342 getBytes ( (unsigned char *) buf, bufsize, index);
343 }
344
345 //------------------------------------------------------------------------
346 inline int String::indexOf (char ch, unsigned int fromIndex) const {
347
348 return std::string::find (ch, fromIndex);
349 }
350
351 //------------------------------------------------------------------------
352 inline int String::indexOf (const char * str, unsigned int fromIndex) const {
353
354 return std::string::find (str, fromIndex);
355 }
356
357 //------------------------------------------------------------------------
358 inline int String::indexOf (const String &str, unsigned int fromIndex) const {
359
360 return std::string::find (str, fromIndex);
361 }
362
363 //------------------------------------------------------------------------
364 inline int String::lastIndexOf (char ch, unsigned int fromIndex) const {
365
366 return std::string::rfind (ch, (fromIndex == -1 ? std::string::npos : fromIndex));
367 }
368
369 //------------------------------------------------------------------------
370 inline int String::lastIndexOf (const String &str, unsigned int fromIndex) const {
371
372 return std::string::rfind (str, (fromIndex == -1 ? std::string::npos : fromIndex));
373 }
374
375 //------------------------------------------------------------------------
376 inline int String::lastIndexOf (const char * str, unsigned int fromIndex) const {
377
378 return std::string::rfind (str, (fromIndex == -1 ? std::string::npos : fromIndex));
379 }
380
381 //------------------------------------------------------------------------
382 inline String String::substring (unsigned int beginIndex) const {
383
384 return substr (beginIndex);
385 }
386
387 //------------------------------------------------------------------------
388 inline String String::substring (unsigned int beginIndex, unsigned int endIndex) const {
389
390 return substr (beginIndex, endIndex - beginIndex);
391 }
392
393 //------------------------------------------------------------------------
394 inline String String::toString (int num, unsigned char base) {
395
396 return toString (static_cast<long> (num), base);
397 }
398
399 //------------------------------------------------------------------------
400 inline String String::toString (short num, unsigned char base) {
401
402 return toString (static_cast<long> (num), base);
403 }
404
405 //------------------------------------------------------------------------
406 inline String String::toString (unsigned int num, unsigned char base) {
407
408 return toString (static_cast<unsigned long> (num), base);
409 }
410
411 //------------------------------------------------------------------------
412 inline String String::toString (unsigned short num, unsigned char base) {
413
414 return toString (static_cast<unsigned long> (num), base);
415 }
416
417 //------------------------------------------------------------------------
418 inline String String::toString (unsigned char num, unsigned char base) {
419
420 return toString (static_cast<unsigned long> (num), base);
421 }
422}
423#endif /* __cplusplus defined */
424
429/* ========================================================================== */
Global namespace for Piduino.
Definition board.h:28
RingBuffer< T, A >::iterator end(RingBuffer< T, A > &cb)
Definition ringbuffer.h:832
RingBuffer< T, A >::iterator begin(RingBuffer< T, A > &cb)
Definition ringbuffer.h:821
STL namespace.