Skip to content

Commit 30bc3b2

Browse files
committed
Fixes implementation of __FlashStringHelper (#183)
Previous __FlashStringHelper implementation was defines as a char which brought problem in case the method with char* parameter used overloading with __FlashStringHelper* parameter (they was identical). Now __FlashStringHelper is defined as a class and all casts between char* and __FlashStringHelper* are made with reinterpret_cast sugar.
1 parent 648c2e8 commit 30bc3b2

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

cores/esp32/Print.cpp

+6-13
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,12 @@ size_t Print::printf(const char *format, ...)
6868
}
6969
return len;
7070
}
71-
/*
72-
size_t Print::print(const __FlashStringHelper *ifsh) {
73-
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
7471

75-
size_t n = 0;
76-
while (1) {
77-
uint8_t c = pgm_read_byte(p++);
78-
if (c == 0) break;
79-
n += write(c);
80-
}
81-
return n;
72+
size_t Print::print(const __FlashStringHelper *ifsh)
73+
{
74+
return print(reinterpret_cast<const char *>(ifsh));
8275
}
83-
*/
76+
8477
size_t Print::print(const String &s)
8578
{
8679
return write(s.c_str(), s.length());
@@ -140,14 +133,14 @@ size_t Print::print(double n, int digits)
140133
{
141134
return printFloat(n, digits);
142135
}
143-
/*
136+
144137
size_t Print::println(const __FlashStringHelper *ifsh)
145138
{
146139
size_t n = print(ifsh);
147140
n += println();
148141
return n;
149142
}
150-
*/
143+
151144
size_t Print::print(const Printable& x)
152145
{
153146
return x.printTo(*this);

cores/esp32/Print.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Print
7272
}
7373

7474
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
75-
//size_t print(const __FlashStringHelper *);
75+
size_t print(const __FlashStringHelper *);
7676
size_t print(const String &);
7777
size_t print(const char[]);
7878
size_t print(char);
@@ -85,7 +85,7 @@ class Print
8585
size_t print(const Printable&);
8686
size_t print(struct tm * timeinfo, const char * format = NULL);
8787

88-
//size_t println(const __FlashStringHelper *);
88+
size_t println(const __FlashStringHelper *);
8989
size_t println(const String &s);
9090
size_t println(const char[]);
9191
size_t println(char);

cores/esp32/WString.cpp

+15-31
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ String::String(const String &value)
4444
init();
4545
*this = value;
4646
}
47-
/*
48-
String::String(const __FlashStringHelper *pstr) {
49-
init();
50-
*this = pstr; // see operator =
51-
}
52-
*/
47+
5348
#ifdef __GXX_EXPERIMENTAL_CXX0X__
5449
String::String(String &&rval)
5550
{
@@ -200,17 +195,12 @@ String & String::copy(const char *cstr, unsigned int length)
200195
strcpy(buffer, cstr);
201196
return *this;
202197
}
203-
/*
204-
String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
205-
if (!reserve(length)) {
206-
invalidate();
207-
return *this;
208-
}
209-
len = length;
210-
strcpy_P(buffer, (PGM_P)pstr);
211-
return *this;
198+
199+
String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
200+
{
201+
return copy(reinterpret_cast<const char *>(pstr), length);
212202
}
213-
*/
203+
214204
#ifdef __GXX_EXPERIMENTAL_CXX0X__
215205
void String::move(String &rhs)
216206
{
@@ -276,15 +266,15 @@ String & String::operator =(const char *cstr)
276266

277267
return *this;
278268
}
279-
/*
269+
280270
String & String::operator = (const __FlashStringHelper *pstr)
281271
{
282272
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
283273
else invalidate();
284274

285275
return *this;
286276
}
287-
*/
277+
288278
// /*********************************************/
289279
// /* concat */
290280
// /*********************************************/
@@ -375,18 +365,12 @@ unsigned char String::concat(double num)
375365
char* string = dtostrf(num, 4, 2, buf);
376366
return concat(string, strlen(string));
377367
}
378-
/*
379-
unsigned char String::concat(const __FlashStringHelper * str) {
380-
if (!str) return 0;
381-
int length = strlen_P((PGM_P)str);
382-
if (length == 0) return 1;
383-
unsigned int newlen = len + length;
384-
if (!reserve(newlen)) return 0;
385-
strcpy_P(buffer + len, (PGM_P)str);
386-
len = newlen;
387-
return 1;
368+
369+
unsigned char String::concat(const __FlashStringHelper * str)
370+
{
371+
return concat(reinterpret_cast<const char *>(str));
388372
}
389-
*/
373+
390374
/*********************************************/
391375
/* Concatenate */
392376
/*********************************************/
@@ -480,14 +464,14 @@ StringSumHelper & operator +(const StringSumHelper &lhs, double num)
480464
}
481465
return a;
482466
}
483-
/*
467+
484468
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
485469
{
486470
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
487471
if (!a.concat(rhs)) a.invalidate();
488472
return a;
489473
}
490-
*/
474+
491475
// /*********************************************/
492476
// /* Comparison */
493477
// /*********************************************/

