diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 6e6dc94beb..8184224957 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -273,6 +273,13 @@ String &String::operator =(const __FlashStringHelper *pstr) { return *this; } +String &String::operator =(char c) { + char buffer[2] { c, '\0' }; + *this = buffer; + return *this; +} + + /*********************************************/ /* concat */ /*********************************************/ @@ -500,6 +507,10 @@ bool String::equals(const char *cstr) const { return strcmp(buffer(), cstr) == 0; } +bool String::equals(const __FlashStringHelper *s) const { + return equals(String(s)); +} + bool String::operator<(const String &rhs) const { return compareTo(rhs) < 0; } @@ -532,6 +543,10 @@ bool String::equalsIgnoreCase(const String &s2) const { return true; } +bool String::equalsIgnoreCase(const __FlashStringHelper *s) const { + return equalsIgnoreCase(String(s)); +} + unsigned char String::equalsConstantTime(const String &s2) const { // To avoid possible time-based attacks present function // compares given strings in a constant time. @@ -565,18 +580,37 @@ bool String::startsWith(const String &s2) const { return startsWith(s2, 0); } +bool String::startsWith(const char *prefix) const { + return this->startsWith(String(prefix)); +} +bool String::startsWith(const __FlashStringHelper *prefix) const { + return this->startsWith(String(prefix)); +} + bool String::startsWith(const String &s2, unsigned int offset) const { if (offset > (unsigned)(len() - s2.len()) || !buffer() || !s2.buffer()) return false; return strncmp(&buffer()[offset], s2.buffer(), s2.len()) == 0; } +bool String::startsWith(const __FlashStringHelper *prefix, unsigned int offset) const { + return startsWith(String(prefix), offset); +} + bool String::endsWith(const String &s2) const { if (len() < s2.len() || !buffer() || !s2.buffer()) return false; return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0; } +bool String::endsWith(const char *suffix) const { + return this->endsWith(String(suffix)); +} +bool String::endsWith(const __FlashStringHelper *suffix) const { + return this->endsWith(String(suffix)); +} + + /*********************************************/ /* Character Access */ /*********************************************/ @@ -678,6 +712,15 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const { return found; } +int String::lastIndexOf(const __FlashStringHelper *str) const { + return lastIndexOf(String(str)); +} + +int String::lastIndexOf(const __FlashStringHelper *str, unsigned int fromIndex) const { + return lastIndexOf(String(str), fromIndex); +} + + String String::substring(unsigned int left, unsigned int right) const { if (left > right) { unsigned int temp = right; @@ -756,6 +799,24 @@ void String::replace(const String &find, const String &replace) { } } + +void String::replace(const char *find, const String &replace) { + this->replace(String(find), replace); +} +void String::replace(const __FlashStringHelper *find, const String &replace) { + this->replace(String(find), replace); +} +void String::replace(const char *find, const char *replace) { + this->replace(String(find), String(replace)); +} +void String::replace(const __FlashStringHelper *find, const char *replace) { + this->replace(String(find), String(replace)); +} +void String::replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) { + this->replace(String(find), String(replace)); +} + + void String::remove(unsigned int index, unsigned int count) { if (index >= len()) { return; diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 37bb3becb6..ad35a0cb4a 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -101,11 +101,7 @@ class String { String &operator =(const char *cstr); String &operator =(const __FlashStringHelper *str); String &operator =(String &&rval) noexcept; - String &operator =(char c) { - char buffer[2] { c, '\0' }; - *this = buffer; - return *this; - } + String &operator =(char c); // concatenate (works w/ built-in types) @@ -142,39 +138,40 @@ class String { int compareTo(const String &s) const; bool equals(const String &s) const; bool equals(const char *cstr) const; + bool equals(const __FlashStringHelper *s) const; bool operator ==(const String &rhs) const { return equals(rhs); } bool operator ==(const char *cstr) const { return equals(cstr); } + bool operator ==(const __FlashStringHelper *rhs) const { + return equals(rhs); + } bool operator !=(const String &rhs) const { return !equals(rhs); } bool operator !=(const char *cstr) const { return !equals(cstr); } + bool operator !=(const __FlashStringHelper *rhs) const { + return !equals(rhs); + } bool operator <(const String &rhs) const; bool operator >(const String &rhs) const; bool operator <=(const String &rhs) const; bool operator >=(const String &rhs) const; bool equalsIgnoreCase(const String &s) const; + bool equalsIgnoreCase(const __FlashStringHelper *s) const; unsigned char equalsConstantTime(const String &s) const; bool startsWith(const String &prefix) const; - bool startsWith(const char *prefix) const { - return this->startsWith(String(prefix)); - } - bool startsWith(const __FlashStringHelper *prefix) const { - return this->startsWith(String(prefix)); - } + bool startsWith(const char *prefix) const; + bool startsWith(const __FlashStringHelper *prefix) const; bool startsWith(const String &prefix, unsigned int offset) const; + bool startsWith(const __FlashStringHelper *prefix, unsigned int offset) const; bool endsWith(const String &suffix) const; - bool endsWith(const char *suffix) const { - return this->endsWith(String(suffix)); - } - bool endsWith(const __FlashStringHelper *suffix) const { - return this->endsWith(String(suffix)); - } + bool endsWith(const char *suffix) const; + bool endsWith(const __FlashStringHelper *suffix) const; // character access char charAt(unsigned int index) const { @@ -204,6 +201,8 @@ class String { int lastIndexOf(char ch, unsigned int fromIndex) const; int lastIndexOf(const String &str) const; int lastIndexOf(const String &str, unsigned int fromIndex) const; + int lastIndexOf(const __FlashStringHelper *str) const; + int lastIndexOf(const __FlashStringHelper *str, unsigned int fromIndex) const; String substring(unsigned int beginIndex) const { return substring(beginIndex, len()); } @@ -212,21 +211,12 @@ class String { // modification void replace(char find, char replace); void replace(const String &find, const String &replace); - void replace(const char *find, const String &replace) { - this->replace(String(find), replace); - } - void replace(const __FlashStringHelper *find, const String &replace) { - this->replace(String(find), replace); - } - void replace(const char *find, const char *replace) { - this->replace(String(find), String(replace)); - } - void replace(const __FlashStringHelper *find, const char *replace) { - this->replace(String(find), String(replace)); - } - void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) { - this->replace(String(find), String(replace)); - } + void replace(const char *find, const String &replace); + void replace(const __FlashStringHelper *find, const String &replace); + void replace(const char *find, const char *replace); + void replace(const __FlashStringHelper *find, const char *replace); + void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace); + // Pass the biggest integer if the count is not specified. // The remove method below will take care of truncating it at the end of the string. void remove(unsigned int index, unsigned int count = (unsigned int)-1);