Skip to content

Commit e36cff1

Browse files
nehebBillyDonahue
andauthored
clang-tidy + any_of usage (open-source-parsers#1171)
* [clang-tidy] change functions to static Found with readability-convert-member-functions-to-static Signed-off-by: Rosen Penev <[email protected]> * optimize JsonWriter::validate open-source-parsers#1171 * do the same for json_reader Signed-off-by: Rosen Penev <[email protected]> * use std::any_of Also simplified two loops. Signed-off-by: Rosen Penev <[email protected]> Co-authored-by: Billy Donahue <[email protected]>
1 parent b8cb888 commit e36cff1

File tree

3 files changed

+56
-72
lines changed

3 files changed

+56
-72
lines changed

src/lib_json/json_reader.cpp

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <json/reader.h>
1111
#include <json/value.h>
1212
#endif // if !defined(JSON_IS_AMALGAMATION)
13+
#include <algorithm>
1314
#include <cassert>
1415
#include <cstring>
1516
#include <iostream>
@@ -77,10 +78,7 @@ Features Features::strictMode() {
7778
// ////////////////////////////////
7879

7980
bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
80-
for (; begin < end; ++begin)
81-
if (*begin == '\n' || *begin == '\r')
82-
return true;
83-
return false;
81+
return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
8482
}
8583

8684
// Class Reader
@@ -998,10 +996,7 @@ class OurReader {
998996

999997
bool OurReader::containsNewLine(OurReader::Location begin,
1000998
OurReader::Location end) {
1001-
for (; begin < end; ++begin)
1002-
if (*begin == '\n' || *begin == '\r')
1003-
return true;
1004-
return false;
999+
return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
10051000
}
10061001

10071002
OurReader::OurReader(OurFeatures const& features) : features_(features) {}
@@ -1902,38 +1897,34 @@ CharReader* CharReaderBuilder::newCharReader() const {
19021897
features.skipBom_ = settings_["skipBom"].asBool();
19031898
return new OurCharReader(collectComments, features);
19041899
}
1905-
static void getValidReaderKeys(std::set<String>* valid_keys) {
1906-
valid_keys->clear();
1907-
valid_keys->insert("collectComments");
1908-
valid_keys->insert("allowComments");
1909-
valid_keys->insert("allowTrailingCommas");
1910-
valid_keys->insert("strictRoot");
1911-
valid_keys->insert("allowDroppedNullPlaceholders");
1912-
valid_keys->insert("allowNumericKeys");
1913-
valid_keys->insert("allowSingleQuotes");
1914-
valid_keys->insert("stackLimit");
1915-
valid_keys->insert("failIfExtra");
1916-
valid_keys->insert("rejectDupKeys");
1917-
valid_keys->insert("allowSpecialFloats");
1918-
valid_keys->insert("skipBom");
1919-
}
1900+
19201901
bool CharReaderBuilder::validate(Json::Value* invalid) const {
1921-
Json::Value my_invalid;
1922-
if (!invalid)
1923-
invalid = &my_invalid; // so we do not need to test for NULL
1924-
Json::Value& inv = *invalid;
1925-
std::set<String> valid_keys;
1926-
getValidReaderKeys(&valid_keys);
1927-
Value::Members keys = settings_.getMemberNames();
1928-
size_t n = keys.size();
1929-
for (size_t i = 0; i < n; ++i) {
1930-
String const& key = keys[i];
1931-
if (valid_keys.find(key) == valid_keys.end()) {
1932-
inv[key] = settings_[key];
1933-
}
1902+
static const auto& valid_keys = *new std::set<String>{
1903+
"collectComments",
1904+
"allowComments",
1905+
"allowTrailingCommas",
1906+
"strictRoot",
1907+
"allowDroppedNullPlaceholders",
1908+
"allowNumericKeys",
1909+
"allowSingleQuotes",
1910+
"stackLimit",
1911+
"failIfExtra",
1912+
"rejectDupKeys",
1913+
"allowSpecialFloats",
1914+
"skipBom",
1915+
};
1916+
for (auto si = settings_.begin(); si != settings_.end(); ++si) {
1917+
auto key = si.name();
1918+
if (valid_keys.count(key))
1919+
continue;
1920+
if (invalid)
1921+
(*invalid)[std::move(key)] = *si;
1922+
else
1923+
return false;
19341924
}
1935-
return inv.empty();
1925+
return invalid ? invalid->empty() : true;
19361926
}
1927+
19371928
Value& CharReaderBuilder::operator[](const String& key) {
19381929
return settings_[key];
19391930
}

src/lib_json/json_writer.cpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "json_tool.h"
88
#include <json/writer.h>
99
#endif // if !defined(JSON_IS_AMALGAMATION)
10+
#include <algorithm>
1011
#include <cassert>
12+
#include <cctype>
1113
#include <cstring>
1214
#include <iomanip>
1315
#include <memory>
@@ -176,14 +178,9 @@ String valueToString(bool value) { return value ? "true" : "false"; }
176178
static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
177179
assert(s || !n);
178180

179-
char const* const end = s + n;
180-
for (char const* cur = s; cur < end; ++cur) {
181-
if (*cur == '\\' || *cur == '\"' ||
182-
static_cast<unsigned char>(*cur) < ' ' ||
183-
static_cast<unsigned char>(*cur) >= 0x80)
184-
return true;
185-
}
186-
return false;
181+
return std::any_of(s, s + n, [](int c) {
182+
return c == '\\' || c == '"' || !std::isprint(c);
183+
});
187184
}
188185

189186
static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
@@ -1198,34 +1195,30 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const {
11981195
endingLineFeedSymbol, usf, emitUTF8, pre,
11991196
precisionType);
12001197
}
1201-
static void getValidWriterKeys(std::set<String>* valid_keys) {
1202-
valid_keys->clear();
1203-
valid_keys->insert("indentation");
1204-
valid_keys->insert("commentStyle");
1205-
valid_keys->insert("enableYAMLCompatibility");
1206-
valid_keys->insert("dropNullPlaceholders");
1207-
valid_keys->insert("useSpecialFloats");
1208-
valid_keys->insert("emitUTF8");
1209-
valid_keys->insert("precision");
1210-
valid_keys->insert("precisionType");
1211-
}
1198+
12121199
bool StreamWriterBuilder::validate(Json::Value* invalid) const {
1213-
Json::Value my_invalid;
1214-
if (!invalid)
1215-
invalid = &my_invalid; // so we do not need to test for NULL
1216-
Json::Value& inv = *invalid;
1217-
std::set<String> valid_keys;
1218-
getValidWriterKeys(&valid_keys);
1219-
Value::Members keys = settings_.getMemberNames();
1220-
size_t n = keys.size();
1221-
for (size_t i = 0; i < n; ++i) {
1222-
String const& key = keys[i];
1223-
if (valid_keys.find(key) == valid_keys.end()) {
1224-
inv[key] = settings_[key];
1225-
}
1200+
static const auto& valid_keys = *new std::set<String>{
1201+
"indentation",
1202+
"commentStyle",
1203+
"enableYAMLCompatibility",
1204+
"dropNullPlaceholders",
1205+
"useSpecialFloats",
1206+
"emitUTF8",
1207+
"precision",
1208+
"precisionType",
1209+
};
1210+
for (auto si = settings_.begin(); si != settings_.end(); ++si) {
1211+
auto key = si.name();
1212+
if (valid_keys.count(key))
1213+
continue;
1214+
if (invalid)
1215+
(*invalid)[std::move(key)] = *si;
1216+
else
1217+
return false;
12261218
}
1227-
return inv.empty();
1219+
return invalid ? invalid->empty() : true;
12281220
}
1221+
12291222
Value& StreamWriterBuilder::operator[](const String& key) {
12301223
return settings_[key];
12311224
}

src/test_lib_json/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,11 +3286,11 @@ struct CharReaderAllowDropNullTest : JsonTest::TestCase {
32863286
return [=](const Value& root) { JSONTEST_ASSERT_EQUAL(root, v); };
32873287
}
32883288

3289-
ValueCheck objGetAnd(std::string idx, ValueCheck f) {
3289+
static ValueCheck objGetAnd(std::string idx, ValueCheck f) {
32903290
return [=](const Value& root) { f(root.get(idx, true)); };
32913291
}
32923292

3293-
ValueCheck arrGetAnd(int idx, ValueCheck f) {
3293+
static ValueCheck arrGetAnd(int idx, ValueCheck f) {
32943294
return [=](const Value& root) { f(root[idx]); };
32953295
}
32963296
};

0 commit comments

Comments
 (0)