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 
196  void clear()
197  {
198  *this = nullptr;
199  stringCount = 0;
200  }
201 
205  unsigned count() const;
206 
211  class Iterator
212  {
213  public:
214  Iterator() = default;
215  Iterator(const Iterator&) = default;
216 
218  : array_(array), offset_(offset), index_(index)
219  {
220  }
221 
222  operator bool() const
223  {
224  return array_ != nullptr && offset_ < array_->length();
225  }
226 
227  bool equals(const char* rhs) const
228  {
229  auto s = str();
230  return s == rhs || strcmp(s, rhs) == 0;
231  }
232 
233  bool equalsIgnoreCase(const char* rhs) const
234  {
235  auto s = str();
236  return s == rhs || strcasecmp(str(), rhs) == 0;
237  }
238 
239  bool operator==(const Iterator& rhs) const
240  {
241  return array_ == rhs.array_ && offset_ == rhs.offset_;
242  }
243 
244  bool operator!=(const Iterator& rhs) const
245  {
246  return !operator==(rhs);
247  }
248 
249  bool operator==(const char* rhs) const
250  {
251  return equals(rhs);
252  }
253 
254  bool operator!=(const char* rhs) const
255  {
256  return !equals(rhs);
257  }
258 
259  bool equals(const String& rhs) const
260  {
261  return rhs.equals(str());
262  }
263 
264  bool equalsIgnoreCase(const String& rhs) const
265  {
266  return rhs.equalsIgnoreCase(str());
267  }
268 
269  bool operator==(const String& rhs) const
270  {
271  return equals(rhs);
272  }
273 
274  bool operator!=(const String& rhs) const
275  {
276  return !equals(rhs);
277  }
278 
279  const char* str() const
280  {
281  if(array_ == nullptr) {
282  return "";
283  }
284 
285  return array_->c_str() + offset_;
286  }
287 
288  const char* operator*() const
289  {
290  return str();
291  }
292 
293  uint16_t index() const
294  {
295  return index_;
296  }
297 
298  uint16_t offset() const
299  {
300  return offset_;
301  }
302 
304  {
305  next();
306  return *this;
307  }
308 
310  {
311  Iterator tmp(*this);
312  next();
313  return tmp;
314  }
315 
316  void next()
317  {
318  if(*this) {
319  offset_ += strlen(str()) + 1;
320  ++index_;
321  }
322  }
323 
325 
326  private:
327  const CStringArray* array_ = nullptr;
328  uint16_t offset_ = 0;
329  uint16_t index_ = 0;
330  };
331 
332  Iterator begin() const
333  {
334  return Iterator(this, 0, 0);
335  }
336 
337  Iterator end() const
338  {
339  return Iterator(this, length(), count());
340  }
341 
344 private:
345  void init();
346 
347 private:
348  mutable unsigned stringCount = 0;
349 };
void next()
Definition: CStringArray.h:316
CStringArray & operator+=(const char *cstr)
Definition: CStringArray.h:121
bool equals(const String &rhs) const
Definition: CStringArray.h:259
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:556
CStringArray(const FlashString &fstr)
Definition: CStringArray.h:69
bool operator==(const char *rhs) const
Definition: CStringArray.h:249
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
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:196
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:233
void toUpperCase(void)
Convert the entire String content to upper case.
Iterator(const CStringArray *array, uint16_t offset, uint16_t index)
Definition: CStringArray.h:217
const char * getValue(unsigned index) const
Get string at the given position.
bool operator!=(const char *rhs) const
Definition: CStringArray.h:254
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:269
const char * operator*() const
Definition: CStringArray.h:288
const char * operator[](unsigned index) const
Get string at the given position.
Definition: CStringArray.h:189
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:337
CStringArray & operator+=(T value)
Append numbers, etc. to the array.
Definition: CStringArray.h:131
bool equalsIgnoreCase(const String &rhs) const
Definition: CStringArray.h:264
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:274
bool equals(const char *rhs) const
Definition: CStringArray.h:227
bool endsWith(char suffix) const
Compare the end of a String.
Definition: CStringArray.h:211
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
bool contains(const String &str, bool ignoreCase=true) const
Check if array contains a string.
Definition: CStringArray.h:174
Iterator & operator++()
Definition: CStringArray.h:303
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:632
Definition: WString.h:878
void toLowerCase(void)
Convert the entire String content to lower case.
const char * str() const
Definition: CStringArray.h:279
bool reserve(size_t size)
Pre-allocate String memory.
Iterator operator++(int)
Definition: CStringArray.h:309
CStringArray & operator+=(const String &str)
Definition: CStringArray.h:115
bool equals(const String &s) const
Definition: WString.h:462
uint16_t offset() const
Definition: CStringArray.h:298
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:332
bool operator==(const Iterator &rhs) const
Definition: CStringArray.h:239
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:293
bool operator!=(const Iterator &rhs) const
Definition: CStringArray.h:244
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