aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/common/String.hpp1
-rw-r--r--src/common/String.cpp28
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);