Skip to content

Commit a44bf41

Browse files
committed
Modernize C++ loops
The modifications were done using this command: run-clang-tidy-8.py -header-filter='.*' -checks='-*,modernize-loop-convert' -fix Then the resulting code was cleaned manually. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent ed01167 commit a44bf41

File tree

7 files changed

+25
-34
lines changed

7 files changed

+25
-34
lines changed

src/ccmain/osdetect.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -416,20 +416,20 @@ bool OrientationDetector::detect_blob(BLOB_CHOICE_LIST* scores) {
416416
// picking an arbitrary probability for it and way better than -inf.
417417
float worst_score = 0.0f;
418418
int num_good_scores = 0;
419-
for (int i = 0; i < 4; ++i) {
420-
if (blob_o_score[i] > 0.0f) {
419+
for (float f : blob_o_score) {
420+
if (f > 0.0f) {
421421
++num_good_scores;
422-
if (worst_score == 0.0f || blob_o_score[i] < worst_score)
423-
worst_score = blob_o_score[i];
422+
if (worst_score == 0.0f || f < worst_score)
423+
worst_score = f;
424424
}
425425
}
426426
if (num_good_scores == 1) {
427427
// Lower worst if there is only one.
428428
worst_score /= 2.0f;
429429
}
430-
for (int i = 0; i < 4; ++i) {
431-
if (blob_o_score[i] == 0.0f) {
432-
blob_o_score[i] = worst_score;
430+
for (float& f : blob_o_score) {
431+
if (f == 0.0f) {
432+
f = worst_score;
433433
total_blob_o_score += worst_score;
434434
}
435435
}
@@ -469,10 +469,8 @@ ScriptDetector::ScriptDetector(const GenericVector<int>* allowed_scripts,
469469
// Score the given blob and return true if it is now sure of the script after
470470
// adding this blob.
471471
void ScriptDetector::detect_blob(BLOB_CHOICE_LIST* scores) {
472-
bool done[kMaxNumberOfScripts];
473472
for (int i = 0; i < 4; ++i) {
474-
for (int j = 0; j < kMaxNumberOfScripts; ++j)
475-
done[j] = false;
473+
bool done[kMaxNumberOfScripts] = { false };
476474

477475
BLOB_CHOICE_IT choice_it;
478476
choice_it.set_to_list(scores + i);

src/ccmain/paramsd.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,8 @@ void ParamsEditor::WriteParams(char *filename,
340340
filename);
341341
return;
342342
}
343-
344-
for (auto iter = vcMap.begin();
345-
iter != vcMap.end();
346-
++iter) {
347-
ParamContent* cur = iter->second;
343+
for (auto& iter : vcMap) {
344+
ParamContent* cur = iter.second;
348345
if (!changes_only || cur->HasChanged()) {
349346
fprintf(fp, "%-25s %-12s # %s\n",
350347
cur->GetName(), cur->GetValue().string(), cur->GetDescription());

src/ccstruct/ratngs.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,7 @@ void WERD_CHOICE::SetScriptPositions(bool small_caps, TWERD* word, int debug) {
555555
return;
556556
}
557557

558-
int position_counts[4];
559-
for (int i = 0; i < 4; i++) {
560-
position_counts[i] = 0;
561-
}
558+
int position_counts[4] = { 0, 0, 0, 0 };
562559

563560
int chunk_index = 0;
564561
for (int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {

src/ccutil/unicharcompress.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,13 @@ bool UnicharCompress::ComputeEncoding(const UNICHARSET& unicharset, int null_id,
168168
} else {
169169
// Add the direct_set unichar-ids of the unicodes in sequence to the
170170
// code.
171-
for (size_t i = 0; i < unicodes.size(); ++i) {
171+
for (int uni : unicodes) {
172172
int position = code.length();
173173
if (position >= RecodedCharID::kMaxCodeLen) {
174174
tprintf("Unichar %d=%s is too long to encode!!\n", u,
175175
unicharset.id_to_unichar(u));
176176
return false;
177177
}
178-
int uni = unicodes[i];
179178
UNICHAR unichar(uni);
180179
char* utf8 = unichar.utf8_str();
181180
if (!direct_set.contains_unichar(utf8))
@@ -412,11 +411,11 @@ void UnicharCompress::SetupDecoder() {
412411
void UnicharCompress::Cleanup() {
413412
decoder_.clear();
414413
is_valid_start_.clear();
415-
for (auto it = next_codes_.begin(); it != next_codes_.end(); ++it) {
416-
delete it->second;
414+
for (auto& next_code : next_codes_) {
415+
delete next_code.second;
417416
}
418-
for (auto it = final_codes_.begin(); it != final_codes_.end(); ++it) {
419-
delete it->second;
417+
for (auto& final_code : final_codes_) {
418+
delete final_code.second;
420419
}
421420
next_codes_.clear();
422421
final_codes_.clear();

src/classify/adaptmatch.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ void Classify::InitAdaptiveClassifier(TessdataManager* mgr) {
565565
set_all_bits(AllConfigsOn, WordsInVectorOfSize(MAX_NUM_CONFIGS));
566566
zero_all_bits(AllConfigsOff, WordsInVectorOfSize(MAX_NUM_CONFIGS));
567567

568-
for (int i = 0; i < MAX_NUM_CLASSES; i++) {
569-
BaselineCutoffs[i] = 0;
568+
for (uint16_t& BaselineCutoff : BaselineCutoffs) {
569+
BaselineCutoff = 0;
570570
}
571571

572572
if (classify_use_pre_adapted_templates) {

src/classify/cluster.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ MakeClusterer (int16_t SampleSize, const PARAM_DESC ParamDesc[]) {
405405
Clusterer->KDTree = MakeKDTree (SampleSize, ParamDesc);
406406

407407
// Initialize cache of histogram buckets to minimize recomputing them.
408-
for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
408+
for (auto & d : Clusterer->bucket_cache) {
409409
for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
410-
Clusterer->bucket_cache[d][c] = nullptr;
410+
d[c] = nullptr;
411411
}
412412

413413
return Clusterer;
@@ -520,10 +520,10 @@ void FreeClusterer(CLUSTERER *Clusterer) {
520520
if (Clusterer->Root != nullptr)
521521
FreeCluster (Clusterer->Root);
522522
// Free up all used buckets structures.
523-
for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
523+
for (auto & d : Clusterer->bucket_cache) {
524524
for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
525-
if (Clusterer->bucket_cache[d][c] != nullptr)
526-
FreeBuckets(Clusterer->bucket_cache[d][c]);
525+
if (d[c] != nullptr)
526+
FreeBuckets(d[c]);
527527
}
528528

529529
free(Clusterer);

src/classify/errorcounter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,8 @@ bool ErrorCounter::ReportString(bool even_if_empty, const Counts& counts,
465465
delete [] formatted_str;
466466
// Now append each field of counts with a tab in front so the result can
467467
// be loaded into a spreadsheet.
468-
for (int ct = 0; ct < CT_SIZE; ++ct)
469-
report->add_str_int("\t", counts.n[ct]);
468+
for (int ct : counts.n)
469+
report->add_str_int("\t", ct);
470470
return true;
471471
}
472472

0 commit comments

Comments
 (0)