Skip to content
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

Unable to call String::indexOf with char* for a needle #7705

Closed
paulocsanz opened this issue Nov 13, 2020 · 3 comments · Fixed by #7706
Closed

Unable to call String::indexOf with char* for a needle #7705

paulocsanz opened this issue Nov 13, 2020 · 3 comments · Fixed by #7706

Comments

@paulocsanz
Copy link
Contributor

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?

Anyway I'm happy with just char*.

@paulocsanz paulocsanz 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 Nov 13, 2020
@paulocsanz
Copy link
Contributor Author

paulocsanz commented Nov 13, 2020

I've went ahead and provided a String::indexOf for __FlashStringHelper pointers. And am calling strstr_P instead, as @earlephilhower showed me how.

@paulocsanz
Copy link
Contributor Author

@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_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?

@earlephilhower
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants