Skip to content

Added __FlashStringhelper* comparison operators to String object #2264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions hardware/arduino/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,21 @@ unsigned char String::equals(const char *cstr) const
return strcmp(buffer, cstr) == 0;
}

unsigned char String::equals(const __FlashStringHelper *fstr) const
{
const char PROGMEM *p = (const char PROGMEM *)fstr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you be using reinterpret_cast here? Please see: #1770.

Note that PGM_P macro should be used for compatible with older version of avr-libc. Please see: 98777e8#diff-0 and ffddfc8.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case PROGMEM/PGM_P is not needed at all, like in #1770.


for (int i = 0; i < len; i++)
{
if (pgm_read_byte(p) == 0) return 0;
if (buffer[i] != (char)pgm_read_byte(p++)) return 0;
}

if (pgm_read_byte(p) != 0) return 0;

return 1;
}

unsigned char String::operator<(const String &rhs) const
{
return compareTo(rhs) < 0;
Expand Down
3 changes: 3 additions & 0 deletions hardware/arduino/cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,13 @@ class String
int compareTo(const String &s) const;
unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) const;
unsigned char equals(const __FlashStringHelper *fstr) const;
unsigned char operator == (const String &rhs) const {return equals(rhs);}
unsigned char operator == (const char *cstr) const {return equals(cstr);}
unsigned char operator == (const __FlashStringHelper *fstr) const {return equals(fstr);}
unsigned char operator != (const String &rhs) const {return !equals(rhs);}
unsigned char operator != (const char *cstr) const {return !equals(cstr);}
unsigned char operator != (const __FlashStringHelper *fstr) const {return !equals(fstr);}
unsigned char operator < (const String &rhs) const;
unsigned char operator > (const String &rhs) const;
unsigned char operator <= (const String &rhs) const;
Expand Down