From eca04bac8e7fa524cff0e054255dd944a7bfde7f Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 8 Mar 2023 13:31:45 +0100 Subject: [PATCH 1/4] Remove __FlashStringHelper from ESP32, it's not needed - all the files using it are different from their ESP8266 counterparts anyway. --- cores/esp32/Print.cpp | 12 --------- cores/esp32/Print.h | 2 -- cores/esp32/WString.cpp | 46 ----------------------------------- cores/esp32/WString.h | 31 ++--------------------- libraries/WebServer/src/Uri.h | 1 - 5 files changed, 2 insertions(+), 90 deletions(-) diff --git a/cores/esp32/Print.cpp b/cores/esp32/Print.cpp index 53231e60909..269ad733386 100644 --- a/cores/esp32/Print.cpp +++ b/cores/esp32/Print.cpp @@ -74,11 +74,6 @@ size_t Print::printf(const char *format, ...) return len; } -size_t Print::print(const __FlashStringHelper *ifsh) -{ - return print(reinterpret_cast(ifsh)); -} - size_t Print::print(const String &s) { return write(s.c_str(), s.length()); @@ -152,13 +147,6 @@ size_t Print::print(double n, int digits) return printFloat(n, digits); } -size_t Print::println(const __FlashStringHelper *ifsh) -{ - size_t n = print(ifsh); - n += println(); - return n; -} - size_t Print::print(const Printable& x) { return x.printTo(*this); diff --git a/cores/esp32/Print.h b/cores/esp32/Print.h index 7aa1a2d00a2..9886ca87472 100644 --- a/cores/esp32/Print.h +++ b/cores/esp32/Print.h @@ -78,7 +78,6 @@ class Print // default to zero, meaning "a single write may block" // should be overriden by subclasses with buffering virtual int availableForWrite() { return 0; } - size_t print(const __FlashStringHelper *); size_t print(const String &); size_t print(const char[]); size_t print(char); @@ -93,7 +92,6 @@ class Print size_t print(const Printable&); size_t print(struct tm * timeinfo, const char * format = NULL); - size_t println(const __FlashStringHelper *); size_t println(const String &s); size_t println(const char[]); size_t println(char); diff --git a/cores/esp32/WString.cpp b/cores/esp32/WString.cpp index 302d7ac9048..6f0a4fc68a8 100644 --- a/cores/esp32/WString.cpp +++ b/cores/esp32/WString.cpp @@ -47,11 +47,6 @@ String::String(const String &value) { *this = value; } -String::String(const __FlashStringHelper *pstr) { - init(); - *this = pstr; // see operator = -} - #ifdef __GXX_EXPERIMENTAL_CXX0X__ String::String(String &&rval) { init(); @@ -235,16 +230,6 @@ String & String::copy(const char *cstr, unsigned int length) { return *this; } -String & String::copy(const __FlashStringHelper *pstr, unsigned int length) { - if (!reserve(length)) { - invalidate(); - return *this; - } - memcpy_P(wbuffer(), (PGM_P)pstr, length + 1); // We know wbuffer() cannot ever be in PROGMEM, so memcpy safe here - setLen(length); - return *this; -} - #ifdef __GXX_EXPERIMENTAL_CXX0X__ void String::move(String &rhs) { if(buffer()) { @@ -308,15 +293,6 @@ String & String::operator =(const char *cstr) { return *this; } -String & String::operator =(const __FlashStringHelper *pstr) { - if(pstr) - copy(pstr, strlen_P((PGM_P)pstr)); - else - invalidate(); - - return *this; -} - /*********************************************/ /* concat */ /*********************************************/ @@ -424,20 +400,6 @@ bool String::concat(double num) { return concat(string, strlen(string)); } -bool String::concat(const __FlashStringHelper * str) { - if (!str) - return false; - int length = strlen_P((PGM_P)str); - if (length == 0) - return true; - unsigned int newlen = len() + length; - if (!reserve(newlen)) - return false; - memcpy_P(wbuffer() + len(), (PGM_P)str, length + 1); - setLen(newlen); - return true; -} - /*********************************************/ /* Concatenate */ /*********************************************/ @@ -526,14 +488,6 @@ StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long long num) return a; } -StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) -{ - StringSumHelper &a = const_cast(lhs); - if (!a.concat(rhs)) - a.invalidate(); - return a; -} - /*********************************************/ /* Comparison */ /*********************************************/ diff --git a/cores/esp32/WString.h b/cores/esp32/WString.h index c0847a801da..55498a53bd0 100644 --- a/cores/esp32/WString.h +++ b/cores/esp32/WString.h @@ -31,11 +31,8 @@ #include -// an abstract class used as a means to proide a unique pointer type -// but really has no body -class __FlashStringHelper; -#define FPSTR(pstr_pointer) (reinterpret_cast(pstr_pointer)) -#define F(string_literal) (FPSTR(PSTR(string_literal))) +#define FPSTR(pstr_pointer) (pstr_pointer) +#define F(string_literal) (string_literal) // An inherited class for holding the result of a concatenation. These // result objects are assumed to be writable by subsequent concatenations. @@ -62,7 +59,6 @@ class String { String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {} #endif String(const String &str); - String(const __FlashStringHelper *str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String(String &&rval); String(StringSumHelper &&rval); @@ -103,7 +99,6 @@ class String { // marked as invalid ("if (s)" will be false). String & operator =(const String &rhs); String & operator =(const char *cstr); - String & operator = (const __FlashStringHelper *str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String & operator =(String &&rval); String & operator =(StringSumHelper &&rval); @@ -128,7 +123,6 @@ class String { bool concat(double num); bool concat(long long num); bool concat(unsigned long long num); - bool concat(const __FlashStringHelper * str); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) @@ -180,10 +174,6 @@ class String { concat(num); return (*this); } - String & operator += (const __FlashStringHelper *str){ - concat(str); - return (*this); - } friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs); friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr); @@ -195,7 +185,6 @@ class String { friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num); friend StringSumHelper & operator +(const StringSumHelper &lhs, float num); friend StringSumHelper & operator +(const StringSumHelper &lhs, double num); - friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs); friend StringSumHelper & operator +(const StringSumHelper &lhs, long long num); friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long long num); @@ -228,17 +217,11 @@ class String { 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 String &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)); - } // character access char charAt(unsigned int index) const; @@ -275,18 +258,9 @@ class String { 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 remove(unsigned int index); void remove(unsigned int index, unsigned int count); void toLowerCase(void); @@ -350,7 +324,6 @@ class String { // copy and move String & copy(const char *cstr, unsigned int length); - String & copy(const __FlashStringHelper *pstr, unsigned int length); #ifdef __GXX_EXPERIMENTAL_CXX0X__ void move(String &rhs); #endif diff --git a/libraries/WebServer/src/Uri.h b/libraries/WebServer/src/Uri.h index d924a0bd0b1..83772b2348e 100644 --- a/libraries/WebServer/src/Uri.h +++ b/libraries/WebServer/src/Uri.h @@ -12,7 +12,6 @@ class Uri { public: Uri(const char *uri) : _uri(uri) {} Uri(const String &uri) : _uri(uri) {} - Uri(const __FlashStringHelper *uri) : _uri(String(uri)) {} virtual ~Uri() {} virtual Uri* clone() const { From 5da45f12b83caba1a89d8927bf308ddf4e69a4ca Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 8 Mar 2023 23:13:37 +0100 Subject: [PATCH 2/4] Revert removal of class __FlashStringHelper forward for continued compatibility with external libs --- cores/esp32/WString.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cores/esp32/WString.h b/cores/esp32/WString.h index 55498a53bd0..067cc242db5 100644 --- a/cores/esp32/WString.h +++ b/cores/esp32/WString.h @@ -31,6 +31,10 @@ #include +// A pure abstract class forward used as a means to proide a unique pointer type +// but really is never defined. +class [[deprecated("not required on ESP32, only for ESP8266 compatibility")]] + __FlashStringHelper; #define FPSTR(pstr_pointer) (pstr_pointer) #define F(string_literal) (string_literal) From 6577b4036c5f6a8b1a6049f907bdf08f22a65ee8 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 9 Mar 2023 00:36:30 +0100 Subject: [PATCH 3/4] Improved fix, works for libs that return const __FlashStringHelper* --- cores/esp32/WString.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/WString.h b/cores/esp32/WString.h index 067cc242db5..b64c634e213 100644 --- a/cores/esp32/WString.h +++ b/cores/esp32/WString.h @@ -33,8 +33,8 @@ // A pure abstract class forward used as a means to proide a unique pointer type // but really is never defined. -class [[deprecated("not required on ESP32, only for ESP8266 compatibility")]] - __FlashStringHelper; +typedef [[deprecated("not required on ESP32, only for compatibility")]] + char __FlashStringHelper; #define FPSTR(pstr_pointer) (pstr_pointer) #define F(string_literal) (string_literal) From 51fbf472e97fe1dbb994b882fa4f6134621f44dd Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 9 Mar 2023 01:38:46 +0100 Subject: [PATCH 4/4] Inline all wrappers using const __FlashStringHelper*. --- cores/esp32/Print.h | 2 ++ cores/esp32/WString.h | 35 ++++++++++++++++++++++++++++++----- libraries/WebServer/src/Uri.h | 1 + 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cores/esp32/Print.h b/cores/esp32/Print.h index 9886ca87472..57284c49762 100644 --- a/cores/esp32/Print.h +++ b/cores/esp32/Print.h @@ -78,6 +78,7 @@ class Print // default to zero, meaning "a single write may block" // should be overriden by subclasses with buffering virtual int availableForWrite() { return 0; } + size_t print(const __FlashStringHelper *ifsh) { return print(reinterpret_cast(ifsh)); } size_t print(const String &); size_t print(const char[]); size_t print(char); @@ -92,6 +93,7 @@ class Print size_t print(const Printable&); size_t print(struct tm * timeinfo, const char * format = NULL); + size_t println(const __FlashStringHelper *ifsh) { return println(reinterpret_cast(ifsh)); } size_t println(const String &s); size_t println(const char[]); size_t println(char); diff --git a/cores/esp32/WString.h b/cores/esp32/WString.h index b64c634e213..261868bc9aa 100644 --- a/cores/esp32/WString.h +++ b/cores/esp32/WString.h @@ -33,8 +33,7 @@ // A pure abstract class forward used as a means to proide a unique pointer type // but really is never defined. -typedef [[deprecated("not required on ESP32, only for compatibility")]] - char __FlashStringHelper; +class __FlashStringHelper; #define FPSTR(pstr_pointer) (pstr_pointer) #define F(string_literal) (string_literal) @@ -60,9 +59,10 @@ class String { String(const char *cstr = ""); String(const char *cstr, unsigned int length); #ifdef __GXX_EXPERIMENTAL_CXX0X__ - String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {} + String(const uint8_t *cstr, unsigned int length) : String(reinterpret_cast(cstr), length) {} #endif String(const String &str); + String(const __FlashStringHelper *str) : String(reinterpret_cast(str)) {} #ifdef __GXX_EXPERIMENTAL_CXX0X__ String(String &&rval); String(StringSumHelper &&rval); @@ -103,6 +103,7 @@ class String { // marked as invalid ("if (s)" will be false). String & operator =(const String &rhs); String & operator =(const char *cstr); + String & operator = (const __FlashStringHelper *str) {return *this = reinterpret_cast(str);} #ifdef __GXX_EXPERIMENTAL_CXX0X__ String & operator =(String &&rval); String & operator =(StringSumHelper &&rval); @@ -116,7 +117,7 @@ class String { bool concat(const String &str); bool concat(const char *cstr); bool concat(const char *cstr, unsigned int length); - bool concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);} + bool concat(const uint8_t *cstr, unsigned int length) {return concat(reinterpret_cast(cstr), length);} bool concat(char c); bool concat(unsigned char c); bool concat(int num); @@ -127,6 +128,7 @@ class String { bool concat(double num); bool concat(long long num); bool concat(unsigned long long num); + bool concat(const __FlashStringHelper * str) {return concat(reinterpret_cast(str));} // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) @@ -178,6 +180,7 @@ class String { concat(num); return (*this); } + String & operator += (const __FlashStringHelper *str) {return *this += reinterpret_cast(str);} friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs); friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr); @@ -221,11 +224,17 @@ class String { bool startsWith(const char *prefix) const { return this->startsWith(String(prefix)); } + bool startsWith(const __FlashStringHelper *prefix) const { + return this->startsWith(reinterpret_cast(prefix)); + } bool startsWith(const String &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(reinterpret_cast(suffix)); + } // character access char charAt(unsigned int index) const; @@ -262,9 +271,18 @@ class String { void replace(const char *find, const String &replace) { this->replace(String(find), replace); } + void replace(const __FlashStringHelper *find, const String &replace) { + this->replace(reinterpret_cast(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(reinterpret_cast(find), String(replace)); + } + void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) { + this->replace(reinterpret_cast(find), reinterpret_cast(replace)); + } void remove(unsigned int index); void remove(unsigned int index, unsigned int count); void toLowerCase(void); @@ -318,7 +336,7 @@ class String { inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; } inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; } // Buffer accessor functions - inline const char *buffer() const { return (const char *)(isSSO() ? sso.buff : ptr.buff); } + inline const char *buffer() const { return reinterpret_cast(isSSO() ? sso.buff : ptr.buff); } inline char *wbuffer() const { return isSSO() ? const_cast(sso.buff) : ptr.buff; } // Writable version of buffer protected: @@ -328,6 +346,9 @@ class String { // copy and move String & copy(const char *cstr, unsigned int length); + String & copy(const __FlashStringHelper *pstr, unsigned int length) { + return copy(reinterpret_cast(pstr), length); + } #ifdef __GXX_EXPERIMENTAL_CXX0X__ void move(String &rhs); #endif @@ -372,6 +393,10 @@ class StringSumHelper: public String { String(num) { } }; + +inline StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs) { + return lhs + reinterpret_cast(rhs); +} extern const String emptyString; diff --git a/libraries/WebServer/src/Uri.h b/libraries/WebServer/src/Uri.h index 83772b2348e..1a4bac9dff3 100644 --- a/libraries/WebServer/src/Uri.h +++ b/libraries/WebServer/src/Uri.h @@ -12,6 +12,7 @@ class Uri { public: Uri(const char *uri) : _uri(uri) {} Uri(const String &uri) : _uri(uri) {} + Uri(const __FlashStringHelper *uri) : _uri((const char *)uri) {} virtual ~Uri() {} virtual Uri* clone() const {