Skip to content

Commit 53650bd

Browse files
committed
PERF: readability container size empty
The emptiness of a container should be checked using the empty() method instead of the size() method. It is not guaranteed that size() is a constant-time function, and it is generally more efficient and also shows clearer intent to use empty(). Furthermore some containers may implement the empty() method but not implement the size() method. Using empty() whenever possible makes it easier to switch to another container in the future. SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/ run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,readability-container-size-empty -header-filter=.* -fix
1 parent 71c3a79 commit 53650bd

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

Diff for: src/lib_json/json_reader.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ bool Reader::pushError(const Value& value,
874874
return true;
875875
}
876876

877-
bool Reader::good() const { return !errors_.size(); }
877+
bool Reader::good() const { return errors_.empty(); }
878878

879879
// exact copy of Features
880880
class OurFeatures {
@@ -1885,7 +1885,7 @@ bool OurReader::pushError(const Value& value,
18851885
return true;
18861886
}
18871887

1888-
bool OurReader::good() const { return !errors_.size(); }
1888+
bool OurReader::good() const { return errors_.empty(); }
18891889

18901890
class OurCharReader : public CharReader {
18911891
bool const collectComments_;
@@ -1951,7 +1951,7 @@ bool CharReaderBuilder::validate(Json::Value* invalid) const {
19511951
inv[key] = settings_[key];
19521952
}
19531953
}
1954-
return 0u == inv.size();
1954+
return inv.empty();
19551955
}
19561956
Value& CharReaderBuilder::operator[](const JSONCPP_STRING& key) {
19571957
return settings_[key];

Diff for: src/lib_json/json_value.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,8 @@ bool Value::isConvertibleTo(ValueType other) const {
889889
return (isNumeric() && asDouble() == 0.0) ||
890890
(type_ == booleanValue && value_.bool_ == false) ||
891891
(type_ == stringValue && asString().empty()) ||
892-
(type_ == arrayValue && value_.map_->size() == 0) ||
893-
(type_ == objectValue && value_.map_->size() == 0) ||
892+
(type_ == arrayValue && value_.map_->empty()) ||
893+
(type_ == objectValue && value_.map_->empty()) ||
894894
type_ == nullValue;
895895
case intValue:
896896
return isInt() ||

Diff for: src/lib_json/json_writer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ bool StyledWriter::isMultilineArray(const Value& value) {
551551
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
552552
const Value& childValue = value[index];
553553
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
554-
childValue.size() > 0);
554+
!childValue.empty());
555555
}
556556
if (!isMultiLine) // check if line length > max line length
557557
{
@@ -774,7 +774,7 @@ bool StyledStreamWriter::isMultilineArray(const Value& value) {
774774
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
775775
const Value& childValue = value[index];
776776
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
777-
childValue.size() > 0);
777+
!childValue.empty());
778778
}
779779
if (!isMultiLine) // check if line length > max line length
780780
{
@@ -1059,7 +1059,7 @@ bool BuiltStyledStreamWriter::isMultilineArray(Value const& value) {
10591059
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
10601060
Value const& childValue = value[index];
10611061
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
1062-
childValue.size() > 0);
1062+
!childValue.empty());
10631063
}
10641064
if (!isMultiLine) // check if line length > max line length
10651065
{
@@ -1226,7 +1226,7 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const {
12261226
inv[key] = settings_[key];
12271227
}
12281228
}
1229-
return 0u == inv.size();
1229+
return inv.empty();
12301230
}
12311231
Value& StreamWriterBuilder::operator[](const JSONCPP_STRING& key) {
12321232
return settings_[key];

Diff for: src/test_lib_json/main.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,7 @@ JSONTEST_FIXTURE(StreamWriterTest, dropNullPlaceholders) {
18161816
b.settings_["dropNullPlaceholders"] = false;
18171817
JSONTEST_ASSERT(Json::writeString(b, nullValue) == "null");
18181818
b.settings_["dropNullPlaceholders"] = true;
1819-
JSONTEST_ASSERT(Json::writeString(b, nullValue) == "");
1819+
JSONTEST_ASSERT(Json::writeString(b, nullValue).empty());
18201820
}
18211821

18221822
JSONTEST_FIXTURE(StreamWriterTest, writeZeroes) {
@@ -1848,8 +1848,8 @@ JSONTEST_FIXTURE(ReaderTest, parseWithNoErrors) {
18481848
Json::Value root;
18491849
bool ok = reader.parse("{ \"property\" : \"value\" }", root);
18501850
JSONTEST_ASSERT(ok);
1851-
JSONTEST_ASSERT(reader.getFormattedErrorMessages().size() == 0);
1852-
JSONTEST_ASSERT(reader.getStructuredErrors().size() == 0);
1851+
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
1852+
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
18531853
}
18541854

18551855
JSONTEST_FIXTURE(ReaderTest, parseWithNoErrorsTestingOffsets) {
@@ -1860,8 +1860,8 @@ JSONTEST_FIXTURE(ReaderTest, parseWithNoErrorsTestingOffsets) {
18601860
"null, \"false\" : false }",
18611861
root);
18621862
JSONTEST_ASSERT(ok);
1863-
JSONTEST_ASSERT(reader.getFormattedErrorMessages().size() == 0);
1864-
JSONTEST_ASSERT(reader.getStructuredErrors().size() == 0);
1863+
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
1864+
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
18651865
JSONTEST_ASSERT(root["property"].getOffsetStart() == 15);
18661866
JSONTEST_ASSERT(root["property"].getOffsetLimit() == 34);
18671867
JSONTEST_ASSERT(root["property"][0].getOffsetStart() == 16);
@@ -1942,7 +1942,7 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithNoErrors) {
19421942
char const doc[] = "{ \"property\" : \"value\" }";
19431943
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
19441944
JSONTEST_ASSERT(ok);
1945-
JSONTEST_ASSERT(errs.size() == 0);
1945+
JSONTEST_ASSERT(errs.empty());
19461946
delete reader;
19471947
}
19481948

@@ -1956,7 +1956,7 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithNoErrorsTestingOffsets) {
19561956
"null, \"false\" : false }";
19571957
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
19581958
JSONTEST_ASSERT(ok);
1959-
JSONTEST_ASSERT(errs.size() == 0);
1959+
JSONTEST_ASSERT(errs.empty());
19601960
delete reader;
19611961
}
19621962

@@ -2012,7 +2012,7 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithStackLimit) {
20122012
JSONCPP_STRING errs;
20132013
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
20142014
JSONTEST_ASSERT(ok);
2015-
JSONTEST_ASSERT(errs == "");
2015+
JSONTEST_ASSERT(errs.empty());
20162016
JSONTEST_ASSERT_EQUAL("value", root["property"]);
20172017
delete reader;
20182018
}
@@ -2059,7 +2059,7 @@ JSONTEST_FIXTURE(CharReaderFailIfExtraTest, issue164) {
20592059
JSONCPP_STRING errs;
20602060
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
20612061
JSONTEST_ASSERT(ok);
2062-
JSONTEST_ASSERT(errs == "");
2062+
JSONTEST_ASSERT(errs.empty());
20632063
JSONTEST_ASSERT_EQUAL("property", root);
20642064
delete reader;
20652065
}
@@ -2174,15 +2174,15 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
21742174
char const doc[] = "[]";
21752175
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
21762176
JSONTEST_ASSERT(ok);
2177-
JSONTEST_ASSERT(errs == "");
2177+
JSONTEST_ASSERT(errs.empty());
21782178
JSONTEST_ASSERT_EQUAL(0u, root.size());
21792179
JSONTEST_ASSERT_EQUAL(Json::arrayValue, root);
21802180
}
21812181
{
21822182
char const doc[] = "[null]";
21832183
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
21842184
JSONTEST_ASSERT(ok);
2185-
JSONTEST_ASSERT(errs == "");
2185+
JSONTEST_ASSERT(errs.empty());
21862186
JSONTEST_ASSERT_EQUAL(1u, root.size());
21872187
}
21882188
{
@@ -2210,7 +2210,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
22102210
char const doc[] = "[,null]";
22112211
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
22122212
JSONTEST_ASSERT(ok);
2213-
JSONTEST_ASSERT(errs == "");
2213+
JSONTEST_ASSERT(errs.empty());
22142214
JSONTEST_ASSERT_EQUAL(2u, root.size());
22152215
}
22162216
{
@@ -2238,7 +2238,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
22382238
char const doc[] = "[,,null]";
22392239
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
22402240
JSONTEST_ASSERT(ok);
2241-
JSONTEST_ASSERT(errs == "");
2241+
JSONTEST_ASSERT(errs.empty());
22422242
JSONTEST_ASSERT_EQUAL(3u, root.size());
22432243
}
22442244
{
@@ -2261,7 +2261,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
22612261
char const doc[] = "[,,,[]]";
22622262
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
22632263
JSONTEST_ASSERT(ok);
2264-
JSONTEST_ASSERT(errs == "");
2264+
JSONTEST_ASSERT(errs.empty());
22652265
JSONTEST_ASSERT_EQUAL(4u, root.size());
22662266
JSONTEST_ASSERT_EQUAL(Json::arrayValue, root[3u]);
22672267
}

0 commit comments

Comments
 (0)