AceCommon
1.4.6
Arduino library for low-level common functions and features with no external dependencies
|
These are utilities and classes for working with strings stored in flash (const __FlashStringHelper*
). On certain chips (AVR, ESP8266), storing strings in flash memory using the PROGMEM
keyword and the F()
macro can reduce static RAM consumption. On most other 32-bit processors, const strings are automatically stored in flash memory, and the PROGMEM
and F()
are defined for compatibility, but don't do anything.
This is a very thin wrapper class that looks and acts like a normal c-string pointer (const char*
). Its purpose is to help avoid duplication of code. When writing string manipulation utilties, it is often useful to provide variants of the utilities that accept a flash string with the type const __FlashStringHelper*
in addition to the normal c-string const char*
. But accessing these flash pointers must be done with the pgm_read_byte()
method, instead of just dereferencing the const char*
pointer with a *
operator. This usually means that we have to write out the algorithm twice, with almost duplicate code, except for some minor variations.
The FlashString
class works by wrapping the const __FlashStringHelper*
pointer into a FlashString
object which acts basically like a const char*
pointer. You can then write a generic template function that handles the normal const char*
pointer case, then reuse the same function when called with the const __FlashStringHelper*
pointer.
The FlashString
implements various methods to emulate a const char*
, with the critical operator*()
method calling the pgm_read_byte()
function:
Then for your code, write out the template function using a generic T
class while assuming that it acts like a simple const char*
pointer:
See print_utils/printReplaceTo.h for an example of this in action.
This class stores either a c-string (const char*
) or a flash string (const __FlashStringHelper*
) in a single object, and provides a type discriminator that indicates which type of string is being stored. The original pointer can be retrieved using a getCString()
or a getFString()
.