diff options
author | crupest <crupest@outlook.com> | 2021-09-14 22:44:23 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-14 22:44:23 +0800 |
commit | 7ad71df1cc81074747024620f421bfcb2bc6dbb1 (patch) | |
tree | cb300101cd6cd4da533a61b4d32271889bd7f283 | |
parent | 46d4838ac8ff1bd8658b57cf4ebb4438e396fce8 (diff) | |
download | cru-7ad71df1cc81074747024620f421bfcb2bc6dbb1.tar.gz cru-7ad71df1cc81074747024620f421bfcb2bc6dbb1.tar.bz2 cru-7ad71df1cc81074747024620f421bfcb2bc6dbb1.zip |
...
-rw-r--r-- | src/common/String.cpp | 16 | ||||
-rw-r--r-- | test/common/StringTest.cpp | 12 |
2 files changed, 25 insertions, 3 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; diff --git a/test/common/StringTest.cpp b/test/common/StringTest.cpp index 1200a60b..a0895af9 100644 --- a/test/common/StringTest.cpp +++ b/test/common/StringTest.cpp @@ -2,8 +2,18 @@ #include <gtest/gtest.h> +TEST(String, Append) { + using cru::String; + + String s; + s.append(u"ha"); + s.append(s); + ASSERT_EQ(s, String(u"haha")); +} + TEST(String, Format) { using cru::Format; + using cru::String; - ASSERT_EQ(Format(u"{} + {} = {}", 123, 321, 444), u"123 + 321 = 444"); + ASSERT_EQ(Format(u"{} + {} = {}", 123, 321, 444), String(u"123 + 321 = 444")); } |