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

◆ insert() [4/5]

basic_string & fl::basic_string::insert ( fl::size pos,
const char * s,
fl::size count )

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

834 {
835 if (pos > mLength) pos = mLength;
836 if (!s || count == 0) return *this;
837
838 // Materialize non-owning storage before modifying
839 if (isNonOwning()) {
840 materialize();
841 }
842
843 fl::size newLen = mLength + count;
844
845 // Handle COW
846 if (hasHeapData() && heapData().get().use_count() > 1) {
847 const NotNullStringHolderPtr& heap = heapData();
849 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
850 fl::memcpy(newData->data() + pos, s, count);
851 if (pos < mLength) fl::memcpy(newData->data() + pos + count, heap->data() + pos, mLength - pos);
852 newData->data()[newLen] = '\0';
853 mStorage = newData;
854 mLength = newLen;
855 return *this;
856 }
857
858 // Inline buffer
859 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
860 if (pos < mLength) {
862 }
863 fl::memcpy(inlineBufferPtr() + pos, s, count);
864 mLength = newLen;
865 inlineBufferPtr()[mLength] = '\0';
866 return *this;
867 }
868
869 // Heap in-place or new allocation
870 bool canInsertInPlace = hasHeapData();
871 if (canInsertInPlace) {
873 canInsertInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
874 if (canInsertInPlace) {
875 char* data = heap->data();
876 if (pos < mLength) fl::memmove(data + pos + count, data + pos, mLength - pos);
877 fl::memcpy(data + pos, s, count);
878 mLength = newLen;
879 data[mLength] = '\0';
880 }
881 }
882 if (!canInsertInPlace) {
884 const char* src = c_str();
885 if (pos > 0) fl::memcpy(newData->data(), src, pos);
886 fl::memcpy(newData->data() + pos, s, count);
887 if (pos < mLength) fl::memcpy(newData->data() + pos + count, src + pos, mLength - pos);
888 newData->data()[newLen] = '\0';
889 mStorage = newData;
890 mLength = newLen;
891 }
892 return *this;
893}
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().

+ Here is the call graph for this function: