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

Rebuild with string_view argument, like the constructor #584

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions tests/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,45 @@ TEST_CASE("message equality non equal lhs empty", "[message]")
CHECK(msg_a != msg_b);
}

TEST_CASE("message rebuild with size", "[message]")
{
zmq::message_t msg;
msg.rebuild(5);
CHECK(msg.size() == 5);
}

#if defined(ZMQ_CPP11) && !defined(ZMQ_CPP11_PARTIAL)
TEST_CASE("message rebuild with string literal", "[message]")
{
zmq::message_t hi_msg;
hi_msg.rebuild("Hi");
REQUIRE(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}

TEST_CASE("message rebuild with strings", "[message]")
{
SECTION("string")
{
const std::string hi(data);
zmq::message_t hi_msg;
hi_msg.rebuild(hi);
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
#if CPPZMQ_HAS_STRING_VIEW
SECTION("string_view")
{
const std::string_view hi(data);
zmq::message_t hi_msg;
hi_msg.rebuild(hi);
CHECK(2u == hi_msg.size());
CHECK(0 == memcmp(data, hi_msg.data(), 2));
}
#endif
}
#endif

TEST_CASE("message to string", "[message]")
{
const zmq::message_t a;
Expand Down
7 changes: 7 additions & 0 deletions zmq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,13 @@ class message_t
rebuild(str.data(), str.size());
}

#if CPPZMQ_HAS_STRING_VIEW
void rebuild(std::string_view str)
{
rebuild(str.data(), str.size());
}
#endif

void rebuild(void *data_, size_t size_, free_fn *ffn_, void *hint_ = ZMQ_NULLPTR)
{
int rc = zmq_msg_close(&msg);
Expand Down