From e36cff19f098723a45a98f53bba113da3c3781f3 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Tue, 12 May 2020 16:19:36 -0700 Subject: [PATCH] clang-tidy + any_of usage (#1171) * [clang-tidy] change functions to static Found with readability-convert-member-functions-to-static Signed-off-by: Rosen Penev * optimize JsonWriter::validate #1171 * do the same for json_reader Signed-off-by: Rosen Penev * use std::any_of Also simplified two loops. Signed-off-by: Rosen Penev Co-authored-by: Billy Donahue --- src/lib_json/json_reader.cpp | 65 ++++++++++++++++-------------------- src/lib_json/json_writer.cpp | 59 +++++++++++++++----------------- src/test_lib_json/main.cpp | 4 +-- 3 files changed, 56 insertions(+), 72 deletions(-) diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 02a7d54..2dca4ca 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -10,6 +10,7 @@ #include #include #endif // if !defined(JSON_IS_AMALGAMATION) +#include #include #include #include @@ -77,10 +78,7 @@ Features Features::strictMode() { // //////////////////////////////// bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) { - for (; begin < end; ++begin) - if (*begin == '\n' || *begin == '\r') - return true; - return false; + return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; }); } // Class Reader @@ -998,10 +996,7 @@ private: bool OurReader::containsNewLine(OurReader::Location begin, OurReader::Location end) { - for (; begin < end; ++begin) - if (*begin == '\n' || *begin == '\r') - return true; - return false; + return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; }); } OurReader::OurReader(OurFeatures const& features) : features_(features) {} @@ -1902,38 +1897,34 @@ CharReader* CharReaderBuilder::newCharReader() const { features.skipBom_ = settings_["skipBom"].asBool(); return new OurCharReader(collectComments, features); } -static void getValidReaderKeys(std::set* valid_keys) { - valid_keys->clear(); - valid_keys->insert("collectComments"); - valid_keys->insert("allowComments"); - valid_keys->insert("allowTrailingCommas"); - valid_keys->insert("strictRoot"); - valid_keys->insert("allowDroppedNullPlaceholders"); - valid_keys->insert("allowNumericKeys"); - valid_keys->insert("allowSingleQuotes"); - valid_keys->insert("stackLimit"); - valid_keys->insert("failIfExtra"); - valid_keys->insert("rejectDupKeys"); - valid_keys->insert("allowSpecialFloats"); - valid_keys->insert("skipBom"); -} + bool CharReaderBuilder::validate(Json::Value* invalid) const { - Json::Value my_invalid; - if (!invalid) - invalid = &my_invalid; // so we do not need to test for NULL - Json::Value& inv = *invalid; - std::set valid_keys; - getValidReaderKeys(&valid_keys); - Value::Members keys = settings_.getMemberNames(); - size_t n = keys.size(); - for (size_t i = 0; i < n; ++i) { - String const& key = keys[i]; - if (valid_keys.find(key) == valid_keys.end()) { - inv[key] = settings_[key]; - } + static const auto& valid_keys = *new std::set{ + "collectComments", + "allowComments", + "allowTrailingCommas", + "strictRoot", + "allowDroppedNullPlaceholders", + "allowNumericKeys", + "allowSingleQuotes", + "stackLimit", + "failIfExtra", + "rejectDupKeys", + "allowSpecialFloats", + "skipBom", + }; + for (auto si = settings_.begin(); si != settings_.end(); ++si) { + auto key = si.name(); + if (valid_keys.count(key)) + continue; + if (invalid) + (*invalid)[std::move(key)] = *si; + else + return false; } - return inv.empty(); + return invalid ? invalid->empty() : true; } + Value& CharReaderBuilder::operator[](const String& key) { return settings_[key]; } diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 56195dc..cb528b8 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -7,7 +7,9 @@ #include "json_tool.h" #include #endif // if !defined(JSON_IS_AMALGAMATION) +#include #include +#include #include #include #include @@ -176,14 +178,9 @@ String valueToString(bool value) { return value ? "true" : "false"; } static bool isAnyCharRequiredQuoting(char const* s, size_t n) { assert(s || !n); - char const* const end = s + n; - for (char const* cur = s; cur < end; ++cur) { - if (*cur == '\\' || *cur == '\"' || - static_cast(*cur) < ' ' || - static_cast(*cur) >= 0x80) - return true; - } - return false; + return std::any_of(s, s + n, [](int c) { + return c == '\\' || c == '"' || !std::isprint(c); + }); } static unsigned int utf8ToCodepoint(const char*& s, const char* e) { @@ -1198,34 +1195,30 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const { endingLineFeedSymbol, usf, emitUTF8, pre, precisionType); } -static void getValidWriterKeys(std::set* valid_keys) { - valid_keys->clear(); - valid_keys->insert("indentation"); - valid_keys->insert("commentStyle"); - valid_keys->insert("enableYAMLCompatibility"); - valid_keys->insert("dropNullPlaceholders"); - valid_keys->insert("useSpecialFloats"); - valid_keys->insert("emitUTF8"); - valid_keys->insert("precision"); - valid_keys->insert("precisionType"); -} + bool StreamWriterBuilder::validate(Json::Value* invalid) const { - Json::Value my_invalid; - if (!invalid) - invalid = &my_invalid; // so we do not need to test for NULL - Json::Value& inv = *invalid; - std::set valid_keys; - getValidWriterKeys(&valid_keys); - Value::Members keys = settings_.getMemberNames(); - size_t n = keys.size(); - for (size_t i = 0; i < n; ++i) { - String const& key = keys[i]; - if (valid_keys.find(key) == valid_keys.end()) { - inv[key] = settings_[key]; - } + static const auto& valid_keys = *new std::set{ + "indentation", + "commentStyle", + "enableYAMLCompatibility", + "dropNullPlaceholders", + "useSpecialFloats", + "emitUTF8", + "precision", + "precisionType", + }; + for (auto si = settings_.begin(); si != settings_.end(); ++si) { + auto key = si.name(); + if (valid_keys.count(key)) + continue; + if (invalid) + (*invalid)[std::move(key)] = *si; + else + return false; } - return inv.empty(); + return invalid ? invalid->empty() : true; } + Value& StreamWriterBuilder::operator[](const String& key) { return settings_[key]; } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index e6fe43d..73850cf 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3286,11 +3286,11 @@ struct CharReaderAllowDropNullTest : JsonTest::TestCase { return [=](const Value& root) { JSONTEST_ASSERT_EQUAL(root, v); }; } - ValueCheck objGetAnd(std::string idx, ValueCheck f) { + static ValueCheck objGetAnd(std::string idx, ValueCheck f) { return [=](const Value& root) { f(root.get(idx, true)); }; } - ValueCheck arrGetAnd(int idx, ValueCheck f) { + static ValueCheck arrGetAnd(int idx, ValueCheck f) { return [=](const Value& root) { f(root[idx]); }; } };