From 77ed2886a73e75dc8319dee4e51f1226a8db7e8b Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 22 Mar 2021 08:32:09 +0100 Subject: [PATCH] Modernize code (clang-tidy -checks='-*,modernize-loop-convert') Signed-off-by: Stefan Weil --- src/classify/shapeclassifier.cpp | 4 ++-- src/lstm/recodebeam.cpp | 4 ++-- unittest/baseapi_test.cc | 14 +++++++------- unittest/equationdetect_test.cc | 6 +++--- unittest/fileio_test.cc | 4 ++-- unittest/heap_test.cc | 10 +++++----- unittest/normstrngs_test.cc | 12 ++++++------ unittest/pango_font_info_test.cc | 6 +++--- unittest/recodebeam_test.cc | 8 ++++---- unittest/resultiterator_test.cc | 4 ++-- unittest/stringrenderer_test.cc | 4 ++-- unittest/tablefind_test.cc | 4 ++-- unittest/tfile_test.cc | 4 ++-- unittest/unicharcompress_test.cc | 6 ++---- 14 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/classify/shapeclassifier.cpp b/src/classify/shapeclassifier.cpp index 6754d192..b180013c 100644 --- a/src/classify/shapeclassifier.cpp +++ b/src/classify/shapeclassifier.cpp @@ -175,8 +175,8 @@ void ShapeClassifier::UnicharPrintResults(const char *context, GetUnicharset().id_to_unichar(result.unichar_id)); if (!result.fonts.empty()) { tprintf(" Font Vector:"); - for (int f = 0; f < result.fonts.size(); ++f) { - tprintf(" %d", result.fonts[f].fontinfo_id); + for (auto font : result.fonts) { + tprintf(" %d", font.fontinfo_id); } } tprintf("\n"); diff --git a/src/lstm/recodebeam.cpp b/src/lstm/recodebeam.cpp index 267eba86..485723ea 100644 --- a/src/lstm/recodebeam.cpp +++ b/src/lstm/recodebeam.cpp @@ -173,8 +173,8 @@ RecodeBeamSearch::combineSegmentedTimesteps( std::vector>>> *segmentedTimesteps) { std::vector>> combined_timesteps; for (auto &segmentedTimestep : *segmentedTimesteps) { - for (int j = 0; j < segmentedTimestep.size(); ++j) { - combined_timesteps.push_back(segmentedTimestep[j]); + for (auto &j : segmentedTimestep) { + combined_timesteps.push_back(j); } } return combined_timesteps; diff --git a/unittest/baseapi_test.cc b/unittest/baseapi_test.cc index af2c513c..aabf97c8 100644 --- a/unittest/baseapi_test.cc +++ b/unittest/baseapi_test.cc @@ -278,25 +278,25 @@ TEST_F(TesseractTest, InitConfigOnlyTest) { const char *langs[] = {"eng", "chi_tra", "jpn", "vie"}; std::unique_ptr api; CycleTimer timer; - for (size_t i = 0; i < countof(langs); ++i) { + for (auto &lang : langs) { api = std::make_unique(); timer.Restart(); - EXPECT_EQ(0, api->Init(TessdataPath().c_str(), langs[i], tesseract::OEM_TESSERACT_ONLY)); + EXPECT_EQ(0, api->Init(TessdataPath().c_str(), lang, tesseract::OEM_TESSERACT_ONLY)); timer.Stop(); - LOG(INFO) << "Lang " << langs[i] << " took " << timer.GetInMs() << "ms in regular init"; + LOG(INFO) << "Lang " << lang << " took " << timer.GetInMs() << "ms in regular init"; } // Init variables to set for config-only initialization. std::vector vars_vec, vars_values; vars_vec.push_back("tessedit_init_config_only"); vars_values.push_back("1"); LOG(INFO) << "Switching to config only initialization:"; - for (size_t i = 0; i < countof(langs); ++i) { + for (auto &lang : langs) { api = std::make_unique(); timer.Restart(); - EXPECT_EQ(0, api->Init(TessdataPath().c_str(), langs[i], tesseract::OEM_TESSERACT_ONLY, nullptr, - 0, &vars_vec, &vars_values, false)); + EXPECT_EQ(0, api->Init(TessdataPath().c_str(), lang, tesseract::OEM_TESSERACT_ONLY, nullptr, 0, + &vars_vec, &vars_values, false)); timer.Stop(); - LOG(INFO) << "Lang " << langs[i] << " took " << timer.GetInMs() << "ms in config-only init"; + LOG(INFO) << "Lang " << lang << " took " << timer.GetInMs() << "ms in config-only init"; } } diff --git a/unittest/equationdetect_test.cc b/unittest/equationdetect_test.cc index 15009d32..fa322924 100644 --- a/unittest/equationdetect_test.cc +++ b/unittest/equationdetect_test.cc @@ -162,9 +162,9 @@ protected: } void ClearParts(std::vector *all_parts) { - for (size_t i = 0; i < all_parts->size(); ++i) { - (*all_parts)[i]->DeleteBoxes(); - delete ((*all_parts)[i]); + for (auto &all_part : *all_parts) { + all_part->DeleteBoxes(); + delete all_part; } } diff --git a/unittest/fileio_test.cc b/unittest/fileio_test.cc index 44746715..033d4781 100644 --- a/unittest/fileio_test.cc +++ b/unittest/fileio_test.cc @@ -28,8 +28,8 @@ TEST(FileTest, JoinPath) { TEST(OutputBufferTest, WriteString) { const int kMaxBufSize = 128; char buffer[kMaxBufSize]; - for (int i = 0; i < kMaxBufSize; ++i) - buffer[i] = '\0'; + for (char &i : buffer) + i = '\0'; FILE *fp = tmpfile(); CHECK(fp != nullptr); diff --git a/unittest/heap_test.cc b/unittest/heap_test.cc index a1ef5613..324122f6 100644 --- a/unittest/heap_test.cc +++ b/unittest/heap_test.cc @@ -136,11 +136,11 @@ TEST_F(HeapTest, RevalueTest) { GenericHeap heap; std::vector v; // Push the test data onto both the heap and the vector. - for (size_t i = 0; i < countof(test_data); ++i) { + for (int i : test_data) { PtrPair h_pair; - h_pair.key() = test_data[i]; + h_pair.key() = i; PtrPair v_pair; - v_pair.key() = test_data[i]; + v_pair.key() = i; h_pair.data().Connect(&v_pair.data()); heap.Push(&h_pair); v.push_back(v_pair); @@ -163,8 +163,8 @@ TEST_F(HeapTest, RevalueTest) { // of the vector. std::sort(v.begin(), v.end()); EXPECT_GT(v[0].key(), v.back().key()); - for (int i = 0; i < v.size(); ++i) { - EXPECT_EQ(v[i].key(), heap.PeekTop().key()); + for (auto &i : v) { + EXPECT_EQ(i.key(), heap.PeekTop().key()); EXPECT_FALSE(heap.empty()); heap.Pop(nullptr); } diff --git a/unittest/normstrngs_test.cc b/unittest/normstrngs_test.cc index 714f5de8..bb2ca5aa 100644 --- a/unittest/normstrngs_test.cc +++ b/unittest/normstrngs_test.cc @@ -104,15 +104,15 @@ TEST(NormstrngsTest, DetectsCorrectText) { } TEST(NormstrngsTest, DetectsIncorrectText) { - for (size_t i = 0; i < countof(kBadlyFormedHinWords); ++i) { + for (auto &kBadlyFormedHinWord : kBadlyFormedHinWords) { EXPECT_FALSE(NormalizeUTF8String(UnicodeNormMode::kNFKC, OCRNorm::kNone, - GraphemeNorm::kNormalize, kBadlyFormedHinWords[i], nullptr)) - << kBadlyFormedHinWords[i]; + GraphemeNorm::kNormalize, kBadlyFormedHinWord, nullptr)) + << kBadlyFormedHinWord; } - for (size_t i = 0; i < countof(kBadlyFormedThaiWords); ++i) { + for (auto &kBadlyFormedThaiWord : kBadlyFormedThaiWords) { EXPECT_FALSE(NormalizeUTF8String(UnicodeNormMode::kNFKC, OCRNorm::kNone, - GraphemeNorm::kNormalize, kBadlyFormedThaiWords[i], nullptr)) - << kBadlyFormedThaiWords[i]; + GraphemeNorm::kNormalize, kBadlyFormedThaiWord, nullptr)) + << kBadlyFormedThaiWord; } } diff --git a/unittest/pango_font_info_test.cc b/unittest/pango_font_info_test.cc index d15cb180..b3862e50 100644 --- a/unittest/pango_font_info_test.cc +++ b/unittest/pango_font_info_test.cc @@ -165,10 +165,10 @@ TEST_F(PangoFontInfoTest, CanDropUncoveredChars) { "\u200C", // U+200C (ZWJ) "\u200D" // U+200D (ZWNJ) }; - for (size_t i = 0; i < countof(kJoiners); ++i) { - word = kJoiners[i]; + for (auto &kJoiner : kJoiners) { + word = kJoiner; EXPECT_EQ(0, font_info_.DropUncoveredChars(&word)); - EXPECT_STREQ(kJoiners[i], word.c_str()); + EXPECT_STREQ(kJoiner, word.c_str()); } } diff --git a/unittest/recodebeam_test.cc b/unittest/recodebeam_test.cc index 2e41f01a..f56bff71 100644 --- a/unittest/recodebeam_test.cc +++ b/unittest/recodebeam_test.cc @@ -108,8 +108,8 @@ protected: const std::vector &transcription) { // Get the utf8 string of the transcription. std::string truth_utf8; - for (int i = 0; i < transcription.size(); ++i) { - truth_utf8 += ccutil_.unicharset.id_to_unichar(transcription[i]); + for (int i : transcription) { + truth_utf8 += ccutil_.unicharset.id_to_unichar(i); } PointerVector words; ExpectCorrect(output, truth_utf8, nullptr, &words); @@ -211,9 +211,9 @@ protected: outputs(t, i) = random.UnsignedRand(0.25); } int t = 0; - for (int i = 0; i < unichar_ids.size(); ++i) { + for (int unichar_id : unichar_ids) { RecodedCharID code; - int len = recoder_.EncodeUnichar(unichar_ids[i], &code); + int len = recoder_.EncodeUnichar(unichar_id, &code); EXPECT_NE(0, len); for (int j = 0; j < len; ++j) { // Make the desired answer a clear winner. diff --git a/unittest/resultiterator_test.cc b/unittest/resultiterator_test.cc index fcdea19c..a676fe14 100644 --- a/unittest/resultiterator_test.cc +++ b/unittest/resultiterator_test.cc @@ -505,8 +505,8 @@ TEST_F(ResultIteratorTest, TextlineOrderSanityCheck) { for (int i = 0; i < kNumCombos; i++) { // generate the next combination. int tmp = i; - for (int j = 0; j < kNumWords; j++) { - word_dirs[j] = static_cast(tmp % 4); + for (auto &word_dir : word_dirs) { + word_dir = static_cast(tmp % 4); tmp = tmp / 4; } VerifySaneTextlineOrder(true, word_dirs, kNumWords); diff --git a/unittest/stringrenderer_test.cc b/unittest/stringrenderer_test.cc index 28c838c0..01cb50f8 100644 --- a/unittest/stringrenderer_test.cc +++ b/unittest/stringrenderer_test.cc @@ -213,8 +213,8 @@ TEST_F(StringRendererTest, ArabicBoxcharsInLTROrder) { std::vector texts; EXPECT_TRUE(ReadMemBoxes(0, false, boxes_str.c_str(), false, nullptr, &texts, nullptr, nullptr)); std::string ltr_str; - for (size_t i = 0; i < texts.size(); ++i) { - ltr_str += texts[i].c_str(); + for (auto &text : texts) { + ltr_str += text.c_str(); } // The string should come out perfectly reversed, despite there being a // ligature. diff --git a/unittest/tablefind_test.cc b/unittest/tablefind_test.cc index 0d966900..1c55f87b 100644 --- a/unittest/tablefind_test.cc +++ b/unittest/tablefind_test.cc @@ -130,8 +130,8 @@ private: TEST_F(TableFinderTest, GapInXProjectionNoGap) { int data[100]; - for (int i = 0; i < 100; ++i) - data[i] = 10; + for (int &i : data) + i = 10; EXPECT_FALSE(finder_->GapInXProjection(data, 100)); } diff --git a/unittest/tfile_test.cc b/unittest/tfile_test.cc index aeb9c782..f1faf53c 100644 --- a/unittest/tfile_test.cc +++ b/unittest/tfile_test.cc @@ -78,8 +78,8 @@ protected: // used as its size on reading. if (fp->FWrite(&num_squares_, sizeof(num_squares_), 1) != 1) return false; - for (int i = 0; i < squares_.size(); ++i) - ReverseN(&squares_[i], sizeof(squares_[i])); + for (int &square : squares_) + ReverseN(&square, sizeof(square)); if (!fp->Serialize(squares_)) return false; ReverseN(&num_triangles_, sizeof(num_triangles_)); diff --git a/unittest/unicharcompress_test.cc b/unittest/unicharcompress_test.cc index f6993574..9824fa77 100644 --- a/unittest/unicharcompress_test.cc +++ b/unittest/unicharcompress_test.cc @@ -139,8 +139,7 @@ protected: int length = code.length(); const std::vector *final_codes = compressed_.GetFinalCodes(code); if (final_codes != nullptr) { - for (int i = 0; i < final_codes->size(); ++i) { - int ending = (*final_codes)[i]; + for (int ending : *final_codes) { EXPECT_GT(times_seen[ending](length), 0); extended.Set(length, ending); int unichar_id = compressed_.DecodeUnichar(extended); @@ -149,8 +148,7 @@ protected: } const std::vector *next_codes = compressed_.GetNextCodes(code); if (next_codes != nullptr) { - for (int i = 0; i < next_codes->size(); ++i) { - int extension = (*next_codes)[i]; + for (int extension : *next_codes) { EXPECT_GT(times_seen[extension](length), 0); extended.Set(length, extension); CheckCodeExtensions(extended, times_seen);