Skip to content

Commit

Permalink
Merge 9476de9 into 9906eb5
Browse files Browse the repository at this point in the history
  • Loading branch information
cpjulia committed Sep 1, 2022
2 parents 9906eb5 + 9476de9 commit ff4323f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
3 changes: 3 additions & 0 deletions include/velocypack/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ struct Options {
// JSON with a Dumper
bool escapeForwardSlashes = false;

// with a Dumper (creates \uxxxx sequences or displays '\n', '\r' or \'t')
bool escapeControl = true;

// escape multi-byte Unicode characters when dumping them to JSON
// with a Dumper (creates \uxxxx sequences)
bool escapeUnicode = false;
Expand Down
30 changes: 17 additions & 13 deletions src/Dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,25 @@ void Dumper::dumpString(char const* src, ValueLength len) {
char esc = EscapeTable[c];

if (esc) {
if (c != '/' || options->escapeForwardSlashes) {
// escape forward slashes only when requested
_sink->push_back('\\');
}
_sink->push_back(static_cast<char>(esc));
if (options->escapeControl) {
if (c != '/' || options->escapeForwardSlashes) {
// escape forward slashes only when requested
_sink->push_back('\\');
}
_sink->push_back(static_cast<char>(esc));

if (esc == 'u') {
uint16_t i1 = (((uint16_t)c) & 0xf0U) >> 4;
uint16_t i2 = (((uint16_t)c) & 0x0fU);
if (esc == 'u') {
uint16_t i1 = (((uint16_t)c) & 0xf0U) >> 4;
uint16_t i2 = (((uint16_t)c) & 0x0fU);

_sink->append("00", 2);
_sink->push_back(
static_cast<char>((i1 < 10) ? ('0' + i1) : ('A' + i1 - 10)));
_sink->push_back(
static_cast<char>((i2 < 10) ? ('0' + i2) : ('A' + i2 - 10)));
_sink->append("00", 2);
_sink->push_back(
static_cast<char>((i1 < 10) ? ('0' + i1) : ('A' + i1 - 10)));
_sink->push_back(
static_cast<char>((i2 < 10) ? ('0' + i2) : ('A' + i2 - 10)));
}
} else {
_sink->push_back(' ');
}
} else {
_sink->push_back(static_cast<char>(c));
Expand Down
20 changes: 20 additions & 0 deletions tests/testsDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,26 @@ TEST(StringDumperTest, StringControlChars) {
Dumper::toString(slice));
}

TEST(StringDumperTest, SuppressControlChars) {
Builder b;
b.add(Value("Before\nAfter\r\t\v\x01\x02"));

Options options;
options.escapeControl = false;
ASSERT_EQ(std::string("\"Before After \""),
Dumper::toString(b.slice(), &options));
}

TEST(StringDumperTest, EscapeControlChars) {
Builder b;
b.add(Value("Before\nAfter\r\t\v\x01\x02"));

Options options;
options.escapeControl = true;
ASSERT_EQ(std::string("\"Before\\nAfter\\r\\t\\u000B\\u0001\\u0002\""),
Dumper::toString(b.slice(), &options));
}

TEST(StringDumperTest, StringUTF8) {
Builder b;
b.add(Value("mötör"));
Expand Down

0 comments on commit ff4323f

Please sign in to comment.