Skip to content

Commit

Permalink
tweak variable move constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jkl1337 committed Mar 11, 2024
1 parent 2f5e237 commit 7161a4a
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions kiwi/kiwi/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -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
Expand Down

0 comments on commit 7161a4a

Please sign in to comment.