cores/esp32/WString.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class StringSumHelper;
3434

3535
// an abstract class used as a means to proide a unique pointer type
3636
// but really has no body
37-
//class __FlashStringHelper;
37+
class __FlashStringHelper;
38+
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
3839

3940
// The string class
4041
class String
@@ -55,7 +56,7 @@ class String
5556
// be false).
5657
String(const char *cstr = "");
5758
String(const String &str);
58-
//String(const __FlashStringHelper *str);
59+
String(const __FlashStringHelper *str) : String(reinterpret_cast<const char *>(str)) {};
5960
#ifdef __GXX_EXPERIMENTAL_CXX0X__
6061
String(String &&rval);
6162
String(StringSumHelper &&rval);
@@ -89,7 +90,7 @@ class String
8990
// marked as invalid ("if (s)" will be false).
9091
String & operator =(const String &rhs);
9192
String & operator =(const char *cstr);
92-
//String & operator = (const __FlashStringHelper *str);
93+
String & operator = (const __FlashStringHelper *str);
9394
#ifdef __GXX_EXPERIMENTAL_CXX0X__
9495
String & operator =(String &&rval);
9596
String & operator =(StringSumHelper &&rval);
@@ -110,7 +111,7 @@ class String
110111
unsigned char concat(unsigned long num);
111112
unsigned char concat(float num);
112113
unsigned char concat(double num);
113-
//unsigned char concat(const __FlashStringHelper * str);
114+
unsigned char concat(const __FlashStringHelper * str);
114115

115116
// if there's not enough memory for the concatenated value, the string
116117
// will be left unchanged (but this isn't signalled in any way)
@@ -164,10 +165,11 @@ class String
164165
concat(num);
165166
return (*this);
166167
}
167-
//String & operator += (const __FlashStringHelper *str){
168-
// concat(str);
169-
// return (*this);
170-
//}
168+
String & operator += (const __FlashStringHelper *str)
169+
{
170+
concat(str);
171+
return (*this);
172+
}
171173

172174
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
173175
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
@@ -179,7 +181,7 @@ class String
179181
friend StringSumHelper & operator +(const StringSumHelper &lhs, unsigned long num);
180182
friend StringSumHelper & operator +(const StringSumHelper &lhs, float num);
181183
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
182-
//friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
184+
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);
183185

184186
// comparison (only works w/ Strings and "strings")
185187
operator StringIfHelperType() const
@@ -270,7 +272,7 @@ class String
270272

271273
// copy and move
272274
String & copy(const char *cstr, unsigned int length);
273-
//String & copy(const __FlashStringHelper *pstr, unsigned int length);
275+
String & copy(const __FlashStringHelper *pstr, unsigned int length);
274276
#ifdef __GXX_EXPERIMENTAL_CXX0X__
275277
void move(String &rhs);
276278
#endif

cores/esp32/pgmspace.h

-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ typedef unsigned short prog_uint16_t;
2929
typedef long prog_int32_t;
3030
typedef unsigned long prog_uint32_t;
3131

32-
typedef char __FlashStringHelper;
33-
3432
#define SIZE_IRRELEVANT 0x7fffffff
3533

3634
#define PROGMEM
3735
#define PGM_P const char *
3836
#define PGM_VOID_P const void *
3937
#define FPSTR(p) ((const char *)(p))
4038
#define PSTR(s) (s)
41-
#define F(s) (s)
4239
#define _SFR_BYTE(n) (n)
4340

4441
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))

0 commit comments

Comments
 (0)