FastLED 3.10.6
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 971 of file basic_string.cpp.hpp.

971 {
972 if (pos > mLength) return *this;
973 if (!s) return erase(pos, count);
974
975 // Materialize non-owning storage before modifying
976 if (isNonOwning()) {
977 materialize();
978 }
979
980 fl::size actualCount = count;
981 if (actualCount == npos || pos + actualCount > mLength) {
982 actualCount = mLength - pos;
983 }
984 fl::size newLen = mLength - actualCount + count2;
985
986 // Handle COW
987 if (hasHeapData() && heapData().get().use_count() > 1) {
988 const NotNullStringHolderPtr& heap = heapData();
990 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
991 fl::memcpy(newData->data() + pos, s, count2);
992 fl::size remainingLen = mLength - pos - actualCount;
993 if (remainingLen > 0) {
994 fl::memcpy(newData->data() + pos + count2, heap->data() + pos + actualCount, remainingLen);
995 }
996 newData->data()[newLen] = '\0';
997 mStorage = newData;
998 mLength = newLen;
999 return *this;
1000 }
1001
1002 // Inline buffer
1003 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
1004 if (count2 != actualCount) {
1005 fl::size remainingLen = mLength - pos - actualCount;
1006 if (remainingLen > 0) {
1007 fl::memmove(inlineBufferPtr() + pos + count2, inlineBufferPtr() + pos + actualCount, remainingLen);
1008 }
1009 }
1010 fl::memcpy(inlineBufferPtr() + pos, s, count2);
1011 mLength = newLen;
1012 inlineBufferPtr()[mLength] = '\0';
1013 return *this;
1014 }
1015
1016 // Heap in-place or new allocation
1017 bool canReplaceInPlace = hasHeapData();
1018 if (canReplaceInPlace) {
1020 canReplaceInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
1021 if (canReplaceInPlace) {
1022 char* data = heap->data();
1023 if (count2 != actualCount) {
1024 fl::size remainingLen = mLength - pos - actualCount;
1025 if (remainingLen > 0) {
1026 fl::memmove(data + pos + count2, data + pos + actualCount, remainingLen);
1027 }
1028 }
1029 fl::memcpy(data + pos, s, count2);
1030 mLength = newLen;
1031 data[mLength] = '\0';
1032 }
1033 }
1034 if (!canReplaceInPlace) {
1036 const char* src = c_str();
1037 if (pos > 0) fl::memcpy(newData->data(), src, pos);
1038 fl::memcpy(newData->data() + pos, s, count2);
1039 fl::size remainingLen = mLength - pos - actualCount;
1040 if (remainingLen > 0) {
1041 fl::memcpy(newData->data() + pos + count2, src + pos + actualCount, remainingLen);
1042 }
1043 newData->data()[newLen] = '\0';
1044 mStorage = newData;
1045 mLength = newLen;
1046 }
1047 return *this;
1048}
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
static constexpr fl::size npos
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
basic_string & erase(fl::size pos=0, fl::size count=npos) 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(), 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: