CStringArray.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * CStringArray.h
8  *
9  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "WString.h"
16 #include "stringutil.h"
17 
21 class CStringArray : private String
22 {
23 public:
24  // Inherit any safe/useful methods
25  using String::compareTo;
26  using String::equals;
27  using String::reserve;
28  using String::operator StringIfHelperType;
29  using String::operator==;
30  using String::operator!=;
31  using String::operator<;
32  using String::operator>;
33  using String::operator<=;
34  using String::operator>=;
35  using String::c_str;
36  using String::endsWith;
38  using String::getBytes;
39  using String::length;
40  using String::startsWith;
41  using String::toCharArray;
42  using String::toLowerCase;
43  using String::toUpperCase;
44 
49  CStringArray(const String& str) : String(str)
50  {
51  init();
52  }
53 
54  CStringArray(const char* cstr = nullptr) : String(cstr)
55  {
56  init();
57  }
58 
59  CStringArray(const char* cstr, unsigned int length) : String(cstr, length)
60  {
61  init();
62  }
63 
64  explicit CStringArray(flash_string_t pstr, int length = -1) : String(pstr, length)
65  {
66  init();
67  }
68 
69  CStringArray(const FlashString& fstr) : String(fstr)
70  {
71  init();
72  }
73 
74 #ifdef __GXX_EXPERIMENTAL_CXX0X__
75  CStringArray(String&& rval) noexcept : String(std::move(rval))
76  {
77  init();
78  }
79  CStringArray(StringSumHelper&& rval) noexcept : String(std::move(rval))
80  {
81  init();
82  }
83 #endif
84 
86  CStringArray& operator=(const char* cstr)
87  {
88  String::operator=(cstr);
89  init();
90  return *this;
91  }
92 
99  bool add(const char* str, int length = -1);
100 
106  bool add(const String& str)
107  {
108  return add(str.c_str(), str.length());
109  }
110 
116  {
117  add(str);
118  return *this;
119  }
120 
121  CStringArray& operator+=(const char* cstr)
122  {
123  add(cstr);
124  return *this;
125  }
126 
131  template <typename T> CStringArray& operator+=(T value)
132  {
133  add(String(value));
134  return *this;
135  }
144  int indexOf(const char* str, bool ignoreCase = true) const;
145 
152  int indexOf(const String& str, bool ignoreCase = true) const
153  {
154  return indexOf(str.c_str(), ignoreCase);
155  }
156 
163  bool contains(const char* str, bool ignoreCase = true) const
164  {
165  return indexOf(str, ignoreCase) >= 0;
166  }
167 
174  bool contains(const String& str, bool ignoreCase = true) const
175  {
176  return indexOf(str, ignoreCase) >= 0;
177  }
178 
183  const char* getValue(unsigned index) const;
184 
189  const char* operator[](unsigned index) const
190  {
191  return getValue(index);
192  }
193 
197  const char* front() const
198  {
199  return cbuffer();
200  }
201 
207  bool pushFront(const char* str);
208 
213  String popFront();
214 
218  const char* back() const;
219 
225  bool pushBack(const char* str)
226  {
227  return add(str);
228  }
229 
234  String popBack();
235 
238  void clear()
239  {
240  *this = nullptr;
241  stringCount = 0;
242  }
243 
247  unsigned count() const;
248 
253  class Iterator
254  {
255  public:
256  Iterator() = default;
257  Iterator(const Iterator&) = default;
258 
260  : mArray(array), mOffset(offset), mIndex(index)
261  {
262  }
263 
264  operator bool() const
265  {
266  return mArray != nullptr && mOffset < mArray->length();
267  }
268 
269  bool equals(const char* rhs) const
270  {
271  auto s = str();
272  return s == rhs || strcmp(s, rhs) == 0;
273  }
274 
275  bool equalsIgnoreCase(const char* rhs) const
276  {
277  auto s = str();
278  return s == rhs || strcasecmp(str(), rhs) == 0;
279  }
280 
281  bool operator==(const Iterator& rhs) const
282  {
283  return mArray == rhs.mArray && mOffset == rhs.mOffset;
284  }
285 
286  bool operator!=(const Iterator& rhs) const
287  {
288  return !operator==(rhs);
289  }
290 
291  bool operator==(const char* rhs) const
292  {
293  return equals(rhs);
294  }
295 
296  bool operator!=(const char* rhs) const
297  {
298  return !equals(rhs);
299  }
300 
301  bool equals(const String& rhs) const
302  {
303  return rhs.equals(str());
304  }
305 
306  bool equalsIgnoreCase(const String& rhs) const
307  {
308  return rhs.equalsIgnoreCase(str());
309  }
310 
311  bool operator==(const String& rhs) const
312  {
313  return equals(rhs);
314  }
315 
316  bool operator!=(const String& rhs) const
317  {
318  return !equals(rhs);
319  }
320 
321  const char* str() const
322  {
323  if(mArray == nullptr) {
324  return "";
325  }
326 
327  return mArray->c_str() + mOffset;
328  }
329 
330  const char* operator*() const
331  {
332  return str();
333  }
334 
335  uint16_t index() const
336  {
337  return mIndex;
338  }
339 
340  uint16_t offset() const
341  {
342  return mOffset;
343  }
344 
346  {
347  next();
348  return *this;
349  }
350 
352  {
353  Iterator tmp(*this);
354  next();
355  return tmp;
356  }
357 
358  void next()
359  {
360  if(*this) {
361  mOffset += strlen(str()) + 1;
362  ++mIndex;
363  }
364  }
365 
367 
368  private:
369  const CStringArray* mArray = nullptr;
370  uint16_t mOffset = 0;
371  uint16_t mIndex = 0;
372  };
373 
374  Iterator begin() const
375  {
376  return Iterator(this, 0, 0);
377  }
378 
379  Iterator end() const
380  {
381  return Iterator(this, length(), count());
382  }
383 
386 private:
387  void init();
388 
389 private:
390  mutable unsigned stringCount = 0;
391 };
void next()
Definition: CStringArray.h:358
CStringArray & operator+=(const char *cstr)
Definition: CStringArray.h:121
bool equals(const String &rhs) const
Definition: CStringArray.h:301
int indexOf(const char *str, bool ignoreCase=true) const
Find the given string and return its index.
CStringArray(flash_string_t pstr, int length=-1)
Definition: CStringArray.h:64
bool startsWith(const String &prefix) const
Compare the start of a String Comparison is case-sensitive, must match exactly.
Definition: WString.h:531
CStringArray(const FlashString &fstr)
Definition: CStringArray.h:69
bool operator==(const char *rhs) const
Definition: CStringArray.h:291
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
size_t getBytes(unsigned char *buf, size_t bufsize, size_t index=0) const
Read contents of a String into a buffer.
void clear()
Empty the array.
Definition: CStringArray.h:238
int strcasecmp(const char *s1, const char *s2)
A case-insensitive strcmp().
#define str(s)
Definition: testrunner.h:124
bool equalsIgnoreCase(const char *rhs) const
Definition: CStringArray.h:275
void toUpperCase(void)
Convert the entire String content to upper case.
Iterator(const CStringArray *array, uint16_t offset, uint16_t index)
Definition: CStringArray.h:259
const char * getValue(unsigned index) const
Get string at the given position.
bool operator!=(const char *rhs) const
Definition: CStringArray.h:296
bool add(const char *str, int length=-1)
Append a new string (or array of strings) to the end of the array.
bool operator==(const String &rhs) const
Definition: CStringArray.h:311
const char * operator*() const
Definition: CStringArray.h:330
const char * operator[](unsigned index) const
Get string at the given position.
Definition: CStringArray.h:189
bool pushBack(const char *str)
Add item to end of array.
Definition: CStringArray.h:225
const char * cbuffer() const
Definition: WString.h:856
The String class.
Definition: WString.h:136
String & operator=(const String &rhs)
unsigned count() const
Get quantity of strings in array.
CStringArray(const String &str)
Definition: CStringArray.h:49
CStringArray(const char *cstr, unsigned int length)
Definition: CStringArray.h:59
Iterator end() const
Definition: CStringArray.h:379
String popBack()
Pop last item from array.
CStringArray & operator+=(T value)
Append numbers, etc. to the array.
Definition: CStringArray.h:131
bool equalsIgnoreCase(const String &rhs) const
Definition: CStringArray.h:306
const __FlashStringHelper * flash_string_t
Provides a strongly-typed pointer to allow safe implicit operation using String class methods...
Definition: WString.h:96
bool operator!=(const String &rhs) const
Definition: CStringArray.h:316
bool equals(const char *rhs) const
Definition: CStringArray.h:269
bool endsWith(char suffix) const
Compare the end of a String.
Definition: CStringArray.h:253
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
bool contains(const String &str, bool ignoreCase=true) const
Check if array contains a string.
Definition: CStringArray.h:174
String popFront()
Pop first item from array (at index 0)
Iterator & operator++()
Definition: CStringArray.h:345
bool add(const String &str)
Append a new string (or array of strings) to the end of the array.
Definition: CStringArray.h:106
String()
Default constructor.
Definition: WString.h:155
describes a counted string stored in flash memory
Definition: String.hpp:173
void toCharArray(char *buf, size_t bufsize, size_t index=0) const
Read contents of String into a buffer.
Definition: WString.h:607
const char * back() const
Get last item in array, null if empty.
Definition: WString.h:891
bool pushFront(const char *str)
Insert item at start of array.
void toLowerCase(void)
Convert the entire String content to lower case.
const char * str() const
Definition: CStringArray.h:321
bool reserve(size_t size)
Pre-allocate String memory.
Iterator operator++(int)
Definition: CStringArray.h:351
CStringArray & operator+=(const String &str)
Definition: CStringArray.h:115
bool equals(const String &s) const
Definition: WString.h:437
uint16_t offset() const
Definition: CStringArray.h:340
bool equalsIgnoreCase(const char *cstr) const
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:21
Iterator begin() const
Definition: CStringArray.h:374
bool operator==(const Iterator &rhs) const
Definition: CStringArray.h:281
int indexOf(const String &str, bool ignoreCase=true) const
Find the given string and return its index.
Definition: CStringArray.h:152
uint16_t index() const
Definition: CStringArray.h:335
const char * front() const
Get first value in array, null if empty.
Definition: CStringArray.h:197
bool operator!=(const Iterator &rhs) const
Definition: CStringArray.h:286
bool contains(const char *str, bool ignoreCase=true) const
Check if array contains a string.
Definition: CStringArray.h:163
CStringArray(const char *cstr=nullptr)
Definition: CStringArray.h:54
int compareTo(const char *cstr, size_t length) const
CStringArray & operator=(const char *cstr)
Definition: CStringArray.h:86