Skip to content

Commit 72e81a2

Browse files
author
mikee47
committed
Rationalise string comparison methods
1 parent 5059948 commit 72e81a2

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

src/String.cpp

+26-21
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,44 @@
2323

2424
namespace FSTR
2525
{
26-
bool String::equals(const char* cstr, size_t len) const
26+
bool String::equals(const char* cstr, size_t clen, bool ignoreCase) const
2727
{
2828
// Unlikely we'd want an empty flash string, but check anyway
2929
if(cstr == nullptr) {
3030
return length() == 0;
3131
}
32-
// Don't use strcmp as our data may contain nuls
33-
if(len == 0) {
34-
len = strlen(cstr);
35-
}
36-
if(len != length()) {
32+
auto len = length();
33+
if(clen != len) {
3734
return false;
3835
}
3936
LOAD_FSTR(buf, *this);
37+
if(ignoreCase) {
38+
return memicmp(buf, cstr, len) == 0;
39+
}
4040
return memcmp(buf, cstr, len) == 0;
4141
}
4242

43-
bool String::equals(const String& str) const
43+
bool String::equals(const char* cstr, bool ignoreCase) const
44+
{
45+
return equals(cstr, cstr ? strlen(cstr) : 0, ignoreCase);
46+
}
47+
48+
bool String::equals(const String& str, bool ignoreCase) const
4449
{
45-
if(data() == str.data()) {
50+
auto dataptr = data();
51+
auto strdata = str.data();
52+
if(dataptr == strdata) {
4653
return true;
4754
}
48-
if(length() != str.length()) {
55+
auto len = length();
56+
if(len != str.length()) {
4957
return false;
5058
}
51-
return memcmp_aligned(data(), str.data(), length()) == 0;
59+
if(ignoreCase) {
60+
LOAD_FSTR(buf, *this);
61+
return memicmp(dataptr, buf, len) == 0;
62+
}
63+
return memcmp_aligned(dataptr, strdata, len) == 0;
5264
}
5365

5466
/* Wiring String support */
@@ -58,25 +70,18 @@ String::operator WString() const
5870
return isNull() ? WString() : WString(data(), length());
5971
}
6072

61-
bool String::equals(const WString& str) const
73+
bool String::equals(const WString& str, bool ignoreCase) const
6274
{
6375
auto len = str.length();
6476
if(len != length()) {
6577
return false;
6678
}
6779
// @todo optimise memcmp_P then we won't need to load entire String into RAM first
6880
LOAD_FSTR(buf, *this);
69-
return memcmp(buf, str.c_str(), len) == 0;
70-
}
71-
72-
bool String::equalsIgnoreCase(const WString& str) const
73-
{
74-
auto len = str.length();
75-
if(len != length()) {
76-
return false;
81+
if(ignoreCase) {
82+
return memicmp(buf, str.c_str(), len) == 0;
7783
}
78-
LOAD_FSTR(buf, *this);
79-
return memicmp(buf, str.c_str(), len) == 0;
84+
return memcmp(buf, str.c_str(), len) == 0;
8085
}
8186

8287
} // namespace FSTR

src/include/FlashString/String.hpp

+17-27
Original file line numberDiff line numberDiff line change
@@ -197,30 +197,32 @@ class String : public Object<String, char>
197197
* @retval bool true if strings are identical
198198
* @note loads string into a stack buffer for the comparison, no heap required
199199
*/
200-
bool equals(const char* cstr, size_t len = 0) const;
200+
bool equals(const char* cstr, size_t len, bool ignoreCase = false) const;
201201

202-
/** @brief Check for equality with another String
203-
* @param str
204-
* @retval bool true if strings are identical
205-
*/
206-
bool equals(const String& str) const;
207-
208-
bool operator==(const char* str) const
202+
bool equalsIgnoreCase(const char* cstr, size_t len) const
209203
{
210-
return equals(str);
204+
return equals(cstr, len, true);
211205
}
212206

213-
bool operator==(const String& str) const
207+
bool equals(const char* cstr, bool ignoreCase = false) const;
208+
209+
template <typename T> bool equalsIgnoreCase(const T& str) const
214210
{
215-
return equals(str);
211+
return equals(str, true);
216212
}
217213

218-
bool operator!=(const char* str) const
214+
/** @brief Check for equality with another String
215+
* @param str
216+
* @retval bool true if strings are identical
217+
*/
218+
bool equals(const String& str, bool ignoreCase = false) const;
219+
220+
template <typename T> bool operator==(const T& str) const
219221
{
220-
return !equals(str);
222+
return equals(str);
221223
}
222224

223-
bool operator!=(const String& str) const
225+
template <typename T> bool operator!=(const T& str) const
224226
{
225227
return !equals(str);
226228
}
@@ -229,19 +231,7 @@ class String : public Object<String, char>
229231

230232
operator WString() const;
231233

232-
bool equals(const WString& str) const;
233-
234-
bool equalsIgnoreCase(const WString& str) const;
235-
236-
bool operator==(const WString& str) const
237-
{
238-
return equals(str);
239-
}
240-
241-
bool operator!=(const WString& str) const
242-
{
243-
return !equals(str);
244-
}
234+
bool equals(const WString& str, bool ignoreCase = false) const;
245235

246236
/* Arduino Print support */
247237

0 commit comments

Comments
 (0)