Skip to content

Commit 0ac0dcf

Browse files
committed
Adding F("foo") syntax for flash strings.
1 parent e3c7a54 commit 0ac0dcf

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

hardware/arduino/cores/arduino/Print.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ void Print::write(const uint8_t *buffer, size_t size)
4343
write(*buffer++);
4444
}
4545

46+
void Print::print(const __FlashStringHelper *ifsh)
47+
{
48+
const prog_char *p = (const prog_char *)ifsh;
49+
while (1) {
50+
unsigned char c = pgm_read_byte(p++);
51+
if (c == 0) return;
52+
write(c);
53+
}
54+
}
55+
4656
void Print::print(const String &s)
4757
{
4858
for (int i = 0; i < s.length(); i++) {
@@ -101,10 +111,16 @@ void Print::print(double n, int digits)
101111
printFloat(n, digits);
102112
}
103113

114+
void Print::println(const __FlashStringHelper *ifsh)
115+
{
116+
print(ifsh);
117+
println();
118+
}
119+
104120
void Print::println(void)
105121
{
106122
print('\r');
107-
print('\n');
123+
print('\n');
108124
}
109125

110126
void Print::println(const String &s)

hardware/arduino/cores/arduino/Print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Print
4040
virtual void write(const char *str);
4141
virtual void write(const uint8_t *buffer, size_t size);
4242

43+
void print(const __FlashStringHelper *);
4344
void print(const String &);
4445
void print(const char[]);
4546
void print(char);
@@ -50,6 +51,7 @@ class Print
5051
void print(unsigned long, int = DEC);
5152
void print(double, int = 2);
5253

54+
void println(const __FlashStringHelper *);
5355
void println(const String &s);
5456
void println(const char[]);
5557
void println(char);

hardware/arduino/cores/arduino/WString.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
// -felide-constructors
3535
// -std=c++0x
3636

37+
class __FlashStringHelper;
38+
#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))
39+
3740
// An inherited class for holding the result of a concatenation. These
3841
// result objects are assumed to be writable by subsequent concatenations.
3942
class StringSumHelper;
@@ -51,8 +54,8 @@ class String
5154
// constructors
5255
// creates a copy of the initial value.
5356
// if the initial value is null or invalid, or if memory allocation
54-
// fails, the string will be marked as invalid (i.e. operator bool()
55-
// will return false).
57+
// fails, the string will be marked as invalid (i.e. "if (s)" will
58+
// be false).
5659
String(const char *cstr = "");
5760
String(const String &str);
5861
#ifdef __GXX_EXPERIMENTAL_CXX0X__
@@ -70,13 +73,13 @@ class String
7073
// memory management
7174
// return true on success, false on failure (in which case, the string
7275
// is left unchanged). reserve(0), if successful, will validate an
73-
// invalid string (i.e., operator bool() will return true afterwards)
76+
// invalid string (i.e., "if (s)" will be true afterwards)
7477
unsigned char reserve(unsigned int size);
7578
inline unsigned int length(void) const {return len;}
7679

7780
// creates a copy of the assigned value. if the value is null or
7881
// invalid, or if the memory allocation fails, the string will be
79-
// marked as invalid (operator bool() will return false).
82+
// marked as invalid ("if (s)" will be false).
8083
String & operator = (const String &rhs);
8184
String & operator = (const char *cstr);
8285
#ifdef __GXX_EXPERIMENTAL_CXX0X__

0 commit comments

Comments
 (0)