fix escaping of double quote in JSON response, fixes #1410 and also adds a unit test for that case.
This commit is contained in:
parent
559a2dcec7
commit
3a291f5c8e
@ -36,9 +36,9 @@ BOOST_AUTO_TEST_SUITE(string_util)
|
|||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(replace_all_test)
|
BOOST_AUTO_TEST_CASE(replace_all_test)
|
||||||
{
|
{
|
||||||
std::string input {"ababababababa"};
|
std::string input{"ababababababa"};
|
||||||
const std::string sub {"a"};
|
const std::string sub{"a"};
|
||||||
const std::string other {"c"};
|
const std::string other{"c"};
|
||||||
replaceAll(input, sub, other);
|
replaceAll(input, sub, other);
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL(input, "cbcbcbcbcbcbc");
|
BOOST_CHECK_EQUAL(input, "cbcbcbcbcbcbc");
|
||||||
@ -46,15 +46,19 @@ BOOST_AUTO_TEST_CASE(replace_all_test)
|
|||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(json_escaping)
|
BOOST_AUTO_TEST_CASE(json_escaping)
|
||||||
{
|
{
|
||||||
const std::string input {"\b\\"};
|
std::string input{"\b\\"};
|
||||||
const std::string output { escape_JSON(input) };
|
std::string output{escape_JSON(input)};
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL(output, "\\b\\\\");
|
BOOST_CHECK_EQUAL(output, "\\b\\\\");
|
||||||
|
|
||||||
|
input = "Aleja \"Solidarnosci\"";
|
||||||
|
output = escape_JSON(input);
|
||||||
|
BOOST_CHECK_EQUAL(output, "Aleja \\\"Solidarnosci\\\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(print_int)
|
BOOST_AUTO_TEST_CASE(print_int)
|
||||||
{
|
{
|
||||||
const std::string input {"\b\\"};
|
const std::string input{"\b\\"};
|
||||||
char buffer[12];
|
char buffer[12];
|
||||||
buffer[11] = 0; // zero termination
|
buffer[11] = 0; // zero termination
|
||||||
std::string output = printInt<11, 8>(buffer, 314158976);
|
std::string output = printInt<11, 8>(buffer, 314158976);
|
||||||
@ -65,6 +69,7 @@ BOOST_AUTO_TEST_CASE(print_int)
|
|||||||
BOOST_CHECK_EQUAL(output, "0.00000000");
|
BOOST_CHECK_EQUAL(output, "0.00000000");
|
||||||
|
|
||||||
output = printInt<11, 8>(buffer, -314158976);
|
output = printInt<11, 8>(buffer, -314158976);
|
||||||
BOOST_CHECK_EQUAL(output, "-3.14158976");}
|
BOOST_CHECK_EQUAL(output, "-3.14158976");
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
@ -44,7 +44,8 @@ template <int length, int precision> static inline char *printInt(char *buffer,
|
|||||||
static_assert(length > 0, "length must be positive");
|
static_assert(length > 0, "length must be positive");
|
||||||
static_assert(precision > 0, "precision must be positive");
|
static_assert(precision > 0, "precision must be positive");
|
||||||
|
|
||||||
const bool minus = [&value]{
|
const bool minus = [&value]
|
||||||
|
{
|
||||||
if (value >= 0)
|
if (value >= 0)
|
||||||
{
|
{
|
||||||
value = -value;
|
value = -value;
|
||||||
@ -89,12 +90,6 @@ inline void replaceAll(std::string &s, const std::string &sub, const std::string
|
|||||||
|
|
||||||
inline std::string escape_JSON(const std::string &input)
|
inline std::string escape_JSON(const std::string &input)
|
||||||
{
|
{
|
||||||
// return the input if no backslash can be found -> no allocations
|
|
||||||
if (input.find_first_of('\\') == std::string::npos)
|
|
||||||
{
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
// escape and skip reallocations if possible
|
// escape and skip reallocations if possible
|
||||||
std::string output;
|
std::string output;
|
||||||
output.reserve(input.size() + 4); // +4 assumes two backslashes on avg
|
output.reserve(input.size() + 4); // +4 assumes two backslashes on avg
|
||||||
|
Loading…
Reference in New Issue
Block a user