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