Skip to content

Commit

Permalink
Revert "Completely inline the helper pure abstract __FlashStringHelpe…
Browse files Browse the repository at this point in the history
…r class (espressif#7941)" (#210)

This reverts commit b98255d.
  • Loading branch information
Jason2866 authored Apr 13, 2023
1 parent 4b73de3 commit 9eec287
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 26 deletions.
12 changes: 12 additions & 0 deletions cores/esp32/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ 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 @@ -147,6 +152,13 @@ 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 *ifsh) { return print(reinterpret_cast<const char *>(ifsh)); }
size_t print(const __FlashStringHelper *);
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 *ifsh) { return println(reinterpret_cast<const char *>(ifsh)); }
size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
Expand Down
46 changes: 46 additions & 0 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ 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 @@ -230,6 +235,16 @@ 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 @@ -293,6 +308,15 @@ 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 @@ -400,6 +424,20 @@ 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 @@ -488,6 +526,14 @@ 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: 21 additions & 23 deletions cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
#include <ctype.h>


// A pure abstract class forward used as a means to proide a unique pointer type
// but really is never defined.
// an abstract class used as a means to proide a unique pointer type
// but really has no body
class __FlashStringHelper;
#define FPSTR(pstr_pointer) (pstr_pointer)
#define F(string_literal) (string_literal)
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
#define F(string_literal) (FPSTR(PSTR(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(reinterpret_cast<const char*>(cstr), length) {}
String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {}
#endif
String(const String &str);
String(const __FlashStringHelper *str) : String(reinterpret_cast<const char*>(str)) {}
String(const __FlashStringHelper *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) {return *this = reinterpret_cast<const char*>(str);}
String & operator = (const __FlashStringHelper *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(reinterpret_cast<const char*>(cstr), length);}
bool concat(const uint8_t *cstr, unsigned int length) {return concat((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) {return concat(reinterpret_cast<const char*>(str));}
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)
Expand Down Expand Up @@ -180,7 +180,10 @@ class String {
concat(num);
return (*this);
}
String & operator += (const __FlashStringHelper *str) {return *this += reinterpret_cast<const char*>(str);}
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);
Expand All @@ -192,6 +195,7 @@ 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 @@ -225,15 +229,15 @@ class String {
return this->startsWith(String(prefix));
}
bool startsWith(const __FlashStringHelper *prefix) const {
return this->startsWith(reinterpret_cast<const char*>(prefix));
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(reinterpret_cast<const char*>(suffix));
return this->endsWith(String(suffix));
}

// character access
Expand Down Expand Up @@ -272,16 +276,16 @@ class String {
this->replace(String(find), replace);
}
void replace(const __FlashStringHelper *find, const String &replace) {
this->replace(reinterpret_cast<const char*>(find), 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(reinterpret_cast<const char*>(find), String(replace));
this->replace(String(find), String(replace));
}
void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) {
this->replace(reinterpret_cast<const char*>(find), reinterpret_cast<const char*>(replace));
this->replace(String(find), String(replace));
}
void remove(unsigned int index);
void remove(unsigned int index, unsigned int count);
Expand Down Expand Up @@ -336,7 +340,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 reinterpret_cast<const char *>(isSSO() ? sso.buff : ptr.buff); }
inline const char *buffer() const { return (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 @@ -346,9 +350,7 @@ 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<const char*>(pstr), length);
}
String & copy(const __FlashStringHelper *pstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs);
#endif
Expand Down Expand Up @@ -393,10 +395,6 @@ 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((const char *)uri) {}
Uri(const __FlashStringHelper *uri) : _uri(String(uri)) {}
virtual ~Uri() {}

virtual Uri* clone() const {
Expand Down

0 comments on commit 9eec287

Please sign in to comment.