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

◆ replace() [4/5]

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

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

947 {
948 if (pos > mLength) return *this;
949 if (!s) return erase(pos, count);
950
951 // Materialize non-owning storage before modifying
952 if (isNonOwning()) {
953 materialize();
954 }
955
956 fl::size actualCount = count;
957 if (actualCount == npos || pos + actualCount > mLength) {
958 actualCount = mLength - pos;
959 }
960 fl::size newLen = mLength - actualCount + count2;
961
962 // Handle COW
963 if (hasHeapData() && heapData().get().use_count() > 1) {
964 const NotNullStringHolderPtr& heap = heapData();
966 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
967 fl::memcpy(newData->data() + pos, s, count2);
968 fl::size remainingLen = mLength - pos - actualCount;
969 if (remainingLen > 0) {
970 fl::memcpy(newData->data() + pos + count2, heap->data() + pos + actualCount, remainingLen);
971 }
972 newData->data()[newLen] = '\0';
973 mStorage = newData;
974 mLength = newLen;
975 return *this;
976 }
977
978 // Inline buffer
979 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
980 if (count2 != actualCount) {
981 fl::size remainingLen = mLength - pos - actualCount;
982 if (remainingLen > 0) {
983 fl::memmove(inlineBufferPtr() + pos + count2, inlineBufferPtr() + pos + actualCount, remainingLen);
984 }
985 }
986 fl::memcpy(inlineBufferPtr() + pos, s, count2);
987 mLength = newLen;
988 inlineBufferPtr()[mLength] = '\0';
989 return *this;
990 }
991
992 // Heap in-place or new allocation
993 bool canReplaceInPlace = hasHeapData();
994 if (canReplaceInPlace) {
996 canReplaceInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
997 if (canReplaceInPlace) {
998 char* data = heap->data();
999 if (count2 != actualCount) {
1000 fl::size remainingLen = mLength - pos - actualCount;
1001 if (remainingLen > 0) {
1002 fl::memmove(data + pos + count2, data + pos + actualCount, remainingLen);
1003 }
1004 }
1005 fl::memcpy(data + pos, s, count2);
1006 mLength = newLen;
1007 data[mLength] = '\0';
1008 }
1009 }
1010 if (!canReplaceInPlace) {
1012 const char* src = c_str();
1013 if (pos > 0) fl::memcpy(newData->data(), src, pos);
1014 fl::memcpy(newData->data() + pos, s, count2);
1015 fl::size remainingLen = mLength - pos - actualCount;
1016 if (remainingLen > 0) {
1017 fl::memcpy(newData->data() + pos + count2, src + pos + actualCount, remainingLen);
1018 }
1019 newData->data()[newLen] = '\0';
1020 mStorage = newData;
1021 mLength = newLen;
1022 }
1023 return *this;
1024}
uint8_t pos
Definition Blur.ino:11
basic_string & erase(fl::size pos=0, fl::size count=npos) FL_NOEXCEPT
bool isNonOwning() const FL_NOEXCEPT
bool hasHeapData() const FL_NOEXCEPT
void materialize() FL_NOEXCEPT
fl::size mInlineCapacity
NotNullStringHolderPtr & heapData() FL_NOEXCEPT
static constexpr fl::size npos
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(), erase(), fl::get(), fl::not_null< T >::get(), hasHeapData(), heapData(), inlineBufferPtr(), isNonOwning(), fl::make_shared(), materialize(), fl::memcpy(), fl::memmove(), mInlineCapacity, mLength, mStorage, npos, pos, and fl::shared_ptr< T >::use_count().

+ Here is the call graph for this function: