FastLED 3.10.10
Loading...
Searching...
No Matches

◆ swapWith()

void fl::basic_string::swapWith ( basic_string & other)
protected

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

1282 {
1283 if (this == &other) return;
1284
1285 bool thisInline = isInline();
1286 bool otherInline = other.isInline();
1287
1288 if (!thisInline && !otherInline) {
1289 // Both non-inline: swap variant + length
1290 fl::swap(mStorage, other.mStorage);
1291 fl::swap(mLength, other.mLength);
1292 } else if (thisInline && otherInline) {
1293 // Both inline: check capacity before swapping
1294 bool thisFits = other.mLength + 1 <= mInlineCapacity;
1295 bool otherFits = mLength + 1 <= other.mInlineCapacity;
1296 if (thisFits && otherFits) {
1297 // Both fit: swap buffer contents directly
1298 fl::size maxLen = fl::max(mLength, other.mLength);
1299 for (fl::size i = 0; i <= maxLen; ++i) {
1300 char tmp = inlineBufferPtr()[i];
1301 inlineBufferPtr()[i] = other.inlineBufferPtr()[i];
1302 other.inlineBufferPtr()[i] = tmp;
1303 }
1304 fl::swap(mLength, other.mLength);
1305 } else {
1306 // Capacity mismatch: promote to heap where needed
1307 NotNullStringHolderPtr thisData(
1309 NotNullStringHolderPtr otherData(
1310 fl::make_shared<StringHolder>(other.inlineBufferPtr(), other.mLength));
1311 fl::size thisLen = mLength;
1312 fl::size otherLen = other.mLength;
1313 if (thisFits) {
1314 mStorage.reset();
1315 fl::memcpy(inlineBufferPtr(), otherData->data(), otherLen + 1);
1316 } else {
1317 mStorage = otherData;
1318 }
1319 mLength = otherLen;
1320 if (otherFits) {
1321 other.mStorage.reset();
1322 fl::memcpy(other.inlineBufferPtr(), thisData->data(), thisLen + 1);
1323 } else {
1324 other.mStorage = thisData;
1325 }
1326 other.mLength = thisLen;
1327 }
1328 } else if (thisInline) {
1329 // this inline, other non-inline
1330 fl::size thisLen = mLength;
1331 // Take other's non-inline storage
1332 mStorage = fl::move(other.mStorage);
1333 mLength = other.mLength;
1334 // Put this's old inline data into other
1335 other.mStorage.reset();
1336 if (thisLen + 1 <= other.mInlineCapacity) {
1337 fl::memcpy(other.inlineBufferPtr(), inlineBufferPtr(), thisLen + 1);
1338 } else {
1339 other.mStorage = NotNullStringHolderPtr(
1341 }
1342 other.mLength = thisLen;
1343 } else {
1344 // this non-inline, other inline — reverse
1345 other.swapWith(*this);
1346 }
1347}
fl::size mInlineCapacity
char * inlineBufferPtr() FL_NO_EXCEPT
bool isInline() const FL_NO_EXCEPT
fl::variant< NotNullStringHolderPtr, ConstLiteral, ConstView > mStorage
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition s16x16x4.h:30
enable_if_t< is_same< T, U >::value > swap(T &a, U &b) FL_NO_EXCEPT
Definition s16x16x4.h:905
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
FASTLED_FORCE_INLINE void * memcpy(void *dest, const void *src, size_t n) FL_NO_EXCEPT
Definition cstring.h:136

References basic_string(), fl::FL_NO_EXCEPT, inlineBufferPtr(), isInline(), fl::adl_barrier::make_shared(), fl::max(), fl::memcpy(), mInlineCapacity, mLength, fl::fl::adl_barrier::move(), mStorage, and fl::fl::swap().

Referenced by operator=().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: