Skip to content

Commit 6da2129

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. Also, subscribing to a feed using the uppercase name of the feed will never work since the feed names are lowercased when received from AdafruitIO but the string compare here is strictly case-sensitive. Fix these problems by matching feed names case-insensitively when searching for existing feeds in the Group data list and when matching callback lookups. Note: I added the new `strsame_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 #123
1 parent 0a7b431 commit 6da2129

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/AdafruitIO_Group.cpp

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

233+
/**************************************************************************/
234+
/*
235+
@brief Helper function to compare strings case-insensitively
236+
@return True if strings are equal (ignoring case)
237+
*/
238+
/**************************************************************************/
239+
bool strsame_nocase(const char *str1, const char *str2) {
240+
while ( *str1 && *str2 && tolower(*str1) == tolower(*str2) ) {
241+
++str1;
242+
++str2;
243+
}
244+
return tolower(*str1) == tolower(*str2);
245+
}
246+
233247
/**************************************************************************/
234248
/*!
235249
@brief Obtains data from feed within group.
@@ -251,7 +265,7 @@ AdafruitIO_Data *AdafruitIO_Group::getFeed(const char *feed) {
251265

252266
while (cur_data != NULL) {
253267

254-
if (strcmp(cur_data->feedName(), feed) == 0) {
268+
if (strsame_nocase(cur_data->feedName(), feed)) {
255269
return cur_data;
256270
}
257271

@@ -314,7 +328,7 @@ void AdafruitIO_Group::onMessage(const char *feed,
314328

315329
while (cur_cb != NULL) {
316330

317-
if (strcmp(cur_cb->feed, feed) == 0) {
331+
if (strsame_nocase(cur_cb->feed, feed)) {
318332
return;
319333
}
320334

@@ -345,7 +359,7 @@ void AdafruitIO_Group::call(AdafruitIO_Data *d) {
345359

346360
while (cur_cb) {
347361

348-
if (cur_cb->feed == NULL || strcmp(cur_cb->feed, d->feedName()) == 0) {
362+
if (cur_cb->feed == NULL || strsame_nocase(cur_cb->feed, d->feedName())) {
349363
cur_cb->dataCallback(d);
350364
}
351365

0 commit comments

Comments
 (0)