You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to call String::indexOf with char* for a needle #7705
Hey I'm trying to avoid allocating+copying to search for a needle in a haystack. So it would be great if String provided a indexOf method that receives a char*.
Ideally it would provide a method for __FlashStringHelper, but since it uses asm/intrinsics to search I don't know if there is a way to do the search without ignoring that it's from PROGMEM. Is it possible?
changed the title [-]Unable to call String::indexOf with char* for a needle (implicit conversion)[/-][+]Unable to call String::indexOf with char* for a needle[/+]on Nov 13, 2020
In the documentation (second paragraph of this section) it says it's UB to call strstr_P with a needle that's not from PROGMEM. So is the documentation wrong or is my code UB?
Should I restore the code to have the strstr_P path only for __FlashStringHelper and keep using strstr for the rest?
If the documentation says that, it's wrong and we should fix it. Anything using PROGMEM is also safe to use RAM (but not vice-versa, of course!). The ESP8266 isn't like the AVRs where reads from PROGMEM require a special instruction. On the 8266 they just need to be 4-byte aligned and only 32-bits in size. The _P calls handle this behind the scenes.
Activity
[-]Unable to call String::indexOf with char* for a needle (implicit conversion)[/-][+]Unable to call String::indexOf with char* for a needle[/+]paulocsanz commentedon Nov 13, 2020
I've went ahead and provided a
String::indexOffor__FlashStringHelperpointers. And am callingstrstr_Pinstead, as @earlephilhower showed me how.paulocsanz commentedon Nov 13, 2020
@earlephilhower @d-a-v
https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html#functions-to-read-back-from-progmem
In the documentation (second paragraph of this section) it says it's UB to call
strstr_Pwith a needle that's not fromPROGMEM. So is the documentation wrong or is my code UB?Should I restore the code to have the
strstr_Ppath only for__FlashStringHelperand keep usingstrstrfor the rest?earlephilhower commentedon Nov 13, 2020
If the documentation says that, it's wrong and we should fix it. Anything using PROGMEM is also safe to use RAM (but not vice-versa, of course!). The ESP8266 isn't like the AVRs where reads from PROGMEM require a special instruction. On the 8266 they just need to be 4-byte aligned and only 32-bits in size. The
_Pcalls handle this behind the scenes.