FastLED 3.10.6
Loading...
Searching...
No Matches

◆ replace() [5/5]

basic_string & fl::basic_string::replace ( fl::size pos,
fl::size count,
fl::size count2,
char ch )

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

1055 {
1056 if (pos > mLength) return *this;
1057
1058 // Materialize non-owning storage before modifying
1059 if (isNonOwning()) {
1060 materialize();
1061 }
1062
1063 fl::size actualCount = count;
1064 if (actualCount == npos || pos + actualCount > mLength) {
1065 actualCount = mLength - pos;
1066 }
1067 fl::size newLen = mLength - actualCount + count2;
1068
1069 // Handle COW
1070 if (hasHeapData() && heapData().get().use_count() > 1) {
1071 const NotNullStringHolderPtr& heap = heapData();
1073 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
1074 for (fl::size i = 0; i < count2; ++i) newData->data()[pos + i] = ch;
1075 fl::size remainingLen = mLength - pos - actualCount;
1076 if (remainingLen > 0) {
1077 fl::memcpy(newData->data() + pos + count2, heap->data() + pos + actualCount, remainingLen);
1078 }
1079 newData->data()[newLen] = '\0';
1080 mStorage = newData;
1081 mLength = newLen;
1082 return *this;
1083 }
1084
1085 // Inline buffer
1086 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
1087 if (count2 != actualCount) {
1088 fl::size remainingLen = mLength - pos - actualCount;
1089 if (remainingLen > 0) {
1090 fl::memmove(inlineBufferPtr() + pos + count2, inlineBufferPtr() + pos + actualCount, remainingLen);
1091 }
1092 }
1093 for (fl::size i = 0; i < count2; ++i) inlineBufferPtr()[pos + i] = ch;
1094 mLength = newLen;
1095 inlineBufferPtr()[mLength] = '\0';
1096 return *this;
1097 }
1098
1099 // Heap in-place or new allocation
1100 bool canReplaceInPlace = hasHeapData();
1101 if (canReplaceInPlace) {
1103 canReplaceInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
1104 if (canReplaceInPlace) {
1105 char* data = heap->data();
1106 if (count2 != actualCount) {
1107 fl::size remainingLen = mLength - pos - actualCount;
1108 if (remainingLen > 0) {
1109 fl::memmove(data + pos + count2, data + pos + actualCount, remainingLen);
1110 }
1111 }
1112 for (fl::size i = 0; i < count2; ++i) data[pos + i] = ch;
1113 mLength = newLen;
1114 data[mLength] = '\0';
1115 }
1116 }
1117 if (!canReplaceInPlace) {
1119 const char* src = c_str();
1120 if (pos > 0) fl::memcpy(newData->data(), src, pos);
1121 for (fl::size i = 0; i < count2; ++i) newData->data()[pos + i] = ch;
1122 fl::size remainingLen = mLength - pos - actualCount;
1123 if (remainingLen > 0) {
1124 fl::memcpy(newData->data() + pos + count2, src + pos + actualCount, remainingLen);
1125 }
1126 newData->data()[newLen] = '\0';
1127 mStorage = newData;
1128 mLength = newLen;
1129 }
1130 return *this;
1131}
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
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(), 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: