FastLED 3.10.6
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 (existingLen > 0 && existingData) {
247 fl::memcpy(newData->data(), existingData, existingLen);
248 }
249 fl::memcpy(newData->data() + existingLen, str, n);
250 newData->data()[newLen] = '\0';
251 mStorage = newData;
252 mLength = newLen;
253 }
254 return mLength;
255 }
256
257 if (hasHeapData() && heapData().get().use_count() <= 1) {
259 if (!heap->hasCapacity(newLen)) {
260 // Check if str points into our buffer (self-referential write).
261 // grow() uses realloc which can relocate the buffer.
262 const char* bufStart = heap->data();
263 fl::size grow_length = fl::max(3, newLen * 3 / 2);
264 if (str >= bufStart && str < bufStart + mLength + 1) {
265 fl::size offset = static_cast<fl::size>(str - bufStart);
266 heap->grow(grow_length);
267 str = heap->data() + offset; // update to new location
268 } else {
269 heap->grow(grow_length);
270 }
271 }
272 fl::memcpy(heap->data() + mLength, str, n);
273 mLength = newLen;
274 heap->data()[mLength] = '\0';
275 return mLength;
276 } else if (hasHeapData()) {
277 // Copy-on-write
279 {
280 const NotNullStringHolderPtr& heap = heapData();
281 fl::memcpy(newData->data(), heap->data(), mLength);
282 fl::memcpy(newData->data() + mLength, str, n);
283 newData->data()[newLen] = '\0';
284 mStorage = newData;
285 mLength = newLen;
286 }
287 return mLength;
288 }
289 if (newLen + 1 <= mInlineCapacity) {
291 FL_DISABLE_WARNING(array-bounds)
294 mLength = newLen;
295 inlineBufferPtr()[mLength] = '\0';
296 return mLength;
297 }
298 // Transition from inline to heap
300 {
301 fl::memcpy(newData->data(), inlineBufferPtr(), mLength);
302 fl::memcpy(newData->data() + mLength, str, n);
303 newData->data()[newLen] = '\0';
304 mStorage = newData;
305 mLength = newLen;
306 }
307 return mLength;
308}
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
#define FL_DISABLE_WARNING(warning)
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_POP
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:414
pair_element< I, T1, T2 >::type & get(pair< T1, T2 > &p) FL_NO_EXCEPT
Definition pair.h:115