Skip to content

Commit ce3e861

Browse files
author
mikee47
committed
Improve Vector search for char* argument
1 parent 62212fc commit ce3e861

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/String.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace FSTR
2525
{
26-
bool String::equals(const char* cstr, size_t clen) 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) {
@@ -38,6 +38,9 @@ bool String::equals(const char* cstr, size_t clen) const
3838
return false;
3939
}
4040
LOAD_FSTR(buf, *this);
41+
if(ignoreCase) {
42+
return memicmp(buf, cstr, len) == 0;
43+
}
4144
return memcmp(buf, cstr, len) == 0;
4245
}
4346

src/include/FlashString/String.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ 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 = 0, bool ignoreCase = false) const;
201+
202+
bool equalsIgnoreCase(const char* cstr, size_t len = 0) const
203+
{
204+
return equals(cstr, len, true);
205+
}
201206

202207
/** @brief Check for equality with another String
203208
* @param str

src/include/FlashString/Vector.hpp

+20
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ template <class ObjectType> class Vector : public Object<Vector<ObjectType>, con
113113

114114
using Object<Vector<ObjectType>, const ObjectType*>::indexOf;
115115

116+
template <typename T = ObjectType>
117+
typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const char* value,
118+
bool ignoreCase = true) const
119+
{
120+
if(!ignoreCase) {
121+
return Object<Vector<String>, const String*>::indexOf(value);
122+
}
123+
124+
auto dataptr = this->data();
125+
auto len = this->length();
126+
auto clen = strlen(value);
127+
for(unsigned i = 0; i < len; ++i) {
128+
if(unsafeValueAt(dataptr, i).equalsIgnoreCase(value, clen)) {
129+
return i;
130+
}
131+
}
132+
133+
return -1;
134+
}
135+
116136
template <typename ValueType, typename T = ObjectType>
117137
typename std::enable_if<std::is_same<T, String>::value, int>::type indexOf(const ValueType& value,
118138
bool ignoreCase = true) const

0 commit comments

Comments
 (0)