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  CStringArray(String&& rval) noexcept : String(std::move(rval))
75  {
76  init();
77  }
78 
79  CStringArray(StringSumHelper&& rval) noexcept : String(std::move(rval))
80  {
81  init();
82  }
85  CStringArray& operator=(const char* cstr)
86  {
87  String::operator=(cstr);
88  init();
89  return *this;
90  }
91 
96  {
97  return std::move(*this);
98  }
99 
106  bool add(const char* str, int length = -1);
107 
113  bool add(const String& str)
114  {
115  return add(str.c_str(), str.length());
116  }
117 
123  {
124  add(str);
125  return *this;
126  }
127 
128  CStringArray& operator+=(const char* cstr)
129  {
130  add(cstr);
131  return *this;
132  }
133 
138  template <typename T> CStringArray& operator+=(const T& value)
139  {
140  add(String(value));
141  return *this;
142  }
151  int indexOf(const char* str, bool ignoreCase = true) const;
152 
159  int indexOf(const String& str, bool ignoreCase = true) const
160  {
161  return indexOf(str.c_str(), ignoreCase);
162  }
163 
170  bool contains(const char* str, bool ignoreCase = true) const
171  {
172  return indexOf(str, ignoreCase) >= 0;
173  }
174 
181  bool contains(const String& str, bool ignoreCase = true) const
182  {
183  return indexOf(str, ignoreCase) >= 0;
184  }
185 
190  const char* getValue(unsigned index) const;
191 
196  const char* operator[](unsigned index) const
197  {
198  return getValue(index);
199  }
200 
204  const char* front() const
205  {
206  return cbuffer();
207  }
208 
214  bool pushFront(const char* str);
215 
221 
225  const char* back() const;
226 
232  bool pushBack(const char* str)
233  {
234  return add(str);
235  }
236 
242 
245  void clear()
246  {
247  *this = nullptr;
248  stringCount = 0;
249  }
250 
254  unsigned count() const;
255 
263  String join(const String& separator = ",") const;
264 
269  class Iterator
270  {
271  public:
272  Iterator() = default;
273  Iterator(const Iterator&) = default;
274 
275  Iterator(const CStringArray* array, uint16_t offset, uint16_t index)
276  : mArray(array), mOffset(offset), mIndex(index)
277  {
278  }
279 
280  operator bool() const
281  {
282  return mArray != nullptr && mOffset < mArray->length();
283  }
284 
285  bool equals(const char* rhs) const
286  {
287  auto s = str();
288  return s == rhs || strcmp(s, rhs) == 0;
289  }
290 
291  bool equalsIgnoreCase(const char* rhs) const
292  {
293  auto s = str();
294  return s == rhs || strcasecmp(str(), rhs) == 0;
295  }
296 
297  bool operator==(const Iterator& rhs) const
298  {
299  return mArray == rhs.mArray && mOffset == rhs.mOffset;
300  }
301 
302  bool operator!=(const Iterator& rhs) const
303  {
304  return !operator==(rhs);
305  }
306 
307  bool operator==(const char* rhs) const
308  {
309  return equals(rhs);
310  }
311 
312  bool operator!=(const char* rhs) const
313  {
314  return !equals(rhs);
315  }
316 
317  bool equals(const String& rhs) const
318  {
319  return rhs.equals(str());
320  }
321 
322  bool equalsIgnoreCase(const String& rhs) const
323  {
324  return rhs.equalsIgnoreCase(str());
325  }
326 
327  bool operator==(const String& rhs) const
328  {
329  return equals(rhs);
330  }
331 
332  bool operator!=(const String& rhs) const
333  {
334  return !equals(rhs);
335  }
336 
337  const char* str() const
338  {
339  if(mArray == nullptr) {
340  return "";
341  }
342 
343  return mArray->c_str() + mOffset;
344  }
345 
346  const char* operator*() const
347  {
348  return str();
349  }
350 
351  uint16_t index() const
352  {
353  return mIndex;
354  }
355 
356  uint16_t offset() const
357  {
358  return mOffset;
359  }
360 
362  {
363  next();
364  return *this;
365  }
366 
368  {
369  Iterator tmp(*this);
370  next();
371  return tmp;
372  }
373 
374  void next()
375  {
376  if(*this) {
377  mOffset += strlen(str()) + 1;
378  ++mIndex;
379  }
380  }
381 
383 
384  private:
385  const CStringArray* mArray = nullptr;
386  uint16_t mOffset = 0;
387  uint16_t mIndex = 0;
388  };
389 
390  Iterator begin() const
391  {
392  return Iterator(this, 0, 0);
393  }
394 
395  Iterator end() const
396  {
397  return Iterator(this, length(), count());
398  }
399 
402 private:
403  void init();
404 
405 private:
406  mutable unsigned stringCount = 0;
407 };
const __FlashStringHelper * flash_string_t
Provides a strongly-typed pointer to allow safe implicit operation using String class methods.
Definition: WString.h:92
Definition: CStringArray.h:270
bool operator==(const Iterator &rhs) const
Definition: CStringArray.h:297
const char * str() const
Definition: CStringArray.h:337
bool equals(const String &rhs) const
Definition: CStringArray.h:317
bool operator!=(const Iterator &rhs) const
Definition: CStringArray.h:302
void next()
Definition: CStringArray.h:374
bool operator!=(const String &rhs) const
Definition: CStringArray.h:332
bool operator==(const String &rhs) const
Definition: CStringArray.h:327
Iterator & operator++()
Definition: CStringArray.h:361
bool equalsIgnoreCase(const char *rhs) const
Definition: CStringArray.h:291
bool equalsIgnoreCase(const String &rhs) const
Definition: CStringArray.h:322
Iterator(const Iterator &)=default
bool equals(const char *rhs) const
Definition: CStringArray.h:285
const char * operator*() const
Definition: CStringArray.h:346
bool operator!=(const char *rhs) const
Definition: CStringArray.h:312
uint16_t offset() const
Definition: CStringArray.h:356
Iterator(const CStringArray *array, uint16_t offset, uint16_t index)
Definition: CStringArray.h:275
Iterator operator++(int)
Definition: CStringArray.h:367
bool operator==(const char *rhs) const
Definition: CStringArray.h:307
uint16_t index() const
Definition: CStringArray.h:351
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:609
Iterator begin() const
Definition: CStringArray.h:390
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:204
bool contains(const char *str, bool ignoreCase=true) const
Check if array contains a string.
Definition: CStringArray.h:170
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:181
CStringArray & operator+=(const char *cstr)
Definition: CStringArray.h:128
CStringArray(const String &str)
Definition: CStringArray.h:49
CStringArray(StringSumHelper &&rval) noexcept
Definition: CStringArray.h:79
CStringArray & operator+=(const T &value)
Append numbers, etc. to the array.
Definition: CStringArray.h:138
bool add(const String &str)
Append a new string (or array of strings) to the end of the array.
Definition: CStringArray.h:113
String release()
Give up underlying String object to caller so it can be manipulated.
Definition: CStringArray.h:95
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:232
CStringArray(const FlashString &fstr)
Definition: CStringArray.h:69
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:122
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:196
int indexOf(const String &str, bool ignoreCase=true) const
Find the given string and return its index.
Definition: CStringArray.h:159
const char * getValue(unsigned index) const
Get string at the given position.
void clear()
Empty the array.
Definition: CStringArray.h:245
CStringArray(flash_string_t pstr, int length=-1)
Definition: CStringArray.h:64
CStringArray(String &&rval) noexcept
Definition: CStringArray.h:74
CStringArray & operator=(const char *cstr)
Definition: CStringArray.h:85
Iterator end() const
Definition: CStringArray.h:395
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:238
describes a counted string stored in flash memory
Definition: String.hpp:174
Definition: WString.h:884
The String class.
Definition: WString.h:133
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:609
bool startsWith(const String &prefix) const
Compare the start of a String Comparison is case-sensitive, must match exactly.
Definition: WString.h:524
bool equals(const String &s) const
Definition: WString.h:430
void(String::*)() const StringIfHelperType
Definition: WString.h:138
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:850
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:600
String()
Default constructor.
Definition: WString.h:152
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:238
int strcasecmp(const char *s1, const char *s2)
A case-insensitive strcmp().
#define str(s)
Definition: testrunner.h:124