39 static const uint16_t DEFAULT_SPACE_ALLOCATED = 64;
48 : m_minimumSpace(initialSize),
49 m_currentSpace(initialSize),
51 this->m_string = (
char *) malloc(initialSize);
52 this->m_string[0] =
'\0';
64 if (NULL != this->m_string)
66 this->m_string[this->m_size] =
'\0';
69 void puts (
const char string[]) {
71 if (NULL != this->m_string) {
72 for (
const char *s =
string; *s; ++s)
73 this->insert_char(*s);
74 this->m_string[this->m_size] =
'\0';
82 return this->m_string;
97 if (this->m_minimumSpace != this->m_currentSpace) {
98 if (NULL != this->m_string)
100 free(this->m_string);
101 this->m_string = (
char *) malloc(this->m_minimumSpace);
102 this->m_currentSpace = this->m_minimumSpace;
104 this->m_string[0] =
'\0';
110 void insert_char (
const char c) {
111 m_string[m_size++] = c;
115 void check_buffer_size () {
116 if (m_size + 1 == m_currentSpace)
121 this->m_currentSpace <<= 1;
122 char *temp = (
char *) malloc((
size_t) this->m_currentSpace);
123 strcpy(temp, this->m_string);
124 free(this->m_string);
125 this->m_string = temp;
129 const uint16_t m_minimumSpace;
130 uint16_t m_currentSpace;
Generic definitions and functions for the Parallax Propeller.
void put_char(const char c)
Print a single character.
uint16_t get_size() const
Determine the length of the string, not including the null terminator.
void clear()
Remove all characters from the string and reallocate to the original size (if needed) ...
Build a dynamically-sized string in RAM using the PropWare::Printer interface.
void puts(const char string[])
Send a null-terminated character array. Though this method could be created using put_char...
~StringBuilder()
Free all memory allocated for the string buffer.
Interface for all classes capable of printing.
const char * to_string() const
Retrieve the address of the string buffer.
StringBuilder(const size_t initialSize=DEFAULT_SPACE_ALLOCATED)
Initialize with a given size to start with. Picking the correct size can increase performance...