diff options
author | crupest <crupest@outlook.com> | 2021-09-15 14:44:18 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-15 14:44:18 +0800 |
commit | 93c9b3738c0a9dfc12f456e53fcaa098296a9d6a (patch) | |
tree | 862c01fc6e713a56ef0d9ba062163c05099f26fd | |
parent | 7ad71df1cc81074747024620f421bfcb2bc6dbb1 (diff) | |
download | cru-93c9b3738c0a9dfc12f456e53fcaa098296a9d6a.tar.gz cru-93c9b3738c0a9dfc12f456e53fcaa098296a9d6a.tar.bz2 cru-93c9b3738c0a9dfc12f456e53fcaa098296a9d6a.zip |
...
-rw-r--r-- | include/cru/common/String.hpp | 1 | ||||
-rw-r--r-- | src/common/String.cpp | 43 |
2 files changed, 21 insertions, 23 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 9443417f..4859a696 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -240,6 +240,7 @@ void FormatAppendFromFormatTokenList( current += ToString(std::forward<TA>(args0)); FormatAppendFromFormatTokenList(current, format_token_list, i + 1, std::forward<T>(args)...); + return; } else { current += token.data; } diff --git a/src/common/String.cpp b/src/common/String.cpp index 22795b17..6d958688 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -41,16 +41,15 @@ String::String(const wchar_t* str, Index size) #endif String::String(const String& other) { - if (other.buffer_ == kEmptyBuffer) return; + if (other.size_ == 0) return; this->buffer_ = new std::uint16_t[other.size_ + 1]; - std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(wchar_t)); + std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(value_type)); this->buffer_[other.size_] = 0; this->size_ = other.size_; this->capacity_ = other.size_; } String::String(String&& other) noexcept { - if (other.buffer_ == kEmptyBuffer) return; this->buffer_ = other.buffer_; this->size_ = other.size_; this->capacity_ = other.capacity_; @@ -71,7 +70,8 @@ String& String::operator=(const String& other) { this->capacity_ = 0; } else { this->buffer_ = new std::uint16_t[other.size_ + 1]; - std::memcpy(this->buffer_, other.buffer_, other.size_ * sizeof(wchar_t)); + std::memcpy(this->buffer_, other.buffer_, + other.size_ * sizeof(value_type)); this->buffer_[other.size_] = 0; this->size_ = other.size_; this->capacity_ = other.size_; @@ -111,13 +111,6 @@ void String::resize(Index new_size) { 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; @@ -138,6 +131,7 @@ void String::reserve(Index new_capacity) { delete[] this->buffer_; } new_buffer[this->size_] = 0; + this->buffer_ = new_buffer; this->capacity_ = new_capacity; } } @@ -151,31 +145,34 @@ String::iterator String::insert(const_iterator pos, const std::uint16_t* str, str = backup_buffer.data(); } + Index index = pos - cbegin(); Index new_size = size_ + size; if (new_size > capacity_) { - this->reserve(this->capacity_ * 2); - } + auto new_capacity = capacity_; + if (new_capacity == 0) { + new_capacity = new_size; + } else { + while (new_capacity < new_size) { + new_capacity *= 2; + } + } - auto p = const_cast<iterator>(pos); + this->reserve(new_capacity); + } - std::memmove(p + size, p, (cend() - pos) * sizeof(std::uint16_t)); - std::memcpy(p, str, size * sizeof(std::uint16_t)); + std::memmove(begin() + index + size, begin() + index, + (size_ - index) * sizeof(value_type)); + std::memcpy(begin() + index, str, size * sizeof(value_type)); buffer_[new_size] = 0; - size_ = new_size; - return p + size; + return begin() + new_size; } 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); |