FastLED 3.9.15
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 1031 of file basic_string.cpp.hpp.

1031 {
1032 if (pos > mLength) return *this;
1033
1034 // Materialize non-owning storage before modifying
1035 if (isNonOwning()) {
1036 materialize();
1037 }
1038
1039 fl::size actualCount = count;
1040 if (actualCount == npos || pos + actualCount > mLength) {
1041 actualCount = mLength - pos;
1042 }
1043 fl::size newLen = mLength - actualCount + count2;
1044
1045 // Handle COW
1046 if (hasHeapData() && heapData().get().use_count() > 1) {
1047 const NotNullStringHolderPtr& heap = heapData();
1049 if (pos > 0) fl::memcpy(newData->data(), heap->data(), pos);
1050 for (fl::size i = 0; i < count2; ++i) newData->data()[pos + i] = ch;
1051 fl::size remainingLen = mLength - pos - actualCount;
1052 if (remainingLen > 0) {
1053 fl::memcpy(newData->data() + pos + count2, heap->data() + pos + actualCount, remainingLen);
1054 }
1055 newData->data()[newLen] = '\0';
1056 mStorage = newData;
1057 mLength = newLen;
1058 return *this;
1059 }
1060
1061 // Inline buffer
1062 if (newLen + 1 <= mInlineCapacity && !hasHeapData()) {
1063 if (count2 != actualCount) {
1064 fl::size remainingLen = mLength - pos - actualCount;
1065 if (remainingLen > 0) {
1066 fl::memmove(inlineBufferPtr() + pos + count2, inlineBufferPtr() + pos + actualCount, remainingLen);
1067 }
1068 }
1069 for (fl::size i = 0; i < count2; ++i) inlineBufferPtr()[pos + i] = ch;
1070 mLength = newLen;
1071 inlineBufferPtr()[mLength] = '\0';
1072 return *this;
1073 }
1074
1075 // Heap in-place or new allocation
1076 bool canReplaceInPlace = hasHeapData();
1077 if (canReplaceInPlace) {
1079 canReplaceInPlace = heap.get().use_count() <= 1 && heap->hasCapacity(newLen);
1080 if (canReplaceInPlace) {
1081 char* data = heap->data();
1082 if (count2 != actualCount) {
1083 fl::size remainingLen = mLength - pos - actualCount;
1084 if (remainingLen > 0) {
1085 fl::memmove(data + pos + count2, data + pos + actualCount, remainingLen);
1086 }
1087 }
1088 for (fl::size i = 0; i < count2; ++i) data[pos + i] = ch;
1089 mLength = newLen;
1090 data[mLength] = '\0';
1091 }
1092 }
1093 if (!canReplaceInPlace) {
1095 const char* src = c_str();
1096 if (pos > 0) fl::memcpy(newData->data(), src, pos);
1097 for (fl::size i = 0; i < count2; ++i) newData->data()[pos + i] = ch;
1098 fl::size remainingLen = mLength - pos - actualCount;
1099 if (remainingLen > 0) {
1100 fl::memcpy(newData->data() + pos + count2, src + pos + actualCount, remainingLen);
1101 }
1102 newData->data()[newLen] = '\0';
1103 mStorage = newData;
1104 mLength = newLen;
1105 }
1106 return *this;
1107}
uint8_t pos
Definition Blur.ino:11
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(), 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: