-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Conversation
@@ -453,6 +453,19 @@ 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@dvdfreitag, May you provide a test sketch that demonstrates the new functionality? |
Something really basic (so basic it looks pointless), String toCompare = "Hello World!";
void setup() {
Serial.begin(9600);
Serial.println(hello == F("Hello World!"));
}
void loop() { } Should always print 1. A more advanced use case could be reading a string from Serial (or SPI, I2C etc..) and then comparing that string to a string stored in flash. |
Without this check the comparison of the String "Hello World!" and the flash String "Hello World!something" would return 1.
@ArduinoBot build this please |
Merged build finished. Test FAILed. |
Build failed. |
Sorry for scheduling the PR builder. It works only on PRs against branch ide-1.5.x |
@dvdfreitag String hello = "Hello";
void setup() {
hello += " World!";
Serial.begin(9600);
Serial.println(hello == F("Hello"));
Serial.println(hello == F(" World!"));
Serial.println(hello == F("Hello World!"));
Serial.println(hello == F("BLAH"));
}
void loop() {
} and the result is:
as expected. May you provide an example with all the details to reproduce the issue? |
@cmaglie String toCompare = "Hello World!";
void setup() {
Serial.begin(9600);
Serial.println(toCompare == F("Hello World!123"));
}
void loop() { } This sketch would produce a 1 since the original for loop only compares up to the null terminator in the String. The added check just ensures that they are both terminated in the same location. |
Mmm, have you actually tested it? |
Yes, using IDE 1.0.5-r2 that sketch prints |
Sorry for the delay, yes, it appears that you are correct. This bug is present in 1.0.5-r2, but not in the |
No description provided.