diff --git a/cores/esp32/Print.cpp b/cores/esp32/Print.cpp index 269ad733386..53231e60909 100644 --- a/cores/esp32/Print.cpp +++ b/cores/esp32/Print.cpp @@ -74,6 +74,11 @@ 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()); @@ -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); diff --git a/cores/esp32/Print.h b/cores/esp32/Print.h index 57284c49762..7aa1a2d00a2 100644 --- a/cores/esp32/Print.h +++ b/cores/esp32/Print.h @@ -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(ifsh)); } + size_t print(const __FlashStringHelper *); size_t print(const String &); size_t print(const char[]); size_t print(char); @@ -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(ifsh)); } + 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 6f0a4fc68a8..302d7ac9048 100644 --- a/cores/esp32/WString.cpp +++ b/cores/esp32/WString.cpp @@ -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(); @@ -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()) { @@ -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 */ /*********************************************/ @@ -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 */ /*********************************************/ @@ -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(lhs); + if (!a.concat(rhs)) + a.invalidate(); + return a; +} + /*********************************************/ /* Comparison */ /*********************************************/ diff --git a/cores/esp32/WString.h b/cores/esp32/WString.h index 261868bc9aa..c0847a801da 100644 --- a/cores/esp32/WString.h +++ b/cores/esp32/WString.h @@ -31,11 +31,11 @@ #include -// 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(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. @@ -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(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(str)) {} + String(const __FlashStringHelper *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) {return *this = reinterpret_cast(str);} + String & operator = (const __FlashStringHelper *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(reinterpret_cast(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); @@ -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(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) @@ -180,7 +180,10 @@ class String { concat(num); return (*this); } - String & operator += (const __FlashStringHelper *str) {return *this += reinterpret_cast(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); @@ -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); @@ -225,7 +229,7 @@ class String { return this->startsWith(String(prefix)); } bool startsWith(const __FlashStringHelper *prefix) const { - return this->startsWith(reinterpret_cast(prefix)); + return this->startsWith(String(prefix)); } bool startsWith(const String &prefix, unsigned int offset) const; bool endsWith(const String &suffix) const; @@ -233,7 +237,7 @@ class String { return this->endsWith(String(suffix)); } bool endsWith(const __FlashStringHelper * suffix) const { - return this->endsWith(reinterpret_cast(suffix)); + return this->endsWith(String(suffix)); } // character access @@ -272,16 +276,16 @@ class String { this->replace(String(find), replace); } void replace(const __FlashStringHelper *find, const String &replace) { - this->replace(reinterpret_cast(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(find), String(replace)); + this->replace(String(find), String(replace)); } void replace(const __FlashStringHelper *find, const __FlashStringHelper *replace) { - this->replace(reinterpret_cast(find), reinterpret_cast(replace)); + this->replace(String(find), String(replace)); } void remove(unsigned int index); void remove(unsigned int index, unsigned int count); @@ -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(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(sso.buff) : ptr.buff; } // Writable version of buffer protected: @@ -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(pstr), length); - } + String & copy(const __FlashStringHelper *pstr, unsigned int length); #ifdef __GXX_EXPERIMENTAL_CXX0X__ void move(String &rhs); #endif @@ -393,10 +395,6 @@ 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 1a4bac9dff3..d924a0bd0b1 100644 --- a/libraries/WebServer/src/Uri.h +++ b/libraries/WebServer/src/Uri.h @@ -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 {