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 
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
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 
214 
218  const char* back() const;
219 
225  bool pushBack(const char* str)
226  {
227  return add(str);
228  }
229 
235 
238  void clear()
239  {
240  *this = nullptr;
241  stringCount = 0;
242  }
243 
247  unsigned count() const;
248 
256  String join(const String& separator = ",") const;
257 
262  class Iterator
263  {
264  public:
265  Iterator() = default;
266  Iterator(const Iterator&) = default;
267 
268  Iterator(const CStringArray* array, uint16_t offset, uint16_t index)
269  : mArray(array), mOffset(offset), mIndex(index)
270  {
271  }
272 
273  operator bool() const
274  {
275  return mArray != nullptr && mOffset < mArray->length();
276  }
277 
278  bool equals(const char* rhs) const
279  {
280  auto s = str();
281  return s == rhs || strcmp(s, rhs) == 0;
282  }
283 
284  bool equalsIgnoreCase(const char* rhs) const
285  {
286  auto s = str();
287  return s == rhs || strcasecmp(str(), rhs) == 0;
288  }
289 
290  bool operator==(const Iterator& rhs) const
291  {
292  return mArray == rhs.mArray && mOffset == rhs.mOffset;
293  }
294 
295  bool operator!=(const Iterator& rhs) const
296  {
297  return !operator==(rhs);
298  }
299 
300  bool operator==(const char* rhs) const
301  {
302  return equals(rhs);
303  }
304 
305  bool operator!=(const char* rhs) const
306  {
307  return !equals(rhs);
308  }
309 
310  bool equals(const String& rhs) const
311  {
312  return rhs.equals(str());
313  }
314 
315  bool equalsIgnoreCase(const String& rhs) const
316  {
317  return rhs.equalsIgnoreCase(str());
318  }
319 
320  bool operator==(const String& rhs) const
321  {
322  return equals(rhs);
323  }
324 
325  bool operator!=(const String& rhs) const
326  {
327  return !equals(rhs);
328  }
329 
330  const char* str() const
331  {
332  if(mArray == nullptr) {
333  return "";
334  }
335 
336  return mArray->c_str() + mOffset;
337  }
338 
339  const char* operator*() const
340  {
341  return str();
342  }
343 
344  uint16_t index() const
345  {
346  return mIndex;
347  }
348 
349  uint16_t offset() const
350  {
351  return mOffset;
352  }
353 
355  {
356  next();
357  return *this;
358  }
359 
361  {
362  Iterator tmp(*this);
363  next();
364  return tmp;
365  }
366 
367  void next()
368  {
369  if(*this) {
370  mOffset += strlen(str()) + 1;
371  ++mIndex;
372  }
373  }
374 
376 
377  private:
378  const CStringArray* mArray = nullptr;
379  uint16_t mOffset = 0;
380  uint16_t mIndex = 0;
381  };
382 
383  Iterator begin() const
384  {
385  return Iterator(this, 0, 0);
386  }
387 
388  Iterator end() const
389  {
390  return Iterator(this, length(), count());
391  }
392 
395 private:
396  void init();
397 
398 private:
399  mutable unsigned stringCount = 0;
400 };
const __FlashStringHelper * flash_string_t
Provides a strongly-typed pointer to allow safe implicit operation using String class methods.
Definition: WString.h:96
Definition: CStringArray.h:263
bool operator==(const Iterator &rhs) const
Definition: CStringArray.h:290
const char * str() const
Definition: CStringArray.h:330
bool equals(const String &rhs) const
Definition: CStringArray.h:310
bool operator!=(const Iterator &rhs) const
Definition: CStringArray.h:295
void next()
Definition: CStringArray.h:367
bool operator!=(const String &rhs) const
Definition: CStringArray.h:325
bool operator==(const String &rhs) const
Definition: CStringArray.h:320
Iterator & operator++()
Definition: CStringArray.h:354
bool equalsIgnoreCase(const char *rhs) const
Definition: CStringArray.h:284
bool equalsIgnoreCase(const String &rhs) const
Definition: CStringArray.h:315
Iterator(const Iterator &)=default
bool equals(const char *rhs) const
Definition: CStringArray.h:278
const char * operator*() const
Definition: CStringArray.h:339
bool operator!=(const char *rhs) const
Definition: CStringArray.h:305
uint16_t offset() const
Definition: CStringArray.h:349
Iterator(const CStringArray *array, uint16_t offset, uint16_t index)
Definition: CStringArray.h:268
Iterator operator++(int)
Definition: CStringArray.h:360
bool operator==(const char *rhs) const
Definition: CStringArray.h:300
uint16_t index() const
Definition: CStringArray.h:344
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:22
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
Iterator begin() const
Definition: CStringArray.h:383
String join(const String &separator=",") const
Get contents of array as delimited string.
const char * front() const
Get first value in array, null if empty.
Definition: CStringArray.h:197
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
bool contains(const String &str, bool ignoreCase=true) const
Check if array contains a string.
Definition: CStringArray.h:174
CStringArray & operator+=(const char *cstr)
Definition: CStringArray.h:121
CStringArray(const String &str)
Definition: CStringArray.h:49
bool add(const String &str)
Append a new string (or array of strings) to the end of the array.
Definition: CStringArray.h:106
int indexOf(const char *str, bool ignoreCase=true) const
Find the given string and return its index.
CStringArray(const char *cstr, unsigned int length)
Definition: CStringArray.h:59
const char * back() const
Get last item in array, null if empty.
bool pushBack(const char *str)
Add item to end of array.
Definition: CStringArray.h:225
CStringArray(const FlashString &fstr)
Definition: CStringArray.h:69
CStringArray & operator+=(T value)
Append numbers, etc. to the array.
Definition: CStringArray.h:131
bool pushFront(const char *str)
Insert item at start of array.
String popFront()
Pop first item from array (at index 0)
CStringArray & operator+=(const String &str)
Definition: CStringArray.h:115
String popBack()
Pop last item from array.
bool add(const char *str, int length=-1)
Append a new string (or array of strings) to the end of the array.
unsigned count() const
Get quantity of strings in array.
const char * operator[](unsigned index) const
Get string at the given position.
Definition: CStringArray.h:189
int indexOf(const String &str, bool ignoreCase=true) const
Find the given string and return its index.
Definition: CStringArray.h:152
const char * getValue(unsigned index) const
Get string at the given position.
void clear()
Empty the array.
Definition: CStringArray.h:238
CStringArray(flash_string_t pstr, int length=-1)
Definition: CStringArray.h:64
CStringArray & operator=(const char *cstr)
Definition: CStringArray.h:86
Iterator end() const
Definition: CStringArray.h:388
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
describes a counted string stored in flash memory
Definition: String.hpp:174
Definition: WString.h:892
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
bool startsWith(const String &prefix) const
Compare the start of a String Comparison is case-sensitive, must match exactly.
Definition: WString.h:531
bool equals(const String &s) const
Definition: WString.h:437
void toLowerCase(void)
Convert the entire String content to lower case.
bool reserve(size_t size)
Pre-allocate String memory.
bool endsWith(char suffix) const
Compare the end of a String.
const char * cbuffer() const
Definition: WString.h:856
int compareTo(const char *cstr, size_t length) const
size_t getBytes(unsigned char *buf, size_t bufsize, size_t index=0) const
Read contents of a String into a buffer.
void toCharArray(char *buf, size_t bufsize, size_t index=0) const
Read contents of String into a buffer.
Definition: WString.h:607
String()
Default constructor.
Definition: WString.h:155
bool equalsIgnoreCase(const char *cstr) const
void toUpperCase(void)
Convert the entire String content to upper case.
String & operator=(const String &rhs)
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
int strcasecmp(const char *s1, const char *s2)
A case-insensitive strcmp().
#define str(s)
Definition: testrunner.h:124