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
This commit is contained in:
Hans Johnson 2019-01-14 17:08:51 -06:00 committed by Hans Johnson
parent d3d2e17b64
commit 3beadff472
4 changed files with 23 additions and 23 deletions

View File

@ -880,7 +880,7 @@ bool Reader::pushError(const Value& value,
return true;
}
bool Reader::good() const { return !errors_.size(); }
bool Reader::good() const { return errors_.empty(); }
// exact copy of Features
class OurFeatures {
@ -1895,7 +1895,7 @@ bool OurReader::pushError(const Value& value,
return true;
}
bool OurReader::good() const { return !errors_.size(); }
bool OurReader::good() const { return errors_.empty(); }
class OurCharReader : public CharReader {
bool const collectComments_;
@ -1961,7 +1961,7 @@ bool CharReaderBuilder::validate(Json::Value* invalid) const {
inv[key] = settings_[key];
}
}
return 0u == inv.size();
return inv.empty();
}
Value& CharReaderBuilder::operator[](const JSONCPP_STRING& key) {
return settings_[key];

View File

@ -889,8 +889,8 @@ bool Value::isConvertibleTo(ValueType other) const {
return (isNumeric() && asDouble() == 0.0) ||
(type_ == booleanValue && value_.bool_ == false) ||
(type_ == stringValue && asString().empty()) ||
(type_ == arrayValue && value_.map_->size() == 0) ||
(type_ == objectValue && value_.map_->size() == 0) ||
(type_ == arrayValue && value_.map_->empty()) ||
(type_ == objectValue && value_.map_->empty()) ||
type_ == nullValue;
case intValue:
return isInt() ||

View File

@ -551,7 +551,7 @@ bool StyledWriter::isMultilineArray(const Value& value) {
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
const Value& childValue = value[index];
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
childValue.size() > 0);
!childValue.empty());
}
if (!isMultiLine) // check if line length > max line length
{
@ -774,7 +774,7 @@ bool StyledStreamWriter::isMultilineArray(const Value& value) {
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
const Value& childValue = value[index];
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
childValue.size() > 0);
!childValue.empty());
}
if (!isMultiLine) // check if line length > max line length
{
@ -1059,7 +1059,7 @@ bool BuiltStyledStreamWriter::isMultilineArray(Value const& value) {
for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
Value const& childValue = value[index];
isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
childValue.size() > 0);
!childValue.empty());
}
if (!isMultiLine) // check if line length > max line length
{
@ -1226,7 +1226,7 @@ bool StreamWriterBuilder::validate(Json::Value* invalid) const {
inv[key] = settings_[key];
}
}
return 0u == inv.size();
return inv.empty();
}
Value& StreamWriterBuilder::operator[](const JSONCPP_STRING& key) {
return settings_[key];

View File

@ -1818,7 +1818,7 @@ JSONTEST_FIXTURE(StreamWriterTest, dropNullPlaceholders) {
b.settings_["dropNullPlaceholders"] = false;
JSONTEST_ASSERT(Json::writeString(b, nullValue) == "null");
b.settings_["dropNullPlaceholders"] = true;
JSONTEST_ASSERT(Json::writeString(b, nullValue) == "");
JSONTEST_ASSERT(Json::writeString(b, nullValue).empty());
}
JSONTEST_FIXTURE(StreamWriterTest, writeZeroes) {
@ -1850,8 +1850,8 @@ JSONTEST_FIXTURE(ReaderTest, parseWithNoErrors) {
Json::Value root;
bool ok = reader.parse("{ \"property\" : \"value\" }", root);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(reader.getFormattedErrorMessages().size() == 0);
JSONTEST_ASSERT(reader.getStructuredErrors().size() == 0);
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
}
JSONTEST_FIXTURE(ReaderTest, parseWithNoErrorsTestingOffsets) {
@ -1862,8 +1862,8 @@ JSONTEST_FIXTURE(ReaderTest, parseWithNoErrorsTestingOffsets) {
"null, \"false\" : false }",
root);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(reader.getFormattedErrorMessages().size() == 0);
JSONTEST_ASSERT(reader.getStructuredErrors().size() == 0);
JSONTEST_ASSERT(reader.getFormattedErrorMessages().empty());
JSONTEST_ASSERT(reader.getStructuredErrors().empty());
JSONTEST_ASSERT(root["property"].getOffsetStart() == 15);
JSONTEST_ASSERT(root["property"].getOffsetLimit() == 34);
JSONTEST_ASSERT(root["property"][0].getOffsetStart() == 16);
@ -1944,7 +1944,7 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithNoErrors) {
char const doc[] = "{ \"property\" : \"value\" }";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs.size() == 0);
JSONTEST_ASSERT(errs.empty());
delete reader;
}
@ -1958,7 +1958,7 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithNoErrorsTestingOffsets) {
"null, \"false\" : false }";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs.size() == 0);
JSONTEST_ASSERT(errs.empty());
delete reader;
}
@ -2014,7 +2014,7 @@ JSONTEST_FIXTURE(CharReaderTest, parseWithStackLimit) {
JSONCPP_STRING errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL("value", root["property"]);
delete reader;
}
@ -2061,7 +2061,7 @@ JSONTEST_FIXTURE(CharReaderFailIfExtraTest, issue164) {
JSONCPP_STRING errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL("property", root);
delete reader;
}
@ -2176,7 +2176,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
char const doc[] = "[]";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL(0u, root.size());
JSONTEST_ASSERT_EQUAL(Json::arrayValue, root);
}
@ -2184,7 +2184,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
char const doc[] = "[null]";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL(1u, root.size());
}
{
@ -2212,7 +2212,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
char const doc[] = "[,null]";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL(2u, root.size());
}
{
@ -2240,7 +2240,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
char const doc[] = "[,,null]";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL(3u, root.size());
}
{
@ -2263,7 +2263,7 @@ JSONTEST_FIXTURE(CharReaderAllowDropNullTest, issue178) {
char const doc[] = "[,,,[]]";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs == "");
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_EQUAL(4u, root.size());
JSONTEST_ASSERT_EQUAL(Json::arrayValue, root[3u]);
}