From 598eddfc6f2d0b358a0e3b29b227eb6afe7cb8c9 Mon Sep 17 00:00:00 2001 From: Paulo Date: Thu, 12 Nov 2020 23:00:36 -0300 Subject: [PATCH 1/7] Provide String::indexOf for char* needle --- cores/esp8266/WString.cpp | 13 +++++++++++++ cores/esp8266/WString.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 056bbeb988..5187ab177d 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -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); } diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index c9b79f21c4..587c1c0b70 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -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; From 543204e5105c65946190d587f624a2105cbdd2a2 Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 13 Nov 2020 01:48:48 -0300 Subject: [PATCH 2/7] Add tests for String::indexOf(char*) --- tests/host/core/test_string.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/host/core/test_string.cpp b/tests/host/core/test_string.cpp index f9fb30a6d5..91961c8eb4 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -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); From c2017d0c3e56b10acee6311356ddd4c2a76f314b Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 13 Nov 2020 02:00:40 -0300 Subject: [PATCH 3/7] Add String::indexOf for __FlashStringHelper pointers strstr_P documentation says only s2 must be from PROGMEM, so we are safe. --- cores/esp8266/WString.cpp | 13 +++++++++++++ cores/esp8266/WString.h | 2 ++ tests/host/core/test_string.cpp | 2 ++ 3 files changed, 17 insertions(+) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 5187ab177d..6adb820074 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -617,6 +617,19 @@ int String::indexOf(char ch, unsigned int fromIndex) const { return temp - buffer(); } +int String::indexOf(const __FlashStringHelper *s2) const { + return indexOf(s2, 0); +} + +int String::indexOf(const __FlashStringHelper *s2, unsigned int fromIndex) const { + if (fromIndex >= len()) + return -1; + const char *found = strstr_P(buffer() + fromIndex, (char *) s2); + if (found == NULL) + return -1; + return found - buffer(); +}} + int String::indexOf(const char *s2) const { return indexOf(s2, 0); } diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 587c1c0b70..262cfda0a9 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -237,6 +237,8 @@ class String { 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 91961c8eb4..1150ba5913 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -247,6 +247,8 @@ TEST_CASE("String nulls", "[core][String]") 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); From 06afd28b1a8fa5fa6512c53fb7afaf6d2ccdd6ad Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 13 Nov 2020 11:18:27 -0300 Subject: [PATCH 4/7] Fix typo --- cores/esp8266/WString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 6adb820074..b9cd899aff 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -628,7 +628,7 @@ int String::indexOf(const __FlashStringHelper *s2, unsigned int fromIndex) const if (found == NULL) return -1; return found - buffer(); -}} +} int String::indexOf(const char *s2) const { return indexOf(s2, 0); From 4b3a6a3ce6d5e4a22ea43043e66bf7d8a2e1bbde Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 13 Nov 2020 12:14:23 -0300 Subject: [PATCH 5/7] Use strstr_P in String::indexOf for char pointers --- cores/esp8266/WString.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index b9cd899aff..b991a0442d 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -622,12 +622,7 @@ int String::indexOf(const __FlashStringHelper *s2) const { } int String::indexOf(const __FlashStringHelper *s2, unsigned int fromIndex) const { - if (fromIndex >= len()) - return -1; - const char *found = strstr_P(buffer() + fromIndex, (char *) s2); - if (found == NULL) - return -1; - return found - buffer(); + return indexOf((char*) s2, fromIndex); } int String::indexOf(const char *s2) const { @@ -637,7 +632,7 @@ int String::indexOf(const char *s2) const { int String::indexOf(const char *s2, unsigned int fromIndex) const { if (fromIndex >= len()) return -1; - const char *found = strstr(buffer() + fromIndex, s2); + const char *found = strstr_P(buffer() + fromIndex, s2); if (found == NULL) return -1; return found - buffer(); From e4665566e510df3036056874266e978597545f6c Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 13 Nov 2020 12:16:40 -0300 Subject: [PATCH 6/7] Make String::indexOf with String needle use char pointer version --- cores/esp8266/WString.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index b991a0442d..19508d38e0 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -643,12 +643,7 @@ int String::indexOf(const String &s2) const { } int String::indexOf(const String &s2, unsigned int fromIndex) const { - if (fromIndex >= len()) - return -1; - const char *found = strstr(buffer() + fromIndex, s2.buffer()); - if (found == NULL) - return -1; - return found - buffer(); + return indexOf(s2.c_str(), fromIndex); } int String::lastIndexOf(char theChar) const { From dc696e0910d0ffbaf3266fcd07794b349832b1d5 Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 13 Nov 2020 16:21:53 -0300 Subject: [PATCH 7/7] Fix pointer cast to const --- cores/esp8266/WString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 19508d38e0..3dda69bda4 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -622,7 +622,7 @@ int String::indexOf(const __FlashStringHelper *s2) const { } int String::indexOf(const __FlashStringHelper *s2, unsigned int fromIndex) const { - return indexOf((char*) s2, fromIndex); + return indexOf((const char*) s2, fromIndex); } int String::indexOf(const char *s2) const {