Pages: [1]   Go Down
Author Topic: Is there a function to compare two const __FlashStringHelper* strings?  (Read 732 times)
0 Members and 1 Guest are viewing this topic.
rome
Offline Offline
Sr. Member
****
Karma: 15
Posts: 474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... or I need to write my own ?
I know this question is trivial, but I did not find any reference ...
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Should be doable at compile time ( or even when typing the source text ; )
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you mean the pointer was constant rather than the string itself ? 
Logged

rome
Offline Offline
Sr. Member
****
Karma: 15
Posts: 474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

const __FlashStringHelper* p1;
const __FlashStringHelper* p2;
....

<compare p1,p2 to check if the two pointed progmem strings are equal>

« Last Edit: September 10, 2012, 07:21:47 am by brunialti » Logged

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2741
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Even in PROGMEM you should avoid duplicate const strings ( Which is a compile time issue ).
I don't know of an avr-gcc feature to check and optimize duplicate strings, however. 

At runtime, you might maximally have the task to compare the pointers themselves, if they refer to the same loaction in flash.
Or to compare variable strings in RAM with const strings in PROGMEM.
   
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the strings are in progmem they are same/different at compile time. Why on earth can't you determine if they are the same?

Either I don't understand what your problem is or you don't understand that you don't have one.
Logged

rome
Offline Offline
Sr. Member
****
Karma: 15
Posts: 474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use string as a way to store data structure in progmem. I need to search in progmem the matching subkey to get the remaining infos.
I could manage the data in many different ways, or I could even copy a const __FlashStringHelper* into a string and then compare with the search string ... but i was curious to know if there is a compare function for a couple of const __FlashStringHelper*.
Quite trivial, is'nt it?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24361
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I honestly don't know if there is such a function, but I think by now (10:55am to 8:30pm) I could have developed one.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

rome
Offline Offline
Sr. Member
****
Karma: 15
Posts: 474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did.
But it has nothing to do with the question about a built in function probably faster than mine. If any.
I assume there is not :-)
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This compiles, but I'm not sure if it works:
Code:

const __FlashStringHelper* s1;
const __FlashStringHelper* s2;
const __FlashStringHelper* s3;

int strcmp(const __FlashStringHelper* s1, const __FlashStringHelper* s2);

void setup() {
    Serial.begin(115200);
    s1 = F("same");
    s2 = F("different");
    s3 = F("same");

    if (strcmp(s1, s2) == 0)
        Serial.println("s1, s2 same");
    else
        Serial.println("s1, s2 different");

    if (strcmp(s1, s3) == 0)
        Serial.println("s1, s3 same");
    else
        Serial.println("s1, s3 different");

    if (strcmp(s2, s3) == 0)
        Serial.println("s2, s3 same");
    else
        Serial.println("s2, s3 different");
}
void loop() {}

int strcmp(const __FlashStringHelper* s1, const __FlashStringHelper* s2) {
    return strcmp_P(reinterpret_cast<const char*>(s1), reinterpret_cast<const char*>(s2));
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 67
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am investigating about this.

I'm trying to do something like this:

sprintf(buf,F("<br><br><input type='submit' value='Relay 1' class='c%d' onclick='f(1,%d)'>"),digitalRead(RELAY1),digitalRead(RELAY1));

But I receive this error:

cant convert  ‘__FlashStringHelper*’ to ‘const char*’ for argument 2 for ‘int sprintf(char*, const char*, ...)’

I've found I can do this:

sprintf_P(buf,PSTR("<br><br><input type='submit' value='Relay 1' class='c%d' onclick='f(1,%d)'>"),digitalRead(RELAY1),digitalRead(RELAY1));

But I don't know the difference between F() and PSTR(). Can't find clear documentation about this.

Logged

Pages: [1]   Go Up
Jump to: