FastLED 3.9.15
Loading...
Searching...
No Matches
string_holder.cpp.hpp
Go to the documentation of this file.
1// IWYU pragma: begin_keep
2#include <stdlib.h>
3#include <string.h> // okay banned header (STL wrapper implementation requires standard header for strlen)
4
5// IWYU pragma: end_keep
7#include "fl/stl/cstring.h" // For memcpy
8#include "fl/stl/malloc.h" // For fl::malloc, fl::free, fl::realloc
9#include "fl/stl/noexcept.h"
10
11namespace fl {
12
13// StringHolder implementations
14// Assumes fl::malloc/fl::realloc/fl::free cannot return NULL (OOM is terminal)
15
17 : mData((char*)fl::malloc(strlen(str) + 1))
18 , mLength(strlen(str))
19 , mCapacity(mLength + 1) {
21 mData[mLength] = '\0';
22}
23
25 : mData((char*)fl::malloc(length + 1))
27 , mCapacity(length + 1) {
28 mData[mLength] = '\0';
29}
30
31StringHolder::StringHolder(const char *str, size length)
32 : mData((char*)fl::malloc(length + 1))
34 , mCapacity(length + 1) {
36 mData[mLength] = '\0';
37}
38
40 fl::free(mData); // Release the memory
41}
42
43void StringHolder::grow(size newLength) {
44 if (newLength + 1 <= mCapacity) {
45 // We have enough capacity for newLength + null terminator
46 mLength = newLength;
47 mData[mLength] = '\0';
48 return;
49 }
50
51 // Use fl::realloc for efficient growth without memory move
52 // fl::realloc may expand in place or copy to larger block as needed
53 mData = (char*)fl::realloc(mData, newLength + 1);
54 mLength = newLength;
55 mCapacity = newLength + 1;
56 mData[mLength] = '\0'; // Ensure null-termination
57}
58
59} // namespace fl
size length() const FL_NOEXCEPT
void grow(size newLength) FL_NOEXCEPT
StringHolder(const char *str) FL_NOEXCEPT
~StringHolder() FL_NOEXCEPT
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
size_t strlen(const char *s) FL_NOEXCEPT
void * malloc(size_t size)
Definition malloc.cpp.hpp:9
void free(void *ptr)
void * realloc(void *ptr, size_t new_size)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT