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

◆ insert() [5/5]

basic_string & fl::basic_string::insert ( fl::size pos,
fl::size count,
char ch )

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

804 {
805 if (pos > mLength) pos = mLength;
806 if (count == 0) return *this;
807
808 // Materialize non-owning storage before modifying
809 if (isNonOwning()) {
810 materialize();
811 }
812
813 fl::size newLen = mLength + count;
814
815 // Handle COW
816 if (hasHeapData() && heapData().get().use_count() > 1) {
817 const NotNullStringHolderPtr& heap = heapData();
819 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
820 for (fl::size i = 0; i < count; ++i) newData->data()[pos + i] = ch;
821 if (pos < mLength) fl::memcpy(newData->data() + pos + count, heap->data() + pos, mLength - pos);
822 newData->data()[newLen] = '\0';
823 mStorage = newData;
824 mLength = newLen;
825 return *this;
826 }
827
828 // Inline buffer
829 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
830 if (pos < mLength) {
832 }
833 for (fl::size i = 0; i < count; ++i) inlineBufferPtr()[pos + i] = ch;
834 mLength = newLen;
835 inlineBufferPtr()[mLength] = '\0';
836 return *this;
837 }
838
839 // Heap in-place or new allocation
840 bool canInsertInPlace = hasHeapData();
841 if (canInsertInPlace) {
843 canInsertInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
844 if (canInsertInPlace) {
845 char* data = heap->data();
846 if (pos < mLength) fl::memmove(data + pos + count, data + pos, mLength - pos);
847 for (fl::size i = 0; i < count; ++i) data[pos + i] = ch;
848 mLength = newLen;
849 data[mLength] = '\0';
850 }
851 }
852 if (!canInsertInPlace) {
854 const char* src = c_str();
855 if (pos > 0) fl::memcpy(newData->data(), src, pos);
856 for (fl::size i = 0; i < count; ++i) newData->data()[pos + i] = ch;
857 if (pos < mLength) fl::memcpy(newData->data() + pos + count, src + pos, mLength - pos);
858 newData->data()[newLen] = '\0';
859 mStorage = newData;
860 mLength = newLen;
861 }
862 return *this;
863}
uint8_t pos
Definition Blur.ino:11
bool isNonOwning() const FL_NO_EXCEPT
void materialize() 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
const char * c_str() const FL_NO_EXCEPT
const char * data() const FL_NO_EXCEPT
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
FASTLED_FORCE_INLINE void * memmove(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:144
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

References basic_string(), c_str(), data(), fl::FL_NO_EXCEPT, fl::get(), fl::not_null< T >::get(), hasHeapData(), heapData(), inlineBufferPtr(), isNonOwning(), fl::adl_barrier::make_shared(), materialize(), fl::memcpy(), fl::memmove(), mInlineCapacity, mLength, mStorage, pos, and fl::shared_ptr< T >::use_count().

Referenced by insert(), insert(), and insert().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: