Skip to content

Commit

Permalink
Adding F("foo") syntax for flash strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
damellis committed Mar 27, 2011
1 parent e3c7a54 commit 0ac0dcf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
18 changes: 17 additions & 1 deletion hardware/arduino/cores/arduino/Print.cpp
Expand Up @@ -43,6 +43,16 @@ void Print::write(const uint8_t *buffer, size_t size)
write(*buffer++); write(*buffer++);
} }


void Print::print(const __FlashStringHelper *ifsh)
{
const prog_char *p = (const prog_char *)ifsh;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) return;
write(c);
}
}

void Print::print(const String &s) void Print::print(const String &s)
{ {
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
Expand Down Expand Up @@ -101,10 +111,16 @@ void Print::print(double n, int digits)
printFloat(n, digits); printFloat(n, digits);
} }


void Print::println(const __FlashStringHelper *ifsh)
{
print(ifsh);
println();
}

void Print::println(void) void Print::println(void)
{ {
print('\r'); print('\r');
print('\n'); print('\n');
} }


void Print::println(const String &s) void Print::println(const String &s)
Expand Down
2 changes: 2 additions & 0 deletions hardware/arduino/cores/arduino/Print.h
Expand Up @@ -40,6 +40,7 @@ class Print
virtual void write(const char *str); virtual void write(const char *str);
virtual void write(const uint8_t *buffer, size_t size); virtual void write(const uint8_t *buffer, size_t size);


void print(const __FlashStringHelper *);
void print(const String &); void print(const String &);
void print(const char[]); void print(const char[]);
void print(char); void print(char);
Expand All @@ -50,6 +51,7 @@ class Print
void print(unsigned long, int = DEC); void print(unsigned long, int = DEC);
void print(double, int = 2); void print(double, int = 2);


void println(const __FlashStringHelper *);
void println(const String &s); void println(const String &s);
void println(const char[]); void println(const char[]);
void println(char); void println(char);
Expand Down
11 changes: 7 additions & 4 deletions hardware/arduino/cores/arduino/WString.h
Expand Up @@ -34,6 +34,9 @@
// -felide-constructors // -felide-constructors
// -std=c++0x // -std=c++0x


class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))

// An inherited class for holding the result of a concatenation. These // An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations. // result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper; class StringSumHelper;
Expand All @@ -51,8 +54,8 @@ class String
// constructors // constructors
// creates a copy of the initial value. // creates a copy of the initial value.
// if the initial value is null or invalid, or if memory allocation // if the initial value is null or invalid, or if memory allocation
// fails, the string will be marked as invalid (i.e. operator bool() // fails, the string will be marked as invalid (i.e. "if (s)" will
// will return false). // be false).
String(const char *cstr = ""); String(const char *cstr = "");
String(const String &str); String(const String &str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
Expand All @@ -70,13 +73,13 @@ class String
// memory management // memory management
// return true on success, false on failure (in which case, the string // return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an // is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., operator bool() will return true afterwards) // invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size); unsigned char reserve(unsigned int size);
inline unsigned int length(void) const {return len;} inline unsigned int length(void) const {return len;}


// creates a copy of the assigned value. if the value is null or // creates a copy of the assigned value. if the value is null or
// invalid, or if the memory allocation fails, the string will be // invalid, or if the memory allocation fails, the string will be
// marked as invalid (operator bool() will return false). // marked as invalid ("if (s)" will be false).
String & operator = (const String &rhs); String & operator = (const String &rhs);
String & operator = (const char *cstr); String & operator = (const char *cstr);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
Expand Down

0 comments on commit 0ac0dcf

Please sign in to comment.