From 7cbf76171342d07236a7852e4e30705dff635cc4 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 9 Sep 2021 20:51:35 +0800 Subject: ... --- src/common/String.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/common/String.cpp') diff --git a/src/common/String.cpp b/src/common/String.cpp index 078462c4..e9acf984 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -101,6 +101,29 @@ String::~String() { } } +void String::resize(Index new_size) { + Expects(new_size >= 0); + + if (new_size == size_) return; + + if (new_size == 0) { + delete[] buffer_; + buffer_ = kEmptyBuffer; + size_ = 0; + capacity_ = 0; + } + + if (new_size < size_) { + size_ = new_size; + buffer_[size_] = 0; + } else { + reserve(new_size); + std::memset(buffer_ + size_, 0, sizeof(std::uint16_t) * (new_size - size_)); + buffer_[new_size] = 0; + size_ = new_size; + } +} + void String::reserve(Index new_capacity) { if (new_capacity <= this->capacity_) return; if (new_capacity > 0) { @@ -134,6 +157,11 @@ String::iterator String::insert(const_iterator pos, std::uint16_t* str, String::iterator String::erase(const_iterator start, const_iterator end) { Index new_size = size_ - (end - start); + if (new_size == 0) { + resize(0); + return this->end(); + } + auto s = const_cast(start); auto e = const_cast(end); -- cgit v1.2.3