aboutsummaryrefslogtreecommitdiff
path: root/src/common/String.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/String.cpp')
-rw-r--r--src/common/String.cpp28
1 files changed, 28 insertions, 0 deletions
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);