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)
32  {
33  assign(src.get());
34  }
35 
36  CString(CString&& other) = default;
37 
38  CString(const String& src)
39  {
40  assign(src);
41  }
42 
43  CString(const char* src)
44  {
45  assign(src);
46  }
47 
48  ~CString() = default;
49 
50  void assign(const String& src)
51  {
52  if(src) {
53  assign(src.c_str(), src.length());
54  } else {
55  reset();
56  }
57  }
58 
59  void assign(const char* src)
60  {
61  assign(src, src ? strlen(src) : 0);
62  }
63 
64  void assign(const char* src, size_t len)
65  {
66  if(src == nullptr || len == 0) {
67  reset();
68  } else {
69  ++len;
70  reset(new char[len]);
71  memcpy(get(), src, len);
72  }
73  }
74 
75  CString& operator=(const CString& src)
76  {
77  assign(src.get());
78  return *this;
79  }
80 
81  CString& operator=(CString&& src) = default;
82 
83  CString& operator=(const String& src)
84  {
85  assign(src);
86  return *this;
87  }
88 
89  CString& operator=(const char* src)
90  {
91  assign(src);
92  return *this;
93  }
94 
95  char* begin()
96  {
97  return get();
98  }
99 
100  const char* c_str() const
101  {
102  return get() ?: "";
103  }
104 
105  bool equals(const CString& other) const
106  {
107  return strcmp(c_str(), other.c_str()) == 0;
108  }
109 
110  bool equals(const String& other) const
111  {
112  return other.equals(c_str());
113  }
114 
115  bool equals(const char* other) const
116  {
117  if(other == nullptr) {
118  return length() == 0;
119  }
120  return strcmp(c_str(), other) == 0;
121  }
122 
123  bool equalsIgnoreCase(const CString& other) const
124  {
125  return strcasecmp(c_str(), other.c_str()) == 0;
126  }
127 
128  bool equalsIgnoreCase(const String& other) const
129  {
130  return other.equalsIgnoreCase(c_str());
131  }
132 
133  bool equalsIgnoreCase(const char* other) const
134  {
135  if(other == nullptr) {
136  return length() == 0;
137  }
138  return strcasecmp(c_str(), other) == 0;
139  }
140 
141  bool operator==(const CString& other) const
142  {
143  return equals(other);
144  }
145 
146  bool operator==(const String& other) const
147  {
148  return equals(other);
149  }
150 
151  bool operator==(const char* other) const
152  {
153  return equals(other);
154  }
155 
156  size_t length() const
157  {
158  auto p = get();
159  return p ? strlen(p) : 0;
160  }
161 
162  explicit operator String() const
163  {
164  return get();
165  }
166 };
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:59
bool equalsIgnoreCase(const char *other) const
Definition: CString.h:133
void assign(const String &src)
Definition: CString.h:50
bool operator==(const CString &other) const
Definition: CString.h:141
CString & operator=(const CString &src)
Definition: CString.h:75
~CString()=default
bool equals(const String &other) const
Definition: CString.h:110
bool equals(const char *other) const
Definition: CString.h:115
const char * c_str() const
Definition: CString.h:100
bool operator==(const char *other) const
Definition: CString.h:151
CString(CString &&other)=default
bool equalsIgnoreCase(const CString &other) const
Definition: CString.h:123
CString(const CString &src)
Definition: CString.h:31
bool equals(const CString &other) const
Definition: CString.h:105
bool equalsIgnoreCase(const String &other) const
Definition: CString.h:128
CString & operator=(CString &&src)=default
CString & operator=(const char *src)
Definition: CString.h:89
CString & operator=(const String &src)
Definition: CString.h:83
void assign(const char *src, size_t len)
Definition: CString.h:64
size_t length() const
Definition: CString.h:156
bool operator==(const String &other) const
Definition: CString.h:146
char * begin()
Definition: CString.h:95
CString(const String &src)
Definition: CString.h:38
CString()=default
CString(const char *src)
Definition: CString.h:43
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
bool equals(const String &s) const
Definition: WString.h:438
bool equalsIgnoreCase(const char *cstr) const
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:244
int strcasecmp(const char *s1, const char *s2)
A case-insensitive strcmp().