Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string::erase function #218

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
-Wno-unneeded-member-function
-Wno-unused-member-function
-Wno-unsafe-buffer-usage
-Wno-deprecated-declarations
-Wno-ctad-maybe-unsupported
-Werror
)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
Expand Down
19 changes: 19 additions & 0 deletions include/cista/containers/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ struct generic_string {
set_non_owning(s, len);
}

generic_string(generic_string&& o) { move_from(std::move(o)); }
generic_string(generic_string const& o) { copy_from(o); }

generic_string& operator=(generic_string&& o) { move_from(std::move(o)); }
generic_string& operator=(generic_string const& o) { copy_from(o); }

char* begin() noexcept { return data(); }
char* end() noexcept { return data() + size(); }
char const* begin() const noexcept { return data(); }
Expand Down Expand Up @@ -311,6 +317,19 @@ struct generic_string {
return h_.size_;
}

generic_string& erase(msize_t const pos, msize_t const n) {
if (!is_short() && !h_.self_allocated_) {
set_owning(view());
}
auto const size_before = size();
std::memmove(data() + pos, data() + pos + n, size_before - (pos + n));
std::memset(data() + size_before - n, 0U, n);
if (!is_short()) {
h_.size_ = size_before - n;
}
return *this;
}

struct heap {
bool is_short_{false};
bool self_allocated_{false};
Expand Down
21 changes: 21 additions & 0 deletions test/string_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <vector>

#include "doctest.h"

Expand Down Expand Up @@ -31,6 +32,26 @@ TEST_CASE("string long short corner 14") {
CHECK(s.view() == CORNER_CASE_SHORT_14);
}

TEST_CASE("string erase") {
auto uut = std::vector<cista::generic_string<char const*>>{};
auto ref = std::vector<std::string>{};
for (auto const s :
{CORNER_CASE_SHORT_14, CORNER_CASE_SHORT_15, CORNER_CASE_LONG_16}) {
auto x = cista::generic_string{
s, cista::generic_string<char const*>::non_owning};
x.erase(3, 7);
uut.emplace_back(std::move(x));

auto y = std::string{s};
y.erase(3, 7);
ref.emplace_back(std::move(y));
}

for (auto i = 0U; i != ref.size(); ++i) {
CHECK(ref[i] == uut[i].view());
}
}

TEST_CASE("string long short corner 15") {
auto s = string{CORNER_CASE_SHORT_15, string::owning};
CHECK(s.is_short());
Expand Down