AceCommon
1.6.0
Arduino library for low-level common functions and features with no external dependencies
|
An implementation of Print
that writes to an in-memory buffer supporting strings less than 65535 in length.
More...
#include <PrintStr.h>
Additional Inherited Members | |
![]() | |
size_t | write (uint8_t c) override |
Write a single character into the internal buffer. | |
size_t | write (const uint8_t *buf, size_t size) override |
Write the buf string of size into the internal buffer. | |
void | flush () override |
Clear the internal buffer. More... | |
const char * | cstr () const |
Return the NUL terminated c-string buffer. More... | |
const char * | getCstr () const |
Backwards compatible version of cstr(). More... | |
size_t | length () const |
Return the length of the internal c-string buffer, not including the NUL terminator. | |
![]() | |
PrintStrBase (uint16_t size, char *buf) | |
Constructor. More... | |
![]() | |
char *const | buf_ |
This is the pointer to the character array buffer. More... | |
An implementation of Print
that writes to an in-memory buffer supporting strings less than 65535 in length.
It is intended to be an alternative to the String
class to help avoid heap fragmentation due to repeated creation and deletion of small String objects. The 'PrintStr' object inherit the methods from the 'Print' interface which can be used to build an internal string representation of various objects. Instead of using the operator+=()
or the concat()
method, use the print()
, println()
(or sometimes the printf()
method) of the Print
class. After the internal string is built, the NUL-terminated c-string representation can be retrieved using cstr()
.
This object is expected to be created on the stack instead of the heap to avoid heap fragmentation. The SIZE
parameter is a compile time constant, to allow the internal string buffer to be created on the stack. The object will be destroyed automatically when the stack is unwound after returning from the function where this is used.
An instance of PrintStr
can be reused by calling the flush()
method which causes the internal buffer to be cleared to an empty string. An instance of this object could be created statically and reused across different calling sites, but this was not the main intended use of this class.
Warning: The contents of cstr()
are valid only as long as the PrintStr object is alive. The pointer should never be passed to another part of the program if the PrintStr object is destroyed before the pointer is used.
Usage:
* #include <PrintStr.h> * * using namespace print_str; * * void fillStringA(PrintStr& message) { * message.print("hello, "); * message.println("world!"); * } * * void fillStringB(PrintStr& message) { * message.print("There are "); * message.print(42); * message.println(" apples"); * } * * void someFunction() { * PrintStr<32> message; * fillStringA(message) * const char* cstr = message.cstr(); * // do stuff with cstr * * message.flush(); * fillStringB(message); * cstr = message.cstr(); * // do more stuff with cstr * } *
SIZE | size of internal string buffer including the NUL terminator character, the maximum is 65535, which means the maximum string length is 65534 characters. |
Definition at line 267 of file PrintStr.h.