diff options
author | crupest <crupest@outlook.com> | 2021-09-09 20:51:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-09 20:51:35 +0800 |
commit | 7cbf76171342d07236a7852e4e30705dff635cc4 (patch) | |
tree | d7946aaa7b01576ef8ba47613960ec08c1a55557 | |
parent | fc8d5e97582310da7bc195df212d847cf2853e36 (diff) | |
download | cru-7cbf76171342d07236a7852e4e30705dff635cc4.tar.gz cru-7cbf76171342d07236a7852e4e30705dff635cc4.tar.bz2 cru-7cbf76171342d07236a7852e4e30705dff635cc4.zip |
...
-rw-r--r-- | include/cru/common/String.hpp | 1 | ||||
-rw-r--r-- | src/common/String.cpp | 28 |
2 files changed, 29 insertions, 0 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 4211a160..8197c402 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -77,6 +77,7 @@ class CRU_BASE_API String { std::uint16_t* data() { return this->buffer_; } const std::uint16_t* data() const { return this->buffer_; } + void resize(Index new_size); void reserve(Index new_capacity); std::uint16_t& front() { return this->operator[](0); } 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<iterator>(start); auto e = const_cast<iterator>(end); |