FastLED 3.9.15
Loading...
Searching...
No Matches

◆ write() [2/11]

fl::size fl::basic_string::write ( const char * str,
fl::size n )

Definition at line 225 of file basic_string.cpp.hpp.

225 {
226 fl::size newLen = mLength + n;
227
228 // Handle non-owning storage
229 if (isNonOwning()) {
230 const char* existingData = constData();
231 fl::size existingLen = mLength;
232 if (newLen + 1 <= mInlineCapacity) {
233 if (existingLen > 0 && existingData) {
234 fl::memcpy(inlineBufferPtr(), existingData, existingLen);
235 }
236 fl::memcpy(inlineBufferPtr() + existingLen, str, n);
237 inlineBufferPtr()[newLen] = '\0';
238 mStorage.reset();
239 mLength = newLen;
240 } else {
242 if (existingLen > 0 && existingData) {
243 fl::memcpy(newData->data(), existingData, existingLen);
244 }
245 fl::memcpy(newData->data() + existingLen, str, n);
246 newData->data()[newLen] = '\0';
247 mStorage = newData;
248 mLength = newLen;
249 }
250 return mLength;
251 }
252
253 if (hasHeapData() && heapData().get().use_count() <= 1) {
255 if (!heap->hasCapacity(newLen)) {
256 // Check if str points into our buffer (self-referential write).
257 // grow() uses realloc which can relocate the buffer.
258 const char* bufStart = heap->data();
259 fl::size grow_length = fl::max(3, newLen * 3 / 2);
260 if (str >= bufStart && str < bufStart + mLength + 1) {
261 fl::size offset = static_cast<fl::size>(str - bufStart);
262 heap->grow(grow_length);
263 str = heap->data() + offset; // update to new location
264 } else {
265 heap->grow(grow_length);
266 }
267 }
268 fl::memcpy(heap->data() + mLength, str, n);
269 mLength = newLen;
270 heap->data()[mLength] = '\0';
271 return mLength;
272 } else if (hasHeapData()) {
273 // Copy-on-write
275 {
276 const NotNullStringHolderPtr& heap = heapData();
277 fl::memcpy(newData->data(), heap->data(), mLength);
278 fl::memcpy(newData->data() + mLength, str, n);
279 newData->data()[newLen] = '\0';
280 mStorage = newData;
281 mLength = newLen;
282 }
283 return mLength;
284 }
285 if (newLen + 1 <= mInlineCapacity) {
287 FL_DISABLE_WARNING(array-bounds)
290 mLength = newLen;
291 inlineBufferPtr()[mLength] = '\0';
292 return mLength;
293 }
294 // Transition from inline to heap
296 {
297 fl::memcpy(newData->data(), inlineBufferPtr(), mLength);
298 fl::memcpy(newData->data() + mLength, str, n);
299 newData->data()[newLen] = '\0';
300 mStorage = newData;
301 mLength = newLen;
302 }
303 return mLength;
304}
bool isNonOwning() const FL_NOEXCEPT
bool hasHeapData() const FL_NOEXCEPT
fl::size mInlineCapacity
NotNullStringHolderPtr & heapData() FL_NOEXCEPT
fl::variant< NotNullStringHolderPtr, ConstLiteral, ConstView > mStorage
char * inlineBufferPtr() FL_NOEXCEPT
const char * constData() const FL_NOEXCEPT
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NOEXCEPT
Definition pair.h:115
#define FL_DISABLE_WARNING(warning)
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_POP