Skip to content

Commit

Permalink
Completely inline the helper pure abstract __FlashStringHelper class (#…
Browse files Browse the repository at this point in the history
…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*.
  • Loading branch information
dok-net committed Apr 6, 2023
1 parent 087ebe0 commit b98255d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 82 deletions.
12 changes: 0 additions & 12 deletions cores/esp32/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions cores/esp32/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +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 *);
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);
Expand All @@ -93,7 +93,7 @@ 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 __FlashStringHelper *ifsh) { return println(reinterpret_cast<const char *>(ifsh)); }
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
Expand Down
46 changes: 0 additions & 46 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 */
/*********************************************/
Expand Down Expand Up @@ -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 */
/*********************************************/
Expand Down Expand Up @@ -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 */
/*********************************************/
Expand Down
44 changes: 23 additions & 21 deletions cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -229,15 +225,15 @@ 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;
bool endsWith(const char *suffix) const {
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion libraries/WebServer/src/Uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b98255d

Please sign in to comment.