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

◆ swapWith()

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

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

1257 {
1258 if (this == &other) return;
1259
1260 bool thisInline = isInline();
1261 bool otherInline = other.isInline();
1262
1263 if (!thisInline && !otherInline) {
1264 // Both non-inline: swap variant + length
1265 fl::swap(mStorage, other.mStorage);
1266 fl::swap(mLength, other.mLength);
1267 } else if (thisInline && otherInline) {
1268 // Both inline: check capacity before swapping
1269 bool thisFits = other.mLength + 1 <= mInlineCapacity;
1270 bool otherFits = mLength + 1 <= other.mInlineCapacity;
1271 if (thisFits && otherFits) {
1272 // Both fit: swap buffer contents directly
1273 fl::size maxLen = fl::max(mLength, other.mLength);
1274 for (fl::size i = 0; i <= maxLen; ++i) {
1275 char tmp = inlineBufferPtr()[i];
1276 inlineBufferPtr()[i] = other.inlineBufferPtr()[i];
1277 other.inlineBufferPtr()[i] = tmp;
1278 }
1279 fl::swap(mLength, other.mLength);
1280 } else {
1281 // Capacity mismatch: promote to heap where needed
1282 NotNullStringHolderPtr thisData(
1284 NotNullStringHolderPtr otherData(
1285 fl::make_shared<StringHolder>(other.inlineBufferPtr(), other.mLength));
1286 fl::size thisLen = mLength;
1287 fl::size otherLen = other.mLength;
1288 if (thisFits) {
1289 mStorage.reset();
1290 fl::memcpy(inlineBufferPtr(), otherData->data(), otherLen + 1);
1291 } else {
1292 mStorage = otherData;
1293 }
1294 mLength = otherLen;
1295 if (otherFits) {
1296 other.mStorage.reset();
1297 fl::memcpy(other.inlineBufferPtr(), thisData->data(), thisLen + 1);
1298 } else {
1299 other.mStorage = thisData;
1300 }
1301 other.mLength = thisLen;
1302 }
1303 } else if (thisInline) {
1304 // this inline, other non-inline
1305 fl::size thisLen = mLength;
1306 // Take other's non-inline storage
1307 mStorage = fl::move(other.mStorage);
1308 mLength = other.mLength;
1309 // Put this's old inline data into other
1310 other.mStorage.reset();
1311 if (thisLen + 1 <= other.mInlineCapacity) {
1312 fl::memcpy(other.inlineBufferPtr(), inlineBufferPtr(), thisLen + 1);
1313 } else {
1314 other.mStorage = NotNullStringHolderPtr(
1316 }
1317 other.mLength = thisLen;
1318 } else {
1319 // this non-inline, other inline — reverse
1320 other.swapWith(*this);
1321 }
1322}
fl::size mInlineCapacity
char * inlineBufferPtr() FL_NO_EXCEPT
bool isInline() const FL_NO_EXCEPT
fl::variant< NotNullStringHolderPtr, ConstLiteral, ConstView > mStorage
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
void swap(array< T, N > &lhs, array< T, N > &rhs) FL_NO_EXCEPT
Definition array.h:209
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:28
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
void * memcpy(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

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

Referenced by operator=(), and swapWith().

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