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

Added __FlashStringhelper* comparison operators to String object #2264

wants to merge 2 commits into from

Conversation

dvdfreitag
Copy link

No description provided.

@@ -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;
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.

@cmaglie
Copy link
Member

cmaglie commented Aug 27, 2014

@dvdfreitag,
thanks for the patch.

May you provide a test sketch that demonstrates the new functionality?

@cmaglie cmaglie added this to the Release 1.5.8 milestone Aug 27, 2014
@dvdfreitag
Copy link
Author

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.
@ffissore
Copy link
Contributor

@ArduinoBot build this please

@ArduinoBot
Copy link
Contributor

Merged build finished. Test FAILed.

@ArduinoBot
Copy link
Contributor

Build failed.

@ffissore
Copy link
Contributor

Sorry for scheduling the PR builder. It works only on PRs against branch ide-1.5.x

@cmaglie
Copy link
Member

cmaglie commented Aug 29, 2014

@dvdfreitag
I tried the following sketch on the latest master branch on an Arduino Uno:

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:

0
0
1
0

as expected. May you provide an example with all the details to reproduce the issue?

@dvdfreitag
Copy link
Author

@cmaglie
Without the if statement added in cb35772, the __FlashStringHelper* is not checked for a null terminator. For example:

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.

@cmaglie
Copy link
Member

cmaglie commented Aug 29, 2014

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?
Maybe I'm missing something but on my Arduino, without your patch, the output is 0 as expected.

@dvdfreitag
Copy link
Author

Mmm, have you actually tested it?

Yes, using IDE 1.0.5-r2 that sketch prints 1 for me without cb35772

@cmaglie
Copy link
Member

cmaglie commented Aug 29, 2014

May you retry without your patch, I mean without both cb35772 and c780997?
I think that this bug has been already fixed in master branch (but not yet released in 1.0.6).

@dvdfreitag
Copy link
Author

I think that this bug has been already fixed in master branch (but not yet released in 1.0.6).

Sorry for the delay, yes, it appears that you are correct. This bug is present in 1.0.5-r2, but not in the master branch.

@dvdfreitag dvdfreitag closed this Sep 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants