Skip to content
This repository
Browse code

Adding F("foo") syntax for flash strings.

  • Loading branch information...
commit 0ac0dcf158f0ac6c15a59925232fc7765ca4654c 1 parent e3c7a54
David A. Mellis authored March 27, 2011
18  hardware/arduino/cores/arduino/Print.cpp
@@ -43,6 +43,16 @@ void Print::write(const uint8_t *buffer, size_t size)
43 43
     write(*buffer++);
44 44
 }
45 45
 
  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
+
46 56
 void Print::print(const String &s)
47 57
 {
48 58
   for (int i = 0; i < s.length(); i++) {
@@ -101,10 +111,16 @@ void Print::print(double n, int digits)
101 111
   printFloat(n, digits);
102 112
 }
103 113
 
  114
+void Print::println(const __FlashStringHelper *ifsh)
  115
+{
  116
+  print(ifsh);
  117
+  println();
  118
+}
  119
+
104 120
 void Print::println(void)
105 121
 {
106 122
   print('\r');
107  
-  print('\n');  
  123
+  print('\n');
108 124
 }
109 125
 
110 126
 void Print::println(const String &s)
2  hardware/arduino/cores/arduino/Print.h
@@ -40,6 +40,7 @@ class Print
40 40
     virtual void write(const char *str);
41 41
     virtual void write(const uint8_t *buffer, size_t size);
42 42
     
  43
+    void print(const __FlashStringHelper *);
43 44
     void print(const String &);
44 45
     void print(const char[]);
45 46
     void print(char);
@@ -50,6 +51,7 @@ class Print
50 51
     void print(unsigned long, int = DEC);
51 52
     void print(double, int = 2);
52 53
 
  54
+    void println(const __FlashStringHelper *);
53 55
     void println(const String &s);
54 56
     void println(const char[]);
55 57
     void println(char);
11  hardware/arduino/cores/arduino/WString.h
@@ -34,6 +34,9 @@
34 34
 //     -felide-constructors
35 35
 //     -std=c++0x
36 36
 
  37
+class __FlashStringHelper;
  38
+#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))
  39
+
37 40
 // An inherited class for holding the result of a concatenation.  These
38 41
 // result objects are assumed to be writable by subsequent concatenations.
39 42
 class StringSumHelper;
@@ -51,8 +54,8 @@ class String
51 54
 	// constructors
52 55
 	// creates a copy of the initial value.
53 56
 	// 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).
56 59
 	String(const char *cstr = "");
57 60
 	String(const String &str);
58 61
 	#ifdef __GXX_EXPERIMENTAL_CXX0X__
@@ -70,13 +73,13 @@ class String
70 73
 	// memory management
71 74
 	// return true on success, false on failure (in which case, the string
72 75
 	// 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)
74 77
 	unsigned char reserve(unsigned int size);
75 78
 	inline unsigned int length(void) const {return len;}
76 79
 
77 80
 	// creates a copy of the assigned value.  if the value is null or
78 81
 	// 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).
80 83
 	String & operator = (const String &rhs);
81 84
 	String & operator = (const char *cstr);
82 85
 	#ifdef __GXX_EXPERIMENTAL_CXX0X__

0 notes on commit 0ac0dcf

Please sign in to comment.
Something went wrong with that request. Please try again.