mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-27 11:21:02 +08:00
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 <rosenp@gmail.com> * optimize JsonWriter::validate #1171 * do the same for json_reader Signed-off-by: Rosen Penev <rosenp@gmail.com> * use std::any_of Also simplified two loops. Signed-off-by: Rosen Penev <rosenp@gmail.com> Co-authored-by: Billy Donahue <billy.donahue@gmail.com>
This commit is contained in:
parent
b8cb8889aa
commit
e36cff19f0
@ -10,6 +10,7 @@
|
|||||||
#include <json/reader.h>
|
#include <json/reader.h>
|
||||||
#include <json/value.h>
|
#include <json/value.h>
|
||||||
#endif // if !defined(JSON_IS_AMALGAMATION)
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -77,10 +78,7 @@ Features Features::strictMode() {
|
|||||||
// ////////////////////////////////
|
// ////////////////////////////////
|
||||||
|
|
||||||
bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
|
bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
|
||||||
for (; begin < end; ++begin)
|
return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
|
||||||
if (*begin == '\n' || *begin == '\r')
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class Reader
|
// Class Reader
|
||||||
@ -998,10 +996,7 @@ private:
|
|||||||
|
|
||||||
bool OurReader::containsNewLine(OurReader::Location begin,
|
bool OurReader::containsNewLine(OurReader::Location begin,
|
||||||
OurReader::Location end) {
|
OurReader::Location end) {
|
||||||
for (; begin < end; ++begin)
|
return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
|
||||||
if (*begin == '\n' || *begin == '\r')
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OurReader::OurReader(OurFeatures const& features) : features_(features) {}
|
OurReader::OurReader(OurFeatures const& features) : features_(features) {}
|
||||||
@ -1902,38 +1897,34 @@ CharReader* CharReaderBuilder::newCharReader() const {
|
|||||||
features.skipBom_ = settings_["skipBom"].asBool();
|
features.skipBom_ = settings_["skipBom"].asBool();
|
||||||
return new OurCharReader(collectComments, features);
|
return new OurCharReader(collectComments, features);
|
||||||
}
|
}
|
||||||
static void getValidReaderKeys(std::set<String>* 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 {
|
bool CharReaderBuilder::validate(Json::Value* invalid) const {
|
||||||
Json::Value my_invalid;
|
static const auto& valid_keys = *new std::set<String>{
|
||||||
if (!invalid)
|
"collectComments",
|
||||||
invalid = &my_invalid; // so we do not need to test for NULL
|
"allowComments",
|
||||||
Json::Value& inv = *invalid;
|
"allowTrailingCommas",
|
||||||
std::set<String> valid_keys;
|
"strictRoot",
|
||||||
getValidReaderKeys(&valid_keys);
|
"allowDroppedNullPlaceholders",
|
||||||
Value::Members keys = settings_.getMemberNames();
|
"allowNumericKeys",
|
||||||
size_t n = keys.size();
|
"allowSingleQuotes",
|
||||||
for (size_t i = 0; i < n; ++i) {
|
"stackLimit",
|
||||||
String const& key = keys[i];
|
"failIfExtra",
|
||||||
if (valid_keys.find(key) == valid_keys.end()) {
|
"rejectDupKeys",
|
||||||
inv[key] = settings_[key];
|
"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 invalid ? invalid->empty() : true;
|
||||||
}
|
}
|
||||||
return inv.empty();
|
|
||||||
}
|
|
||||||
Value& CharReaderBuilder::operator[](const String& key) {
|
Value& CharReaderBuilder::operator[](const String& key) {
|
||||||
return settings_[key];
|
return settings_[key];
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
#include "json_tool.h"
|
#include "json_tool.h"
|
||||||
#include <json/writer.h>
|
#include <json/writer.h>
|
||||||
#endif // if !defined(JSON_IS_AMALGAMATION)
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cctype>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -176,14 +178,9 @@ String valueToString(bool value) { return value ? "true" : "false"; }
|
|||||||
static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
|
static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
|
||||||
assert(s || !n);
|
assert(s || !n);
|
||||||
|
|
||||||
char const* const end = s + n;
|
return std::any_of(s, s + n, [](int c) {
|
||||||
for (char const* cur = s; cur < end; ++cur) {
|
return c == '\\' || c == '"' || !std::isprint(c);
|
||||||
if (*cur == '\\' || *cur == '\"' ||
|
});
|
||||||
static_cast<unsigned char>(*cur) < ' ' ||
|
|
||||||
static_cast<unsigned char>(*cur) >= 0x80)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
|
static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
|
||||||
@ -1198,34 +1195,30 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
|
|||||||
endingLineFeedSymbol, usf, emitUTF8, pre,
|
endingLineFeedSymbol, usf, emitUTF8, pre,
|
||||||
precisionType);
|
precisionType);
|
||||||
}
|
}
|
||||||
static void getValidWriterKeys(std::set<String>* 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 {
|
bool StreamWriterBuilder::validate(Json::Value* invalid) const {
|
||||||
Json::Value my_invalid;
|
static const auto& valid_keys = *new std::set<String>{
|
||||||
if (!invalid)
|
"indentation",
|
||||||
invalid = &my_invalid; // so we do not need to test for NULL
|
"commentStyle",
|
||||||
Json::Value& inv = *invalid;
|
"enableYAMLCompatibility",
|
||||||
std::set<String> valid_keys;
|
"dropNullPlaceholders",
|
||||||
getValidWriterKeys(&valid_keys);
|
"useSpecialFloats",
|
||||||
Value::Members keys = settings_.getMemberNames();
|
"emitUTF8",
|
||||||
size_t n = keys.size();
|
"precision",
|
||||||
for (size_t i = 0; i < n; ++i) {
|
"precisionType",
|
||||||
String const& key = keys[i];
|
};
|
||||||
if (valid_keys.find(key) == valid_keys.end()) {
|
for (auto si = settings_.begin(); si != settings_.end(); ++si) {
|
||||||
inv[key] = settings_[key];
|
auto key = si.name();
|
||||||
|
if (valid_keys.count(key))
|
||||||
|
continue;
|
||||||
|
if (invalid)
|
||||||
|
(*invalid)[std::move(key)] = *si;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
return invalid ? invalid->empty() : true;
|
||||||
}
|
}
|
||||||
return inv.empty();
|
|
||||||
}
|
|
||||||
Value& StreamWriterBuilder::operator[](const String& key) {
|
Value& StreamWriterBuilder::operator[](const String& key) {
|
||||||
return settings_[key];
|
return settings_[key];
|
||||||
}
|
}
|
||||||
|
@ -3286,11 +3286,11 @@ struct CharReaderAllowDropNullTest : JsonTest::TestCase {
|
|||||||
return [=](const Value& root) { JSONTEST_ASSERT_EQUAL(root, v); };
|
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)); };
|
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]); };
|
return [=](const Value& root) { f(root[idx]); };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user