From b2fd2c26cfb73d77fd9c01043d6d659e03365910 Mon Sep 17 00:00:00 2001 From: keqingmoe Date: Sat, 15 Mar 2025 00:25:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20kqm::basic=5Fstring::resiz?= =?UTF-8?q?e=20=E4=B8=8E=20reserve=20=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/string.cppm | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/module/string.cppm b/module/string.cppm index 14420c4..c1f2edb 100644 --- a/module/string.cppm +++ b/module/string.cppm @@ -129,8 +129,8 @@ private: other.mem_ = nullptr; other.sso_ = {}; } else { - sso_.siz = other.sso_.siz; - memcpy(sso_.dat, other.sso_.dat, sso_.siz); + mem_ = nullptr; + sso_ = other.sso_; } } @@ -310,6 +310,7 @@ public: auto buf = std::array{}; auto last = utf::utf32_to_utf8(ch, buf.data()); replace_impl({pos.base(), 0uz}, {buf.data(), last}); + return *this; } constexpr auto erase(iterator first, iterator last) & noexcept -> iterator @@ -475,22 +476,14 @@ public: if (new_cap > max_size_bytes()) { throw std::length_error{"new_cap > max_size_bytes()"}; } - auto [cap, mem, siz] = [this] { - if (mem_) { - return std::tuple{dyn_.cap, mem_, dyn_.siz}; - } else { - return std::tuple{sso_size, sso_.dat, sso_.siz}; - } - }(); - if (new_cap < cap) return; + if (new_cap <= capacity()) return; auto [new_mem, alloc_siz] = alloc_.allocate_at_least(new_cap); - std::memcpy(new_mem, mem, siz); + std::memcpy(new_mem, data(), size_bytes()); + auto new_dyn = dyn_t{size_bytes(), alloc_siz}; if (mem_) { alloc_.deallocate(mem_, dyn_.cap); - dyn_.cap = alloc_siz; - } else { - sso_.siz = alloc_siz; } + dyn_ = new_dyn; mem_ = new_mem; }