Skip to content

Commit a2ebdcf

Browse files
committed
Compare feed names case-insensitively
Feeds are matched with a case-sensitive search when adding new feed data to a Group. This can cause different values to be published for the same feed in a single call to save() in a way that's not easy to see from the code. Avoid the problem by matching feeds case-insensitively when searching for existing feeds in the Group data list. Note: I added the new `strcmp_nocase` function inline in the AdafruitIO_Group.cpp module since I didn't see any clear place to put these kinds of utility functions in the library. I'm open to moving it, renaming, restructuring it or whatever. Perhaps a better option is to use the `String::equalsIgnoreCase()`, but constructing String temporaries for each comparison seemed excessive to me. Fixes adafruit#123
1 parent 55c5df6 commit a2ebdcf

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/AdafruitIO_Group.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ bool AdafruitIO_Group::save() {
230230
/**************************************************************************/
231231
bool AdafruitIO_Group::get() { return _get_pub->publish("\0"); }
232232

233+
/**************************************************************************/
234+
/* Helper function to compare strings case-insensitively */
235+
int strcmp_nocase(const char *str1, const char *str2) {
236+
while (*str1 && *str2 && tolower(*str1++) == tolower(*str2++)) ;
237+
auto diff = tolower(*str1) - tolower(*str2);
238+
return diff < 0 ? -1 : !!diff;
239+
}
240+
233241
/**************************************************************************/
234242
/*!
235243
@brief Obtains data from feed within group.
@@ -251,7 +259,7 @@ AdafruitIO_Data *AdafruitIO_Group::getFeed(const char *feed) {
251259

252260
while (cur_data != NULL) {
253261

254-
if (strcmp(cur_data->feedName(), feed) == 0) {
262+
if (strcmp_nocase(cur_data->feedName(), feed) == 0) {
255263
return cur_data;
256264
}
257265

@@ -314,7 +322,7 @@ void AdafruitIO_Group::onMessage(const char *feed,
314322

315323
while (cur_cb != NULL) {
316324

317-
if (strcmp(cur_cb->feed, feed) == 0) {
325+
if (strcmp_nocase(cur_cb->feed, feed) == 0) {
318326
return;
319327
}
320328

0 commit comments

Comments
 (0)