FastLED 3.10.10
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 883 of file basic_string.cpp.hpp.

883 {
884 if (pos > mLength) pos = mLength;
885 if (!s || count == 0) return *this;
886
887 // Materialize non-owning storage before modifying
888 if (isNonOwning()) {
889 materialize();
890 }
891
892 fl::size newLen = mLength + count;
893
894 // Handle COW
895 if (hasHeapData() && heapData().get().use_count() > 1) {
896 const NotNullStringHolderPtr& heap = heapData();
898 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
899 fl::memcpy(newData->data() + pos, s, count);
900 if (pos < mLength) fl::memcpy(newData->data() + pos + count, heap->data() + pos, mLength - pos);
901 newData->data()[newLen] = '\0';
902 mStorage = newData;
903 mLength = newLen;
904 return *this;
905 }
906
907 // Inline buffer
908 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
909 if (pos < mLength) {
911 }
912 fl::memcpy(inlineBufferPtr() + pos, s, count);
913 mLength = newLen;
914 inlineBufferPtr()[mLength] = '\0';
915 return *this;
916 }
917
918 // Heap in-place or new allocation
919 bool canInsertInPlace = hasHeapData();
920 if (canInsertInPlace) {
922 canInsertInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
923 if (canInsertInPlace) {
924 char* data = heap->data();
925 if (pos < mLength) fl::memmove(data + pos + count, data + pos, mLength - pos);
926 fl::memcpy(data + pos, s, count);
927 mLength = newLen;
928 data[mLength] = '\0';
929 }
930 }
931 if (!canInsertInPlace) {
933 const char* src = c_str();
934 if (pos > 0) fl::memcpy(newData->data(), src, pos);
935 fl::memcpy(newData->data() + pos, s, count);
936 if (pos < mLength) fl::memcpy(newData->data() + pos + count, src + pos, mLength - pos);
937 newData->data()[newLen] = '\0';
938 mStorage = newData;
939 mLength = newLen;
940 }
941 return *this;
942}
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().

+ Here is the call graph for this function: