Skip to content

Commit bd27f24

Browse files
authored
Merge pull request #373 from gummif/gfa/tostring
Problem: message_t to string is hard
2 parents de07119 + 4f9c2ea commit bd27f24

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

tests/message.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ TEST_CASE("message equality non equal lhs empty", "[message]")
151151
CHECK(msg_a != msg_b);
152152
}
153153

154+
TEST_CASE("message to string", "[message]")
155+
{
156+
const zmq::message_t a;
157+
const zmq::message_t b("Foo", 3);
158+
CHECK(a.to_string() == "");
159+
CHECK(b.to_string() == "Foo");
160+
#ifdef ZMQ_CPP17
161+
CHECK(a.to_string_view() == "");
162+
CHECK(b.to_string_view() == "Foo");
163+
#endif
164+
}
165+
154166
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
155167
TEST_CASE("message routing id persists", "[message]")
156168
{

zmq.hpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,25 @@ class message_t
552552
}
553553
#endif
554554

555-
/** Dump content to string. Ascii chars are readable, the rest is printed as hex.
556-
* Probably ridiculously slow.
557-
*/
555+
// interpret message content as a string
556+
std::string to_string() const
557+
{
558+
return std::string(static_cast<const char*>(data()), size());
559+
}
560+
#ifdef ZMQ_CPP17
561+
// interpret message content as a string
562+
std::string_view to_string_view() const noexcept
563+
{
564+
return std::string_view(static_cast<const char*>(data()), size());
565+
}
566+
#endif
567+
568+
/** Dump content to string for debugging.
569+
* Ascii chars are readable, the rest is printed as hex.
570+
* Probably ridiculously slow.
571+
* Use to_string() or to_string_view() for
572+
* interpreting the message as a string.
573+
*/
558574
std::string str() const
559575
{
560576
// Partly mutuated from the same method in zmq::multipart_t

0 commit comments

Comments
 (0)