Skip to content

Commit b2fa119

Browse files
authored
Merge pull request #343 from gummif/gfa/str-buffer
Problem: Sending string literals is awkward
2 parents 88ada98 + 7d9e5cb commit b2fa119

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

tests/buffer.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,52 @@ TEST_CASE("const_buffer creation string_view", "[buffer]")
236236
}
237237
#endif
238238

239+
TEST_CASE("const_buffer creation with str_buffer", "[buffer]")
240+
{
241+
const wchar_t wd[10] = {};
242+
zmq::const_buffer b = zmq::str_buffer(wd);
243+
CHECK(b.size() == 9 * sizeof(wchar_t));
244+
CHECK(b.data() == static_cast<const wchar_t*>(wd));
245+
246+
zmq::const_buffer b2_null = zmq::buffer("hello");
247+
constexpr zmq::const_buffer b2 = zmq::str_buffer("hello");
248+
CHECK(b2_null.size() == 6);
249+
CHECK(b2.size() == 5);
250+
CHECK(std::string(static_cast<const char*>(b2.data()), b2.size()) == "hello");
251+
}
252+
253+
TEST_CASE("const_buffer creation with zbuf string literal char", "[buffer]")
254+
{
255+
using namespace zmq::literals;
256+
constexpr zmq::const_buffer b = "hello"_zbuf;
257+
CHECK(b.size() == 5);
258+
CHECK(std::memcmp(b.data(), "hello", b.size()) == 0);
259+
}
260+
261+
TEST_CASE("const_buffer creation with zbuf string literal wchar_t", "[buffer]")
262+
{
263+
using namespace zmq::literals;
264+
constexpr zmq::const_buffer b = L"hello"_zbuf;
265+
CHECK(b.size() == 5 * sizeof(wchar_t));
266+
CHECK(std::memcmp(b.data(), L"hello", b.size()) == 0);
267+
}
268+
269+
TEST_CASE("const_buffer creation with zbuf string literal char16_t", "[buffer]")
270+
{
271+
using namespace zmq::literals;
272+
constexpr zmq::const_buffer b = u"hello"_zbuf;
273+
CHECK(b.size() == 5 * sizeof(char16_t));
274+
CHECK(std::memcmp(b.data(), u"hello", b.size()) == 0);
275+
}
276+
277+
TEST_CASE("const_buffer creation with zbuf string literal char32_t", "[buffer]")
278+
{
279+
using namespace zmq::literals;
280+
constexpr zmq::const_buffer b = U"hello"_zbuf;
281+
CHECK(b.size() == 5 * sizeof(char32_t));
282+
CHECK(std::memcmp(b.data(), U"hello", b.size()) == 0);
283+
}
284+
239285
TEST_CASE("buffer of structs", "[buffer]")
240286
{
241287
struct some_pod

zmq.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,40 @@ const_buffer buffer(std::basic_string_view<T, Traits> data, size_t n_bytes) noex
11101110
}
11111111
#endif
11121112

1113+
// Buffer for a string literal (null terminated)
1114+
// where the buffer size excludes the terminating character.
1115+
// Equivalent to zmq::buffer(std::string_view("...")).
1116+
template<class Char, size_t N>
1117+
constexpr const_buffer str_buffer(const Char (&data)[N]) noexcept
1118+
{
1119+
static_assert(detail::is_pod_like<Char>::value, "Char must be POD");
1120+
#ifdef ZMQ_CPP14
1121+
assert(data[N - 1] == Char{0});
1122+
#endif
1123+
return const_buffer(static_cast<const Char*>(data),
1124+
(N - 1) * sizeof(Char));
1125+
}
1126+
1127+
namespace literals
1128+
{
1129+
constexpr const_buffer operator"" _zbuf(const char* str, size_t len) noexcept
1130+
{
1131+
return const_buffer(str, len * sizeof(char));
1132+
}
1133+
constexpr const_buffer operator"" _zbuf(const wchar_t* str, size_t len) noexcept
1134+
{
1135+
return const_buffer(str, len * sizeof(wchar_t));
1136+
}
1137+
constexpr const_buffer operator"" _zbuf(const char16_t* str, size_t len) noexcept
1138+
{
1139+
return const_buffer(str, len * sizeof(char16_t));
1140+
}
1141+
constexpr const_buffer operator"" _zbuf(const char32_t* str, size_t len) noexcept
1142+
{
1143+
return const_buffer(str, len * sizeof(char32_t));
1144+
}
1145+
}
1146+
11131147
#endif // ZMQ_CPP11
11141148

11151149
namespace detail

0 commit comments

Comments
 (0)