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

Provide String::indexOf for a char* needle #7706

Merged
merged 7 commits into from
Nov 13, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
@@ -617,6 +617,19 @@ int String::indexOf(char ch, unsigned int fromIndex) const {
return temp - buffer();
}

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);
if (found == NULL)
return -1;
return found - buffer();
}

int String::indexOf(const String &s2) const {
return indexOf(s2, 0);
}
2 changes: 2 additions & 0 deletions cores/esp8266/WString.h
Original file line number Diff line number Diff line change
@@ -235,6 +235,8 @@ 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 String &str) const;
int indexOf(const String &str, unsigned int fromIndex) const;
int lastIndexOf(char ch) const;
2 changes: 2 additions & 0 deletions tests/host/core/test_string.cpp
Original file line number Diff line number Diff line change
@@ -245,6 +245,8 @@ 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("tacos", 1) == -1);
REQUIRE(s.indexOf("tacos") == -1);
REQUIRE(s.indexOf('t', 1) == -1);