diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 056bbeb988..3dda69bda4 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -617,19 +617,35 @@ int String::indexOf(char ch, unsigned int fromIndex) const { return temp - buffer(); } -int String::indexOf(const String &s2) const { +int String::indexOf(const __FlashStringHelper *s2) const { return indexOf(s2, 0); } -int String::indexOf(const String &s2, unsigned int fromIndex) const { +int String::indexOf(const __FlashStringHelper *s2, unsigned int fromIndex) const { + return indexOf((const char*) s2, fromIndex); +} + +int String::indexOf(const char *s2) const { + return indexOf(s2, 0); +} + +int String::indexOf(const char *s2, unsigned int fromIndex) const { if (fromIndex >= len()) return -1; - const char *found = strstr(buffer() + fromIndex, s2.buffer()); + const char *found = strstr_P(buffer() + fromIndex, s2); if (found == NULL) return -1; return found - buffer(); } +int String::indexOf(const String &s2) const { + return indexOf(s2, 0); +} + +int String::indexOf(const String &s2, unsigned int fromIndex) const { + return indexOf(s2.c_str(), fromIndex); +} + int String::lastIndexOf(char theChar) const { return lastIndexOf(theChar, len() - 1); } diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index c9b79f21c4..262cfda0a9 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -235,6 +235,10 @@ class String { // search int indexOf(char ch) const; int indexOf(char ch, unsigned int fromIndex) const; + int indexOf(const char *str) const; + int indexOf(const char *str, unsigned int fromIndex) const; + int indexOf(const __FlashStringHelper *str) const; + int indexOf(const __FlashStringHelper *str, unsigned int fromIndex) const; int indexOf(const String &str) const; int indexOf(const String &str, unsigned int fromIndex) const; int lastIndexOf(char ch) const; diff --git a/tests/host/core/test_string.cpp b/tests/host/core/test_string.cpp index f9fb30a6d5..1150ba5913 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -245,6 +245,10 @@ TEST_CASE("String nulls", "[core][String]") REQUIRE(s.lastIndexOf("tacos") == -1); REQUIRE(s.lastIndexOf('t', 0) == -1); REQUIRE(s.lastIndexOf('t') == -1); + REQUIRE(s.indexOf(String("tacos"), 1) == -1); + REQUIRE(s.indexOf(String("tacos")) == -1); + REQUIRE(s.indexOf(F("tacos"), 1) == -1); + REQUIRE(s.indexOf(F("tacos")) == -1); REQUIRE(s.indexOf("tacos", 1) == -1); REQUIRE(s.indexOf("tacos") == -1); REQUIRE(s.indexOf('t', 1) == -1);