1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

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

This commit is contained in:
Paulo Cabral Sanz 2020-11-13 17:13:32 -03:00 committed by GitHub
parent 8375faa542
commit c5c9f845d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -617,17 +617,33 @@ int String::indexOf(char ch, unsigned int fromIndex) const {
return temp - buffer(); return temp - buffer();
} }
int String::indexOf(const __FlashStringHelper *s2) const {
return indexOf(s2, 0);
}
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_P(buffer() + fromIndex, s2);
if (found == NULL)
return -1;
return found - buffer();
}
int String::indexOf(const String &s2) const { int String::indexOf(const String &s2) const {
return indexOf(s2, 0); return indexOf(s2, 0);
} }
int String::indexOf(const String &s2, unsigned int fromIndex) const { int String::indexOf(const String &s2, unsigned int fromIndex) const {
if (fromIndex >= len()) return indexOf(s2.c_str(), fromIndex);
return -1;
const char *found = strstr(buffer() + fromIndex, s2.buffer());
if (found == NULL)
return -1;
return found - buffer();
} }
int String::lastIndexOf(char theChar) const { int String::lastIndexOf(char theChar) const {

View File

@ -235,6 +235,10 @@ class String {
// search // search
int indexOf(char ch) const; int indexOf(char ch) const;
int indexOf(char ch, unsigned int fromIndex) 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) const;
int indexOf(const String &str, unsigned int fromIndex) const; int indexOf(const String &str, unsigned int fromIndex) const;
int lastIndexOf(char ch) const; int lastIndexOf(char ch) const;

View File

@ -245,6 +245,10 @@ TEST_CASE("String nulls", "[core][String]")
REQUIRE(s.lastIndexOf("tacos") == -1); REQUIRE(s.lastIndexOf("tacos") == -1);
REQUIRE(s.lastIndexOf('t', 0) == -1); REQUIRE(s.lastIndexOf('t', 0) == -1);
REQUIRE(s.lastIndexOf('t') == -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) == -1);
REQUIRE(s.indexOf("tacos") == -1); REQUIRE(s.indexOf("tacos") == -1);
REQUIRE(s.indexOf('t', 1) == -1); REQUIRE(s.indexOf('t', 1) == -1);