Completely inline the helper pure abstract __FlashStringHelper class (#7941)

* Remove __FlashStringHelper from ESP32, it's not needed - all the files using it are different from their ESP8266 counterparts anyway.

* Revert removal of class __FlashStringHelper forward for continued compatibility with external libs

* Improved fix, works for libs that return const __FlashStringHelper*

* Inline all wrappers using const __FlashStringHelper*.
This commit is contained in:
Dirk O. Kaar 2023-04-06 12:32:21 +02:00 committed by GitHub
parent 087ebe0ee6
commit b98255d8d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 82 deletions

View File

@ -74,11 +74,6 @@ size_t Print::printf(const char *format, ...)
return len;
}
size_t Print::print(const __FlashStringHelper *ifsh)
{
return print(reinterpret_cast<const char *>(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);

View File

@ -78,7 +78,7 @@ public:
// 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 __FlashStringHelper *ifsh) { return print(reinterpret_cast<const char *>(ifsh)); }
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
@ -93,7 +93,7 @@ public:
size_t print(const Printable&);
size_t print(struct tm * timeinfo, const char * format = NULL);
size_t println(const __FlashStringHelper *);
size_t println(const __FlashStringHelper *ifsh) { return println(reinterpret_cast<const char *>(ifsh)); }
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);

View File

@ -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<StringSumHelper&>(lhs);
if (!a.concat(rhs))
a.invalidate();
return a;
}
/*********************************************/
/* Comparison */
/*********************************************/

View File

@ -31,11 +31,11 @@
#include <ctype.h>
// an abstract class used as a means to proide a unique pointer type
// but really has no body
// A pure abstract class forward used as a means to proide a unique pointer type
// but really is never defined.
class __FlashStringHelper;
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(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.
@ -59,10 +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<const char*>(cstr), length) {}
#endif
String(const String &str);
String(const __FlashStringHelper *str);
String(const __FlashStringHelper *str) : String(reinterpret_cast<const char*>(str)) {}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval);
String(StringSumHelper &&rval);
@ -103,7 +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);
String & operator = (const __FlashStringHelper *str) {return *this = reinterpret_cast<const char*>(str);}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator =(String &&rval);
String & operator =(StringSumHelper &&rval);
@ -117,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<const char*>(cstr), length);}
bool concat(char c);
bool concat(unsigned char c);
bool concat(int num);
@ -128,7 +128,7 @@ class String {
bool concat(double num);
bool concat(long long num);
bool concat(unsigned long long num);
bool concat(const __FlashStringHelper * str);
bool concat(const __FlashStringHelper * str) {return concat(reinterpret_cast<const char*>(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 +180,7 @@ class String {
concat(num);
return (*this);
}
String & operator += (const __FlashStringHelper *str){
concat(str);
return (*this);
}
String & operator += (const __FlashStringHelper *str) {return *this += reinterpret_cast<const char*>(str);}
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
@ -195,7 +192,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);
@ -229,7 +225,7 @@ class String {
return this->startsWith(String(prefix));
}
bool startsWith(const __FlashStringHelper *prefix) const {
return this->startsWith(String(prefix));
return this->startsWith(reinterpret_cast<const char*>(prefix));
}
bool startsWith(const String &prefix, unsigned int offset) const;
bool endsWith(const String &suffix) const;
@ -237,7 +233,7 @@ class String {
return this->endsWith(String(suffix));
}
bool endsWith(const __FlashStringHelper * suffix) const {
return this->endsWith(String(suffix));
return this->endsWith(reinterpret_cast<const char*>(suffix));
}
// character access
@ -276,16 +272,16 @@ class String {
this->replace(String(find), replace);
}
void replace(const __FlashStringHelper *find, const String &replace) {
this->replace(String(find), replace);
this->replace(reinterpret_cast<const char*>(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));
this->replace(reinterpret_cast<const char*>(find), String(replace));
}
void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) {
this->replace(String(find), String(replace));
this->replace(reinterpret_cast<const char*>(find), reinterpret_cast<const char*>(replace));
}
void remove(unsigned int index);
void remove(unsigned int index, unsigned int count);
@ -340,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<const char *>(isSSO() ? sso.buff : ptr.buff); }
inline char *wbuffer() const { return isSSO() ? const_cast<char *>(sso.buff) : ptr.buff; } // Writable version of buffer
protected:
@ -350,7 +346,9 @@ class String {
// copy and move
String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length) {
return copy(reinterpret_cast<const char*>(pstr), length);
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs);
#endif
@ -395,6 +393,10 @@ class StringSumHelper: public String {
String(num) {
}
};
inline StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs) {
return lhs + reinterpret_cast<const char*>(rhs);
}
extern const String emptyString;

View File

@ -12,7 +12,7 @@ class Uri {
public:
Uri(const char *uri) : _uri(uri) {}
Uri(const String &uri) : _uri(uri) {}
Uri(const __FlashStringHelper *uri) : _uri(String(uri)) {}
Uri(const __FlashStringHelper *uri) : _uri((const char *)uri) {}
virtual ~Uri() {}
virtual Uri* clone() const {