Skip to content

Commit cbeed7b

Browse files
committed
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
1 parent 3beadff commit cbeed7b

File tree

5 files changed

+11
-28
lines changed

5 files changed

+11
-28
lines changed

src/jsontestrunner/main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ static void printValueTree(FILE* fout,
110110
Json::Value::Members members(value.getMemberNames());
111111
std::sort(members.begin(), members.end());
112112
JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : ".";
113-
for (Json::Value::Members::iterator it = members.begin();
114-
it != members.end(); ++it) {
115-
const JSONCPP_STRING name = *it;
113+
for (auto name : members) {
116114
printValueTree(fout, value[name], path + suffix + name);
117115
}
118116
} break;

src/lib_json/json_reader.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,7 @@ JSONCPP_STRING Reader::getFormatedErrorMessages() const {
818818

819819
JSONCPP_STRING Reader::getFormattedErrorMessages() const {
820820
JSONCPP_STRING formattedMessage;
821-
for (Errors::const_iterator itError = errors_.begin();
822-
itError != errors_.end(); ++itError) {
823-
const ErrorInfo& error = *itError;
821+
for (const auto & error : errors_) {
824822
formattedMessage +=
825823
"* " + getLocationLineAndColumn(error.token_.start_) + "\n";
826824
formattedMessage += " " + error.message_ + "\n";
@@ -833,9 +831,7 @@ JSONCPP_STRING Reader::getFormattedErrorMessages() const {
833831

834832
std::vector<Reader::StructuredError> Reader::getStructuredErrors() const {
835833
std::vector<Reader::StructuredError> allErrors;
836-
for (Errors::const_iterator itError = errors_.begin();
837-
itError != errors_.end(); ++itError) {
838-
const ErrorInfo& error = *itError;
834+
for (const auto & error : errors_) {
839835
Reader::StructuredError structured;
840836
structured.offset_start = error.token_.start_ - begin_;
841837
structured.offset_limit = error.token_.end_ - begin_;
@@ -1833,9 +1829,7 @@ JSONCPP_STRING OurReader::getLocationLineAndColumn(Location location) const {
18331829

18341830
JSONCPP_STRING OurReader::getFormattedErrorMessages() const {
18351831
JSONCPP_STRING formattedMessage;
1836-
for (Errors::const_iterator itError = errors_.begin();
1837-
itError != errors_.end(); ++itError) {
1838-
const ErrorInfo& error = *itError;
1832+
for (const auto & error : errors_) {
18391833
formattedMessage +=
18401834
"* " + getLocationLineAndColumn(error.token_.start_) + "\n";
18411835
formattedMessage += " " + error.message_ + "\n";
@@ -1848,9 +1842,7 @@ JSONCPP_STRING OurReader::getFormattedErrorMessages() const {
18481842

18491843
std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
18501844
std::vector<OurReader::StructuredError> allErrors;
1851-
for (Errors::const_iterator itError = errors_.begin();
1852-
itError != errors_.end(); ++itError) {
1853-
const ErrorInfo& error = *itError;
1845+
for (const auto & error : errors_) {
18541846
OurReader::StructuredError structured;
18551847
structured.offset_start = error.token_.start_ - begin_;
18561848
structured.offset_limit = error.token_.end_ - begin_;

src/lib_json/json_value.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,8 +1655,7 @@ void Path::invalidPath(const JSONCPP_STRING& /*path*/, int /*location*/) {
16551655

16561656
const Value& Path::resolve(const Value& root) const {
16571657
const Value* node = &root;
1658-
for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1659-
const PathArgument& arg = *it;
1658+
for (const auto & arg : args_) {
16601659
if (arg.kind_ == PathArgument::kindIndex) {
16611660
if (!node->isArray() || !node->isValidIndex(arg.index_)) {
16621661
// Error: unable to resolve path (array value expected at position...
@@ -1681,8 +1680,7 @@ const Value& Path::resolve(const Value& root) const {
16811680

16821681
Value Path::resolve(const Value& root, const Value& defaultValue) const {
16831682
const Value* node = &root;
1684-
for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1685-
const PathArgument& arg = *it;
1683+
for (const auto & arg : args_) {
16861684
if (arg.kind_ == PathArgument::kindIndex) {
16871685
if (!node->isArray() || !node->isValidIndex(arg.index_))
16881686
return defaultValue;
@@ -1700,8 +1698,7 @@ Value Path::resolve(const Value& root, const Value& defaultValue) const {
17001698

17011699
Value& Path::make(Value& root) const {
17021700
Value* node = &root;
1703-
for (Args::const_iterator it = args_.begin(); it != args_.end(); ++it) {
1704-
const PathArgument& arg = *it;
1701+
for (const auto & arg : args_) {
17051702
if (arg.kind_ == PathArgument::kindIndex) {
17061703
if (!node->isArray()) {
17071704
// Error: node is not an array at position ...

src/test_lib_json/jsontest.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ void TestResult::printFailure(bool printTestName) const {
150150
}
151151

152152
// Print in reverse to display the callstack in the right order
153-
Failures::const_iterator itEnd = failures_.end();
154-
for (Failures::const_iterator it = failures_.begin(); it != itEnd; ++it) {
155-
const Failure& failure = *it;
153+
for (const auto & failure : failures_ ) {
156154
JSONCPP_STRING indent(failure.nestingLevel_ * 2, ' ');
157155
if (failure.file_) {
158156
printf("%s%s(%u): ", indent.c_str(), failure.file_, failure.line_);
@@ -275,8 +273,7 @@ bool Runner::runAllTest(bool printSummary) const {
275273
}
276274
return true;
277275
} else {
278-
for (unsigned int index = 0; index < failures.size(); ++index) {
279-
TestResult& result = failures[index];
276+
for (auto & result : failures) {
280277
result.printFailure(count > 1);
281278
}
282279

src/test_lib_json/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,8 +2365,7 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) {
23652365
{ __LINE__, false, "{\"a\":.Infinity}" }, { __LINE__, false, "{\"a\":_Infinity}" },
23662366
{ __LINE__, false, "{\"a\":_nfinity}" }, { __LINE__, true, "{\"a\":-Infinity}" }
23672367
};
2368-
for (size_t tdi = 0; tdi < sizeof(test_data) / sizeof(*test_data); ++tdi) {
2369-
const TestData& td = test_data[tdi];
2368+
for (const auto& td : test_data) {
23702369
bool ok = reader->parse(&*td.in.begin(), &*td.in.begin() + td.in.size(),
23712370
&root, &errs);
23722371
JSONTEST_ASSERT(td.ok == ok) << "line:" << td.line << "\n"

0 commit comments

Comments
 (0)