FastLED 3.10.10
Loading...
Searching...
No Matches

◆ write() [2/11]

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

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

229 {
230 fl::size newLen = mLength + n;
231
232 // Handle non-owning storage
233 if (isNonOwning()) {
234 const char* existingData = constData();
235 fl::size existingLen = mLength;
236 if (newLen + 1 <= mInlineCapacity) {
237 if (existingLen > 0 && existingData) {
238 fl::memcpy(inlineBufferPtr(), existingData, existingLen);
239 }
240 fl::memcpy(inlineBufferPtr() + existingLen, str, n);
241 inlineBufferPtr()[newLen] = '\0';
242 mStorage.reset();
243 mLength = newLen;
244 } else {
246 if (!newData->hasCapacity(newLen)) {
247 return mLength;
248 }
249 if (existingLen > 0 && existingData) {
250 fl::memcpy(newData->data(), existingData, existingLen);
251 }
252 fl::memcpy(newData->data() + existingLen, str, n);
253 newData->data()[newLen] = '\0';
254 mStorage = newData;
255 mLength = newLen;
256 }
257 return mLength;
258 }
259
260 if (hasHeapData() && heapData().get().use_count() <= 1) {
262 if (!heap->hasCapacity(newLen)) {
263 // Check if str points into our buffer (self-referential write).
264 // grow() uses realloc which can relocate the buffer.
265 const char* bufStart = heap->data();
266 fl::size grow_length = fl::max(3, newLen * 3 / 2);
267 if (str >= bufStart && str < bufStart + mLength + 1) {
268 fl::size offset = static_cast<fl::size>(str - bufStart);
269 heap->grow(grow_length);
270 str = heap->data() + offset; // update to new location
271 } else {
272 heap->grow(grow_length);
273 }
274 // grow() preserves the old allocation on OOM. Do not append or
275 // write its terminator unless the requested bytes actually fit.
276 if (!heap->hasCapacity(newLen)) {
277 return mLength;
278 }
279 }
280 fl::memcpy(heap->data() + mLength, str, n);
281 mLength = newLen;
282 heap->data()[mLength] = '\0';
283 return mLength;
284 } else if (hasHeapData()) {
285 // Copy-on-write
287 if (!newData->hasCapacity(newLen)) {
288 return mLength;
289 }
290 {
291 const NotNullStringHolderPtr& heap = heapData();
292 fl::memcpy(newData->data(), heap->data(), mLength);
293 fl::memcpy(newData->data() + mLength, str, n);
294 newData->data()[newLen] = '\0';
295 mStorage = newData;
296 mLength = newLen;
297 }
298 return mLength;
299 }
300 if (newLen + 1 <= mInlineCapacity) {
302 FL_DISABLE_WARNING(array-bounds)
305 mLength = newLen;
306 inlineBufferPtr()[mLength] = '\0';
307 return mLength;
308 }
309 // Transition from inline to heap
311 if (!newData->hasCapacity(newLen)) {
312 return mLength;
313 }
314 {
315 fl::memcpy(newData->data(), inlineBufferPtr(), mLength);
316 fl::memcpy(newData->data() + mLength, str, n);
317 newData->data()[newLen] = '\0';
318 mStorage = newData;
319 mLength = newLen;
320 }
321 return mLength;
322}
const char * constData() const FL_NO_EXCEPT
bool isNonOwning() const FL_NO_EXCEPT
NotNullStringHolderPtr & heapData() FL_NO_EXCEPT
bool hasHeapData() const FL_NO_EXCEPT
fl::size mInlineCapacity
char * inlineBufferPtr() FL_NO_EXCEPT
fl::variant< NotNullStringHolderPtr, ConstLiteral, ConstView > mStorage
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:136
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NO_EXCEPT
Definition pair.h:119
#define FL_DISABLE_WARNING(warning)
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_POP