CString.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  * CString.h
8  *
9  * @author: 2020 - Mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include <WString.h>
16 #include <stringutil.h>
17 #include <memory>
18 
26 class CString : public std::unique_ptr<char[]>
27 {
28 public:
29  CString() = default;
30 
31  CString(const CString& src) : CString(src.get())
32  {
33  }
34 
35  CString(CString&& other) = default;
36 
37  CString(const String& src)
38  {
39  assign(src);
40  }
41 
42  CString(const char* src)
43  {
44  assign(src);
45  }
46 
47  ~CString() = default;
48 
49  void assign(const String& src)
50  {
51  if(src) {
52  assign(src.c_str(), src.length());
53  } else {
54  reset();
55  }
56  }
57 
58  void assign(const char* src)
59  {
60  assign(src, src ? strlen(src) : 0);
61  }
62 
63  void assign(const char* src, size_t len)
64  {
65  if(src == nullptr || len == 0) {
66  reset();
67  } else {
68  ++len;
69  reset(new char[len]);
70  memcpy(get(), src, len);
71  }
72  }
73 
74  CString& operator=(const CString& src)
75  {
76  assign(src.get());
77  return *this;
78  }
79 
80  CString& operator=(CString&& src) = default;
81 
82  CString& operator=(const String& src)
83  {
84  assign(src);
85  return *this;
86  }
87 
88  CString& operator=(const char* src)
89  {
90  assign(src);
91  return *this;
92  }
93 
94  char* begin()
95  {
96  return get();
97  }
98 
99  const char* c_str() const
100  {
101  return get() ?: "";
102  }
103 
104  bool equals(const CString& other) const
105  {
106  return strcmp(c_str(), other.c_str()) == 0;
107  }
108 
109  bool equals(const String& other) const
110  {
111  return other.equals(c_str());
112  }
113 
114  bool equals(const char* other) const
115  {
116  if(other == nullptr) {
117  return length() == 0;
118  }
119  return strcmp(c_str(), other) == 0;
120  }
121 
122  bool equalsIgnoreCase(const CString& other) const
123  {
124  return strcasecmp(c_str(), other.c_str()) == 0;
125  }
126 
127  bool equalsIgnoreCase(const String& other) const
128  {
129  return other.equalsIgnoreCase(c_str());
130  }
131 
132  bool equalsIgnoreCase(const char* other) const
133  {
134  if(other == nullptr) {
135  return length() == 0;
136  }
137  return strcasecmp(c_str(), other) == 0;
138  }
139 
140  bool operator==(const CString& other) const
141  {
142  return equals(other);
143  }
144 
145  bool operator==(const String& other) const
146  {
147  return equals(other);
148  }
149 
150  bool operator==(const char* other) const
151  {
152  return equals(other);
153  }
154 
155  size_t length() const
156  {
157  auto p = get();
158  return p ? strlen(p) : 0;
159  }
160 
161  explicit operator String() const
162  {
163  return get();
164  }
165 };
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:27
void assign(const char *src)
Definition: CString.h:58
bool equalsIgnoreCase(const char *other) const
Definition: CString.h:132
void assign(const String &src)
Definition: CString.h:49
bool operator==(const CString &other) const
Definition: CString.h:140
CString & operator=(const CString &src)
Definition: CString.h:74
~CString()=default
bool equals(const String &other) const
Definition: CString.h:109
bool equals(const char *other) const
Definition: CString.h:114
const char * c_str() const
Definition: CString.h:99
bool operator==(const char *other) const
Definition: CString.h:150
CString(CString &&other)=default
bool equalsIgnoreCase(const CString &other) const
Definition: CString.h:122
CString(const CString &src)
Definition: CString.h:31
bool equals(const CString &other) const
Definition: CString.h:104
bool equalsIgnoreCase(const String &other) const
Definition: CString.h:127
CString & operator=(CString &&src)=default
CString & operator=(const char *src)
Definition: CString.h:88
CString & operator=(const String &src)
Definition: CString.h:82
void assign(const char *src, size_t len)
Definition: CString.h:63
size_t length() const
Definition: CString.h:155
bool operator==(const String &other) const
Definition: CString.h:145
char * begin()
Definition: CString.h:94
CString(const String &src)
Definition: CString.h:37
CString()=default
CString(const char *src)
Definition: CString.h:42
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 equals(const String &s) const
Definition: WString.h:430
bool equalsIgnoreCase(const char *cstr) const
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().