aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-09-14 22:44:23 +0800
committercrupest <crupest@outlook.com>2021-09-14 22:44:23 +0800
commit7ad71df1cc81074747024620f421bfcb2bc6dbb1 (patch)
treecb300101cd6cd4da533a61b4d32271889bd7f283 /src/common
parent46d4838ac8ff1bd8658b57cf4ebb4438e396fce8 (diff)
downloadcru-7ad71df1cc81074747024620f421bfcb2bc6dbb1.tar.gz
cru-7ad71df1cc81074747024620f421bfcb2bc6dbb1.tar.bz2
cru-7ad71df1cc81074747024620f421bfcb2bc6dbb1.zip
...
Diffstat (limited to 'src/common')
-rw-r--r--src/common/String.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/common/String.cpp b/src/common/String.cpp
index 94eb8f27..22795b17 100644
--- a/src/common/String.cpp
+++ b/src/common/String.cpp
@@ -1,6 +1,7 @@
#include "cru/common/String.hpp"
#include "cru/common/StringUtil.hpp"
+#include <algorithm>
#include <cstring>
#include <string_view>
@@ -143,6 +144,13 @@ void String::reserve(Index new_capacity) {
String::iterator String::insert(const_iterator pos, const std::uint16_t* str,
Index size) {
+ std::vector<std::uint16_t> backup_buffer;
+ if (str >= buffer_ && str < buffer_ + size_) {
+ backup_buffer.resize(size);
+ std::copy(str, str + size, backup_buffer.begin());
+ str = backup_buffer.data();
+ }
+
Index new_size = size_ + size;
if (new_size > capacity_) {
this->reserve(this->capacity_ * 2);
@@ -150,11 +158,13 @@ String::iterator String::insert(const_iterator pos, const std::uint16_t* str,
auto p = const_cast<iterator>(pos);
- std::memmove(p + size, pos, (cend() - pos) * sizeof(std::uint16_t));
+ std::memmove(p + size, p, (cend() - pos) * sizeof(std::uint16_t));
std::memcpy(p, str, size * sizeof(std::uint16_t));
buffer_[new_size] = 0;
+ size_ = new_size;
+
return p + size;
}
@@ -230,10 +240,12 @@ int String::Compare(const String& other) const {
while (i1 != end1 && i2 != end2) {
int r = cru::Compare(*i1, *i2);
if (r != 0) return r;
+ i1++;
+ i2++;
}
if (i1 == end1) {
- if (i2 == end1) {
+ if (i2 == end2) {
return 0;
} else {
return -1;