Skip to content

Commit c5c9f84

Browse files
authored
Provide String::indexOf for a char* needle (#7706)
1 parent 8375faa commit c5c9f84

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

cores/esp8266/WString.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -617,19 +617,35 @@ int String::indexOf(char ch, unsigned int fromIndex) const {
617617
return temp - buffer();
618618
}
619619

620-
int String::indexOf(const String &s2) const {
620+
int String::indexOf(const __FlashStringHelper *s2) const {
621621
return indexOf(s2, 0);
622622
}
623623

624-
int String::indexOf(const String &s2, unsigned int fromIndex) const {
624+
int String::indexOf(const __FlashStringHelper *s2, unsigned int fromIndex) const {
625+
return indexOf((const char*) s2, fromIndex);
626+
}
627+
628+
int String::indexOf(const char *s2) const {
629+
return indexOf(s2, 0);
630+
}
631+
632+
int String::indexOf(const char *s2, unsigned int fromIndex) const {
625633
if (fromIndex >= len())
626634
return -1;
627-
const char *found = strstr(buffer() + fromIndex, s2.buffer());
635+
const char *found = strstr_P(buffer() + fromIndex, s2);
628636
if (found == NULL)
629637
return -1;
630638
return found - buffer();
631639
}
632640

641+
int String::indexOf(const String &s2) const {
642+
return indexOf(s2, 0);
643+
}
644+
645+
int String::indexOf(const String &s2, unsigned int fromIndex) const {
646+
return indexOf(s2.c_str(), fromIndex);
647+
}
648+
633649
int String::lastIndexOf(char theChar) const {
634650
return lastIndexOf(theChar, len() - 1);
635651
}

cores/esp8266/WString.h

+4
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ class String {
235235
// search
236236
int indexOf(char ch) const;
237237
int indexOf(char ch, unsigned int fromIndex) const;
238+
int indexOf(const char *str) const;
239+
int indexOf(const char *str, unsigned int fromIndex) const;
240+
int indexOf(const __FlashStringHelper *str) const;
241+
int indexOf(const __FlashStringHelper *str, unsigned int fromIndex) const;
238242
int indexOf(const String &str) const;
239243
int indexOf(const String &str, unsigned int fromIndex) const;
240244
int lastIndexOf(char ch) const;

tests/host/core/test_string.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ TEST_CASE("String nulls", "[core][String]")
245245
REQUIRE(s.lastIndexOf("tacos") == -1);
246246
REQUIRE(s.lastIndexOf('t', 0) == -1);
247247
REQUIRE(s.lastIndexOf('t') == -1);
248+
REQUIRE(s.indexOf(String("tacos"), 1) == -1);
249+
REQUIRE(s.indexOf(String("tacos")) == -1);
250+
REQUIRE(s.indexOf(F("tacos"), 1) == -1);
251+
REQUIRE(s.indexOf(F("tacos")) == -1);
248252
REQUIRE(s.indexOf("tacos", 1) == -1);
249253
REQUIRE(s.indexOf("tacos") == -1);
250254
REQUIRE(s.indexOf('t', 1) == -1);

0 commit comments

Comments
 (0)