From f42634497072bee700f2c8b4f48141af9ce15649 Mon Sep 17 00:00:00 2001 From: Martin Pischky Date: Tue, 4 Jan 2022 10:16:32 +0100 Subject: [PATCH] Update ostream allow use of F() macro for Strings: cout << F("Hello") << endl; --- src/ostream | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/ostream b/src/ostream index a5dd3c5c..35e675fc 100644 --- a/src/ostream +++ b/src/ostream @@ -27,6 +27,11 @@ #include "cstdio" #include "ostream_helpers" +#ifdef ARDUINO_ARCH_AVR + #include // typedef PGM_P, pgm_read_byte() + #include "WString.h" // class __FlashStringHelper +#endif + #pragma GCC visibility push(default) namespace std { @@ -407,6 +412,24 @@ namespace std { return out; } +#ifdef ARDUINO_ARCH_AVR + //support strings from flash memory [PI] + //stolen from Arduino Print.cpp + //sample use: cout << F("Hello") << endl; + template _UCXXEXPORT basic_ostream& + operator<<(basic_ostream& out, const __FlashStringHelper* c) + { + typename basic_ostream::sentry s(out); + PGM_P p = reinterpret_cast(c); + while (true) { + charT x = pgm_read_byte(p++); + if (x == 0) break; + out.put(x); + } + return out; + } +#endif + // partial specializations template _UCXXEXPORT basic_ostream& operator<<(basic_ostream& out, const char* c) @@ -416,6 +439,24 @@ namespace std { return out; } +#ifdef ARDUINO_ARCH_AVR + //support strings from flash memory [PI] + //stolen from Arduino Print.cpp + //sample use: cout << F("Hello") << endl; + template _UCXXEXPORT basic_ostream& + operator<<(basic_ostream& out, const __FlashStringHelper* c) //[PI: New] stolen from Arduino Print.cpp + { + typename basic_ostream::sentry s(out); + PGM_P p = reinterpret_cast(c); + while (true) { + char x = pgm_read_byte(p++); + if (x == 0) break; + out.put(x); + } + return out; + } +#endif + #ifdef __UCLIBCXX_HAS_WCHAR__ template _UCXXEXPORT basic_ostream& operator<<(basic_ostream& out, const char* c)