| 264 | } |
| 265 | |
| 266 | void String::move(String& rhs) |
| 267 | { |
| 268 | if(rhs.isNull()) { |
| 269 | invalidate(); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | auto rhs_len = rhs.length(); |
| 274 | if(rhs.sso.set) { |
| 275 | // Switch to SSO if required (note: we don't rely on this succeeding) |
| 276 | (void)reserve(rhs_len); |
| 277 | } |
| 278 | |
| 279 | // If we have more capacity than the target, copy the data and free rhs buffers |
| 280 | if(rhs.sso.set || capacity() > rhs.capacity()) { |
| 281 | memmove(buffer(), rhs.buffer(), rhs_len); |
| 282 | setlen(rhs_len); |
| 283 | rhs.invalidate(); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | // We don't have enough space so perform a pointer swap |
| 288 | if(!sso.set) { |
| 289 | free(ptr.buffer); |
| 290 | } |
| 291 | sso.set = false; |
| 292 | ptr = rhs.ptr; |
| 293 | // Can't use rhs.invalidate here as it would free the buffer |
| 294 | rhs.ptr.buffer = nullptr; |
| 295 | rhs.ptr.capacity = 0; |
| 296 | rhs.ptr.len = 0; |
| 297 | } |
| 298 | |
| 299 | String& String::operator=(const String& rhs) |
| 300 | { |