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

1080 {
1081 if (pos > mLength) return *this;
1082
1083 // Materialize non-owning storage before modifying
1084 if (isNonOwning()) {
1085 materialize();
1086 }
1087
1088 fl::size actualCount = count;
1089 if (actualCount == npos || pos + actualCount > mLength) {
1090 actualCount = mLength - pos;
1091 }
1092 fl::size newLen = mLength - actualCount + count2;
1093
1094 // Handle COW
1095 if (hasHeapData() && heapData().get().use_count() > 1) {
1096 const NotNullStringHolderPtr& heap = heapData();
1098 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
1099 for (fl::size i = 0; i < count2; ++i) newData->data()[pos + i] = ch;
1100 fl::size remainingLen = mLength - pos - actualCount;
1101 if (remainingLen > 0) {
1102 fl::memcpy(newData->data() + pos + count2, heap->data() + pos + actualCount, remainingLen);
1103 }
1104 newData->data()[newLen] = '\0';
1105 mStorage = newData;
1106 mLength = newLen;
1107 return *this;
1108 }
1109
1110 // Inline buffer
1111 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
1112 if (count2 != actualCount) {
1113 fl::size remainingLen = mLength - pos - actualCount;
1114 if (remainingLen > 0) {
1115 fl::memmove(inlineBufferPtr() + pos + count2, inlineBufferPtr() + pos + actualCount, remainingLen);
1116 }
1117 }
1118 for (fl::size i = 0; i < count2; ++i) inlineBufferPtr()[pos + i] = ch;
1119 mLength = newLen;
1120 inlineBufferPtr()[mLength] = '\0';
1121 return *this;
1122 }
1123
1124 // Heap in-place or new allocation
1125 bool canReplaceInPlace = hasHeapData();
1126 if (canReplaceInPlace) {
1128 canReplaceInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
1129 if (canReplaceInPlace) {
1130 char* data = heap->data();
1131 if (count2 != actualCount) {
1132 fl::size remainingLen = mLength - pos - actualCount;
1133 if (remainingLen > 0) {
1134 fl::memmove(data + pos + count2, data + pos + actualCount, remainingLen);
1135 }
1136 }
1137 for (fl::size i = 0; i < count2; ++i) data[pos + i] = ch;
1138 mLength = newLen;
1139 data[mLength] = '\0';
1140 }
1141 }
1142 if (!canReplaceInPlace) {
1144 const char* src = c_str();
1145 if (pos > 0) fl::memcpy(newData->data(), src, pos);
1146 for (fl::size i = 0; i < count2; ++i) newData->data()[pos + i] = ch;
1147 fl::size remainingLen = mLength - pos - actualCount;
1148 if (remainingLen > 0) {
1149 fl::memcpy(newData->data() + pos + count2, src + pos + actualCount, remainingLen);
1150 }
1151 newData->data()[newLen] = '\0';
1152 mStorage = newData;
1153 mLength = newLen;
1154 }
1155 return *this;
1156}
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
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, npos, pos, and fl::shared_ptr< T >::use_count().

+ Here is the call graph for this function: