FastLED 3.9.15
Loading...
Searching...
No Matches

◆ swapWith()

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

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

1233 {
1234 if (this == &other) return;
1235
1236 bool thisInline = isInline();
1237 bool otherInline = other.isInline();
1238
1239 if (!thisInline && !otherInline) {
1240 // Both non-inline: swap variant + length
1241 fl::swap(mStorage, other.mStorage);
1242 fl::swap(mLength, other.mLength);
1243 } else if (thisInline && otherInline) {
1244 // Both inline: check capacity before swapping
1245 bool thisFits = other.mLength + 1 <= mInlineCapacity;
1246 bool otherFits = mLength + 1 <= other.mInlineCapacity;
1247 if (thisFits && otherFits) {
1248 // Both fit: swap buffer contents directly
1249 fl::size maxLen = fl::max(mLength, other.mLength);
1250 for (fl::size i = 0; i <= maxLen; ++i) {
1251 char tmp = inlineBufferPtr()[i];
1252 inlineBufferPtr()[i] = other.inlineBufferPtr()[i];
1253 other.inlineBufferPtr()[i] = tmp;
1254 }
1255 fl::swap(mLength, other.mLength);
1256 } else {
1257 // Capacity mismatch: promote to heap where needed
1258 NotNullStringHolderPtr thisData(
1260 NotNullStringHolderPtr otherData(
1261 fl::make_shared<StringHolder>(other.inlineBufferPtr(), other.mLength));
1262 fl::size thisLen = mLength;
1263 fl::size otherLen = other.mLength;
1264 if (thisFits) {
1265 mStorage.reset();
1266 fl::memcpy(inlineBufferPtr(), otherData->data(), otherLen + 1);
1267 } else {
1268 mStorage = otherData;
1269 }
1270 mLength = otherLen;
1271 if (otherFits) {
1272 other.mStorage.reset();
1273 fl::memcpy(other.inlineBufferPtr(), thisData->data(), thisLen + 1);
1274 } else {
1275 other.mStorage = thisData;
1276 }
1277 other.mLength = thisLen;
1278 }
1279 } else if (thisInline) {
1280 // this inline, other non-inline
1281 fl::size thisLen = mLength;
1282 // Take other's non-inline storage
1283 mStorage = fl::move(other.mStorage);
1284 mLength = other.mLength;
1285 // Put this's old inline data into other
1286 other.mStorage.reset();
1287 if (thisLen + 1 <= other.mInlineCapacity) {
1288 fl::memcpy(other.inlineBufferPtr(), inlineBufferPtr(), thisLen + 1);
1289 } else {
1290 other.mStorage = NotNullStringHolderPtr(
1292 }
1293 other.mLength = thisLen;
1294 } else {
1295 // this non-inline, other inline — reverse
1296 other.swapWith(*this);
1297 }
1298}
bool isInline() const FL_NOEXCEPT
fl::size mInlineCapacity
fl::variant< NotNullStringHolderPtr, ConstLiteral, ConstView > mStorage
char * inlineBufferPtr() FL_NOEXCEPT
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
void swap(T &a, T &b) FL_NOEXCEPT
Definition s16x16x4.h:877
void * memcpy(void *dest, const void *src, size_t n) FL_NOEXCEPT
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
fl::not_null< StringHolderPtr > NotNullStringHolderPtr
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414

References basic_string(), inlineBufferPtr(), isInline(), fl::make_shared(), fl::max(), fl::memcpy(), mInlineCapacity, mLength, fl::fl::move(), mStorage, fl::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: