FastLED 3.10.6
Loading...
Searching...
No Matches

◆ insert() [5/5]

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

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

779 {
780 if (pos > mLength) pos = mLength;
781 if (count == 0) return *this;
782
783 // Materialize non-owning storage before modifying
784 if (isNonOwning()) {
785 materialize();
786 }
787
788 fl::size newLen = mLength + count;
789
790 // Handle COW
791 if (hasHeapData() && heapData().get().use_count() > 1) {
792 const NotNullStringHolderPtr& heap = heapData();
794 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
795 for (fl::size i = 0; i < count; ++i) newData->data()[pos + i] = ch;
796 if (pos < mLength) fl::memcpy(newData->data() + pos + count, heap->data() + pos, mLength - pos);
797 newData->data()[newLen] = '\0';
798 mStorage = newData;
799 mLength = newLen;
800 return *this;
801 }
802
803 // Inline buffer
804 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
805 if (pos < mLength) {
807 }
808 for (fl::size i = 0; i < count; ++i) inlineBufferPtr()[pos + i] = ch;
809 mLength = newLen;
810 inlineBufferPtr()[mLength] = '\0';
811 return *this;
812 }
813
814 // Heap in-place or new allocation
815 bool canInsertInPlace = hasHeapData();
816 if (canInsertInPlace) {
818 canInsertInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
819 if (canInsertInPlace) {
820 char* data = heap->data();
821 if (pos < mLength) fl::memmove(data + pos + count, data + pos, mLength - pos);
822 for (fl::size i = 0; i < count; ++i) data[pos + i] = ch;
823 mLength = newLen;
824 data[mLength] = '\0';
825 }
826 }
827 if (!canInsertInPlace) {
829 const char* src = c_str();
830 if (pos > 0) fl::memcpy(newData->data(), src, pos);
831 for (fl::size i = 0; i < count; ++i) newData->data()[pos + i] = ch;
832 if (pos < mLength) fl::memcpy(newData->data() + pos + count, src + pos, mLength - pos);
833 newData->data()[newLen] = '\0';
834 mStorage = newData;
835 mLength = newLen;
836 }
837 return *this;
838}
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
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
void * memmove(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

References basic_string(), c_str(), data(), fl::get(), fl::not_null< T >::get(), hasHeapData(), heapData(), inlineBufferPtr(), isNonOwning(), fl::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: