Skip to content

Billy donahue avoid isprint #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ String valueToString(double value, unsigned int precision,

String valueToString(bool value) { return value ? "true" : "false"; }

static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
static bool doesAnyCharRequireEscaping(char const* s, size_t n) {
assert(s || !n);

return std::any_of(s, s + n, [](unsigned char c) {
return c == '\\' || c == '"' || !std::isprint(c);
return c == '\\' || c == '"' || c < 0x20 || c > 0x7F;
});
}

Expand Down Expand Up @@ -275,7 +275,7 @@ static String valueToQuotedStringN(const char* value, unsigned length,
if (value == nullptr)
return "";

if (!isAnyCharRequiredQuoting(value, length))
if (!doesAnyCharRequireEscaping(value, length))
return String("\"") + value + "\"";
// We have to walk value and escape any special characters.
// Appending to String is not efficient, but this should be rare.
Expand Down
28 changes: 28 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,34 @@ JSONTEST_FIXTURE_LOCAL(StreamWriterTest, escapeControlCharacters) {
}
}

#ifdef _WIN32
JSONTEST_FIXTURE_LOCAL(StreamWriterTest, escapeTabCharacterWindows) {
// Get the current locale before changing it
std::string currentLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "English_United States.1252");

Json::Value root;
root["test"] = "\tTabTesting\t";

Json::StreamWriterBuilder b;

JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"test\" : "
"\"\\tTabTesting\\t\"\n}");

b.settings_["emitUTF8"] = true;
JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"test\" : "
"\"\\tTabTesting\\t\"\n}");

b.settings_["emitUTF8"] = false;
JSONTEST_ASSERT(Json::writeString(b, root) == "{\n\t\"test\" : "
"\"\\tTabTesting\\t\"\n}");

// Restore the locale
if (!currentLocale.empty())
setlocale(LC_ALL, currentLocale.c_str());
}
#endif

struct ReaderTest : JsonTest::TestCase {
void setStrictMode() {
reader = std::unique_ptr<Json::Reader>(
Expand Down