From cbeed7b0769a1a519a2e827480b96cf0d17e791d Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Mon, 14 Jan 2019 17:09:12 -0600 Subject: [PATCH] STYLE: Use range-based loops from C++11 C++11 Range based for loops can be used in Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container, in the forward direction.. Range based loopes are more explicit for only computing the end location once for containers. 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=-*,modernize-loop-convert -header-filter=.* -fix --- src/jsontestrunner/main.cpp | 4 +--- src/lib_json/json_reader.cpp | 16 ++++------------ src/lib_json/json_value.cpp | 9 +++------ src/test_lib_json/jsontest.cpp | 7 ++----- src/test_lib_json/main.cpp | 3 +-- 5 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp index 28691a2..5449299 100644 --- a/src/jsontestrunner/main.cpp +++ b/src/jsontestrunner/main.cpp @@ -110,9 +110,7 @@ static void printValueTree(FILE* fout, Json::Value::Members members(value.getMemberNames()); std::sort(members.begin(), members.end()); JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : "."; - for (Json::Value::Members::iterator it = members.begin(); - it != members.end(); ++it) { - const JSONCPP_STRING name = *it; + for (auto name : members) { printValueTree(fout, value[name], path + suffix + name); } } break; diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index aa5156f..077da1b 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -818,9 +818,7 @@ JSONCPP_STRING Reader::getFormatedErrorMessages() const { JSONCPP_STRING Reader::getFormattedErrorMessages() const { JSONCPP_STRING formattedMessage; - for (Errors::const_iterator itError = errors_.begin(); - itError != errors_.end(); ++itError) { - const ErrorInfo& error = *itError; + for (const auto & error : errors_) { formattedMessage += "* " + getLocationLineAndColumn(error.token_.start_) + "\n"; formattedMessage += " " + error.message_ + "\n"; @@ -833,9 +831,7 @@ JSONCPP_STRING Reader::getFormattedErrorMessages() const { std::vector Reader::getStructuredErrors() const { std::vector allErrors; - for (Errors::const_iterator itError = errors_.begin(); - itError != errors_.end(); ++itError) { - const ErrorInfo& error = *itError; + for (const auto & error : errors_) { Reader::StructuredError structured; structured.offset_start = error.token_.start_ - begin_; structured.offset_limit = error.token_.end_ - begin_; @@ -1833,9 +1829,7 @@ JSONCPP_STRING OurReader::getLocationLineAndColumn(Location location) const { JSONCPP_STRING OurReader::getFormattedErrorMessages() const { JSONCPP_STRING formattedMessage; - for (Errors::const_iterator itError = errors_.begin(); - itError != errors_.end(); ++itError) { - const ErrorInfo& error = *itError; + for (const auto & error : errors_) { formattedMessage += "* " + getLocationLineAndColumn(error.token_.start_) + "\n"; formattedMessage += " " + error.message_ + "\n"; @@ -1848,9 +1842,7 @@ JSONCPP_STRING OurReader::getFormattedErrorMessages() const { std::vector OurReader::getStructuredErrors() const { std::vector allErrors; - for (Errors::const_iterator itError = errors_.begin(); - itError != errors_.end(); ++itError) { - const ErrorInfo& error = *itError; + for (const auto & error : errors_) { OurReader::StructuredError structured; structured.offset_start = error.token_.start_ - begin_; structured.offset_limit = error.token_.end_ - begin_; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index c7fcfa4..5e83239 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1655,8 +1655,7 @@ void Path::invalidPath(const JSONCPP_STRING& /*path*/, int /*location*/) { const Value& Path::resolve(const Value& root) const { const Value* node = &root; - for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) { - const PathArgument& arg = *it; + for (const auto & arg : args_) { if (arg.kind_ == PathArgument::kindIndex) { if (!node->isArray() || !node->isValidIndex(arg.index_)) { // Error: unable to resolve path (array value expected at position... @@ -1681,8 +1680,7 @@ const Value& Path::resolve(const Value& root) const { Value Path::resolve(const Value& root, const Value& defaultValue) const { const Value* node = &root; - for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) { - const PathArgument& arg = *it; + for (const auto & arg : args_) { if (arg.kind_ == PathArgument::kindIndex) { if (!node->isArray() || !node->isValidIndex(arg.index_)) return defaultValue; @@ -1700,8 +1698,7 @@ Value Path::resolve(const Value& root, const Value& defaultValue) const { Value& Path::make(Value& root) const { Value* node = &root; - for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) { - const PathArgument& arg = *it; + for (const auto & arg : args_) { if (arg.kind_ == PathArgument::kindIndex) { if (!node->isArray()) { // Error: node is not an array at position ... diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp index 5e04aa4..f661505 100644 --- a/src/test_lib_json/jsontest.cpp +++ b/src/test_lib_json/jsontest.cpp @@ -150,9 +150,7 @@ void TestResult::printFailure(bool printTestName) const { } // Print in reverse to display the callstack in the right order - Failures::const_iterator itEnd = failures_.end(); - for (Failures::const_iterator it = failures_.begin(); it != itEnd; ++it) { - const Failure& failure = *it; + for (const auto & failure : failures_ ) { JSONCPP_STRING indent(failure.nestingLevel_ * 2, ' '); if (failure.file_) { printf("%s%s(%u): ", indent.c_str(), failure.file_, failure.line_); @@ -275,8 +273,7 @@ bool Runner::runAllTest(bool printSummary) const { } return true; } else { - for (unsigned int index = 0; index < failures.size(); ++index) { - TestResult& result = failures[index]; + for (auto & result : failures) { result.printFailure(count > 1); } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 4dfca75..8b21547 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2365,8 +2365,7 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) { { __LINE__, false, "{\"a\":.Infinity}" }, { __LINE__, false, "{\"a\":_Infinity}" }, { __LINE__, false, "{\"a\":_nfinity}" }, { __LINE__, true, "{\"a\":-Infinity}" } }; - for (size_t tdi = 0; tdi < sizeof(test_data) / sizeof(*test_data); ++tdi) { - const TestData& td = test_data[tdi]; + for (const auto& td : test_data) { bool ok = reader->parse(&*td.in.begin(), &*td.in.begin() + td.in.size(), &root, &errs); JSONTEST_ASSERT(td.ok == ok) << "line:" << td.line << "\n"