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

◆ insert() [5/5]

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

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

755 {
756 if (pos > mLength) pos = mLength;
757 if (count == 0) return *this;
758
759 // Materialize non-owning storage before modifying
760 if (isNonOwning()) {
761 materialize();
762 }
763
764 fl::size newLen = mLength + count;
765
766 // Handle COW
767 if (hasHeapData() && heapData().get().use_count() > 1) {
768 const NotNullStringHolderPtr& heap = heapData();
770 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
771 for (fl::size i = 0; i < count; ++i) newData->data()[pos + i] = ch;
772 if (pos < mLength) fl::memcpy(newData->data() + pos + count, heap->data() + pos, mLength - pos);
773 newData->data()[newLen] = '\0';
774 mStorage = newData;
775 mLength = newLen;
776 return *this;
777 }
778
779 // Inline buffer
780 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
781 if (pos < mLength) {
783 }
784 for (fl::size i = 0; i < count; ++i) inlineBufferPtr()[pos + i] = ch;
785 mLength = newLen;
786 inlineBufferPtr()[mLength] = '\0';
787 return *this;
788 }
789
790 // Heap in-place or new allocation
791 bool canInsertInPlace = hasHeapData();
792 if (canInsertInPlace) {
794 canInsertInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
795 if (canInsertInPlace) {
796 char* data = heap->data();
797 if (pos < mLength) fl::memmove(data + pos + count, data + pos, mLength - pos);
798 for (fl::size i = 0; i < count; ++i) data[pos + i] = ch;
799 mLength = newLen;
800 data[mLength] = '\0';
801 }
802 }
803 if (!canInsertInPlace) {
805 const char* src = c_str();
806 if (pos > 0) fl::memcpy(newData->data(), src, pos);
807 for (fl::size i = 0; i < count; ++i) newData->data()[pos + i] = ch;
808 if (pos < mLength) fl::memcpy(newData->data() + pos + count, src + pos, mLength - pos);
809 newData->data()[newLen] = '\0';
810 mStorage = newData;
811 mLength = newLen;
812 }
813 return *this;
814}
uint8_t pos
Definition Blur.ino:11
bool isNonOwning() const FL_NOEXCEPT
bool hasHeapData() const FL_NOEXCEPT
void materialize() FL_NOEXCEPT
fl::size mInlineCapacity
NotNullStringHolderPtr & heapData() FL_NOEXCEPT
const char * data() const FL_NOEXCEPT
fl::variant< NotNullStringHolderPtr, ConstLiteral, ConstView > mStorage
char * inlineBufferPtr() FL_NOEXCEPT
const char * c_str() const FL_NOEXCEPT
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
void * memmove(void *dest, const void *src, size_t n) FL_NOEXCEPT
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

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: