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

996 {
997 if (pos > mLength) return *this;
998 if (!s) return erase(pos, count);
999
1000 // Materialize non-owning storage before modifying
1001 if (isNonOwning()) {
1002 materialize();
1003 }
1004
1005 fl::size actualCount = count;
1006 if (actualCount == npos || pos + actualCount > mLength) {
1007 actualCount = mLength - pos;
1008 }
1009 fl::size newLen = mLength - actualCount + count2;
1010
1011 // Handle COW
1012 if (hasHeapData() && heapData().get().use_count() > 1) {
1013 const NotNullStringHolderPtr& heap = heapData();
1015 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
1016 fl::memcpy(newData->data() + pos, s, count2);
1017 fl::size remainingLen = mLength - pos - actualCount;
1018 if (remainingLen > 0) {
1019 fl::memcpy(newData->data() + pos + count2, heap->data() + pos + actualCount, remainingLen);
1020 }
1021 newData->data()[newLen] = '\0';
1022 mStorage = newData;
1023 mLength = newLen;
1024 return *this;
1025 }
1026
1027 // Inline buffer
1028 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
1029 if (count2 != actualCount) {
1030 fl::size remainingLen = mLength - pos - actualCount;
1031 if (remainingLen > 0) {
1032 fl::memmove(inlineBufferPtr() + pos + count2, inlineBufferPtr() + pos + actualCount, remainingLen);
1033 }
1034 }
1035 fl::memcpy(inlineBufferPtr() + pos, s, count2);
1036 mLength = newLen;
1037 inlineBufferPtr()[mLength] = '\0';
1038 return *this;
1039 }
1040
1041 // Heap in-place or new allocation
1042 bool canReplaceInPlace = hasHeapData();
1043 if (canReplaceInPlace) {
1045 canReplaceInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
1046 if (canReplaceInPlace) {
1047 char* data = heap->data();
1048 if (count2 != actualCount) {
1049 fl::size remainingLen = mLength - pos - actualCount;
1050 if (remainingLen > 0) {
1051 fl::memmove(data + pos + count2, data + pos + actualCount, remainingLen);
1052 }
1053 }
1054 fl::memcpy(data + pos, s, count2);
1055 mLength = newLen;
1056 data[mLength] = '\0';
1057 }
1058 }
1059 if (!canReplaceInPlace) {
1061 const char* src = c_str();
1062 if (pos > 0) fl::memcpy(newData->data(), src, pos);
1063 fl::memcpy(newData->data() + pos, s, count2);
1064 fl::size remainingLen = mLength - pos - actualCount;
1065 if (remainingLen > 0) {
1066 fl::memcpy(newData->data() + pos + count2, src + pos + actualCount, remainingLen);
1067 }
1068 newData->data()[newLen] = '\0';
1069 mStorage = newData;
1070 mLength = newLen;
1071 }
1072 return *this;
1073}
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
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(), erase(), 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, npos, pos, and fl::shared_ptr< T >::use_count().

+ Here is the call graph for this function: