Skip to content

Commit

Permalink
watchman: remove use of jansson private strbuffer type from bser test
Browse files Browse the repository at this point in the history
Summary:
this test pre-dates our use of C++.  We're now able to replace
strbuffer with std::string

Reviewed By: pkaush

Differential Revision: D17779597

fbshipit-source-id: cdcbc555ed3a2624df85c4388e961129d483b3c8
  • Loading branch information
wez authored and facebook-github-bot committed Oct 10, 2019
1 parent ed6facb commit 0611453
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions tests/bser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
#include <folly/logging/xlog.h>
#include <folly/portability/GTest.h>
#include "thirdparty/jansson/jansson_private.h"
#include "thirdparty/jansson/strbuffer.h"

#define UTF8_PILE_OF_POO "\xf0\x9f\x92\xa9"

// Construct a std::string from a literal that may have embedded NUL bytes.
// The -1 compensates for the NUL terminator that is included in sizeof()
#define S(str_literal) std::string(str_literal, sizeof(str_literal) - 1)

static int dump_to_strbuffer(const char* buffer, size_t size, void* data) {
return strbuffer_append_bytes((strbuffer_t*)data, buffer, size);
static int dump_to_string(const char* buffer, size_t size, void* data) {
auto str = (std::string*)data;
str->append(buffer, size);
return 0;
}

static void hexdump(const char* start, const char* end) {
Expand Down Expand Up @@ -46,39 +47,23 @@ static void hexdump(const char* start, const char* end) {

static std::unique_ptr<std::string>
bdumps(uint32_t version, uint32_t capabilities, const json_ref& json) {
strbuffer_t strbuff;
bser_ctx_t ctx{version, capabilities, dump_to_strbuffer};
std::string buffer;
bser_ctx_t ctx{version, capabilities, dump_to_string};

if (strbuffer_init(&strbuff)) {
return nullptr;
}

SCOPE_EXIT {
strbuffer_close(&strbuff);
};

if (w_bser_dump(&ctx, json, &strbuff) == 0) {
return std::make_unique<std::string>(strbuff.value, strbuff.length);
if (w_bser_dump(&ctx, json, &buffer) == 0) {
return std::make_unique<std::string>(std::move(buffer));
}

return nullptr;
}

static std::unique_ptr<std::string>
bdumps_pdu(uint32_t version, uint32_t capabilities, const json_ref& json) {
strbuffer_t strbuff;

if (strbuffer_init(&strbuff)) {
return nullptr;
}

SCOPE_EXIT {
strbuffer_close(&strbuff);
};
std::string buffer;

if (w_bser_write_pdu(
version, capabilities, dump_to_strbuffer, json, &strbuff) == 0) {
return std::make_unique<std::string>(strbuff.value, strbuff.length);
if (w_bser_write_pdu(version, capabilities, dump_to_string, json, &buffer) ==
0) {
return std::make_unique<std::string>(std::move(buffer));
}

return nullptr;
Expand Down

0 comments on commit 0611453

Please sign in to comment.