From 7161a4ace8794c40df9f266e124a3c0fd0c76f87 Mon Sep 17 00:00:00 2001 From: "John K. Luebs" Date: Mon, 11 Mar 2024 16:54:37 -0500 Subject: [PATCH] tweak variable move constructor --- kiwi/kiwi/variable.h | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/kiwi/kiwi/variable.h b/kiwi/kiwi/variable.h index a910551..c565cfa 100644 --- a/kiwi/kiwi/variable.h +++ b/kiwi/kiwi/variable.h @@ -40,27 +40,22 @@ class SmallStr { } ~SmallStr() { if (len_ > INLINE_LEN) delete[] d_.ptr_; } - SmallStr(const SmallStr &other) : len_(other.len_) { - copy(other); - } - SmallStr(SmallStr &&other) noexcept : len_(other.len_) { - other.len_ = 0; + SmallStr(const SmallStr &other) : len_(other.len_) { if (len_ > INLINE_LEN) { - d_.ptr_ = other.d_.ptr_; + d_.ptr_ = new char[len_ + 1]; + std::memcpy(d_.ptr_, other.d_.ptr_, len_ + 1); } else { std::memcpy(d_.inline_, other.d_.inline_, len_ + 1); } } - SmallStr& operator=(const SmallStr &other) { - if (this == &other) return *this; - if (len_ > INLINE_LEN) delete[] d_.ptr_; - len_ = other.len_; - copy(other); - return *this; + SmallStr(SmallStr &&other) noexcept : len_(other.len_), d_(other.d_) { + other.len_ = 0; } + SmallStr& operator=(const SmallStr &) = delete; + SmallStr& operator=(SmallStr &&other) noexcept { std::swap(len_, other.len_); std::swap(d_, other.d_); @@ -69,16 +64,6 @@ class SmallStr { const char *c_str() const { return len_ > INLINE_LEN ? d_.ptr_ : d_.inline_; } explicit operator bool() const { return len_ != 0; } - -private: - void copy(const SmallStr &other) { - if (len_ > INLINE_LEN) { - d_.ptr_ = new char[len_ + 1]; - std::memcpy(d_.ptr_, other.d_.ptr_, len_ + 1); - } else { - std::memcpy(d_.inline_, other.d_.inline_, len_ + 1); - } - } }; class VariableData