From 03b10e5afe0b2df3ea6f88d4e0ebdd77f03374f7 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sun, 25 Jul 2021 20:50:20 +0200 Subject: [PATCH 1/2] Fix some signed / unsigned mismatches Signed-off-by: Stefan Weil --- src/ccmain/osdetect.cpp | 5 ++--- src/classify/shapetable.cpp | 39 ++++++++++++++++++++----------------- src/dict/dict.cpp | 14 ++++++------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/ccmain/osdetect.cpp b/src/ccmain/osdetect.cpp index 09ae221ca3..55b388258e 100644 --- a/src/ccmain/osdetect.cpp +++ b/src/ccmain/osdetect.cpp @@ -373,9 +373,8 @@ bool OrientationDetector::detect_blob(BLOB_CHOICE_LIST *scores) { for (choice_it.mark_cycle_pt(); !choice_it.cycled_list() && choice == nullptr; choice_it.forward()) { int choice_script = choice_it.data()->script_id(); - unsigned s = 0; - for (s = 0; s < allowed_scripts_->size(); ++s) { - if ((*allowed_scripts_)[s] == choice_script) { + for (auto script : *allowed_scripts_) { + if (script == choice_script) { choice = choice_it.data(); break; } diff --git a/src/classify/shapetable.cpp b/src/classify/shapetable.cpp index abfb3f0fca..cf3ec60b27 100644 --- a/src/classify/shapetable.cpp +++ b/src/classify/shapetable.cpp @@ -37,12 +37,14 @@ namespace tesseract { // Returns -1 if the unichar_id is not found int ShapeRating::FirstResultWithUnichar(const std::vector &results, const ShapeTable &shape_table, UNICHAR_ID unichar_id) { - for (unsigned r = 0; r < results.size(); ++r) { - const auto shape_id = results[r].shape_id; + size_t r = 0; + for (const auto &result : results) { + const auto shape_id = result.shape_id; const Shape &shape = shape_table.GetShape(shape_id); if (shape.ContainsUnichar(unichar_id)) { return r; } + ++r; } return -1; } @@ -53,10 +55,12 @@ int ShapeRating::FirstResultWithUnichar(const std::vector &results, // Returns -1 if the unichar_id is not found int UnicharRating::FirstResultWithUnichar(const std::vector &results, UNICHAR_ID unichar_id) { - for (unsigned r = 0; r < results.size(); ++r) { - if (results[r].unichar_id == unichar_id) { + size_t r = 0; + for (const auto &result : results) { + if (result.unichar_id == unichar_id) { return r; } + ++r; } return -1; } @@ -122,8 +126,8 @@ void Shape::AddToShape(int unichar_id, int font_id) { // Adds everything in other to this. void Shape::AddShape(const Shape &other) { for (const auto &unichar : other.unichars_) { - for (unsigned f = 0; f < unichar.font_ids.size(); ++f) { - AddToShape(unichar.unichar_id, unichar.font_ids[f]); + for (auto font_id : unichar.font_ids) { + AddToShape(unichar.unichar_id, font_id); } } unichars_sorted_ = unichars_.size() <= 1; @@ -267,7 +271,7 @@ int ShapeTable::NumFonts() const { for (auto shape_id : shape_table_) { const Shape &shape = *shape_id; for (int c = 0; c < shape.size(); ++c) { - for (int font_id : shape[c].font_ids) { + for (auto font_id : shape[c].font_ids) { if (font_id >= num_fonts_) { num_fonts_ = font_id + 1; } @@ -405,7 +409,7 @@ int ShapeTable::FindShape(int unichar_id, int font_id) const { if (font_id < 0) { return s; // We don't care about the font. } - for (int f : shape[c].font_ids) { + for (auto f : shape[c].font_ids) { if (f == font_id) { return s; } @@ -428,14 +432,13 @@ void ShapeTable::GetFirstUnicharAndFont(unsigned shape_id, int *unichar_id, int int ShapeTable::BuildFromShape(const Shape &shape, const ShapeTable &master_shapes) { BitVector shape_map(master_shapes.NumShapes()); for (int u_ind = 0; u_ind < shape.size(); ++u_ind) { - for (unsigned f_ind = 0; f_ind < shape[u_ind].font_ids.size(); ++f_ind) { + for (auto font_id : shape[u_ind].font_ids) { int c = shape[u_ind].unichar_id; - int f = shape[u_ind].font_ids[f_ind]; - int master_id = master_shapes.FindShape(c, f); + int master_id = master_shapes.FindShape(c, font_id); if (master_id >= 0) { shape_map.SetBit(master_id); - } else if (FindShape(c, f) < 0) { - AddShape(c, f); + } else if (FindShape(c, font_id) < 0) { + AddShape(c, font_id); } } } @@ -630,19 +633,19 @@ bool ShapeTable::MergeEqualUnichars(int merge_id1, int merge_id2, unsigned shape const Shape &merge2 = GetShape(merge_id2); const Shape &shape = GetShape(shape_id); for (int cs = 0; cs < shape.size(); ++cs) { - int unichar_id = shape[cs].unichar_id; + auto unichar_id = shape[cs].unichar_id; if (!merge1.ContainsUnichar(unichar_id) && !merge2.ContainsUnichar(unichar_id)) { return false; // Shape has a unichar that appears in neither merge. } } for (int cm1 = 0; cm1 < merge1.size(); ++cm1) { - int unichar_id1 = merge1[cm1].unichar_id; + auto unichar_id1 = merge1[cm1].unichar_id; if (!shape.ContainsUnichar(unichar_id1)) { return false; // Merge has a unichar that is not in shape. } } for (int cm2 = 0; cm2 < merge2.size(); ++cm2) { - int unichar_id2 = merge2[cm2].unichar_id; + auto unichar_id2 = merge2[cm2].unichar_id; if (!shape.ContainsUnichar(unichar_id2)) { return false; // Merge has a unichar that is not in shape. } @@ -655,7 +658,7 @@ bool ShapeTable::CommonUnichars(unsigned shape_id1, unsigned shape_id2) const { const Shape &shape1 = GetShape(shape_id1); const Shape &shape2 = GetShape(shape_id2); for (int c1 = 0; c1 < shape1.size(); ++c1) { - int unichar_id1 = shape1[c1].unichar_id; + auto unichar_id1 = shape1[c1].unichar_id; if (shape2.ContainsUnichar(unichar_id1)) { return true; } @@ -725,7 +728,7 @@ void ShapeTable::AddShapeToResults(const ShapeRating &shape_rating, std::vector< for (int u = 0; u < shape.size(); ++u) { int result_index = AddUnicharToResults(shape[u].unichar_id, shape_rating.rating, unichar_map, results); - for (int font_id : shape[u].font_ids) { + for (auto font_id : shape[u].font_ids) { (*results)[result_index].fonts.emplace_back(font_id, IntCastRounded(shape_rating.rating * INT16_MAX)); } diff --git a/src/dict/dict.cpp b/src/dict/dict.cpp index 2dc5dcb733..411768203c 100644 --- a/src/dict/dict.cpp +++ b/src/dict/dict.cpp @@ -612,9 +612,9 @@ void Dict::init_active_dawgs(DawgPositionVector *active_dawgs, bool ambigs_mode) if (hyphenated()) { *active_dawgs = hyphen_active_dawgs_; if (dawg_debug_level >= 3) { - for (unsigned i = 0; i < hyphen_active_dawgs_.size(); ++i) { + for (const auto &dawg : hyphen_active_dawgs_) { tprintf("Adding hyphen beginning dawg [%d, " REFFORMAT "]\n", - hyphen_active_dawgs_[i].dawg_index, hyphen_active_dawgs_[i].dawg_ref); + dawg.dawg_index, dawg.dawg_ref); } } } else { @@ -633,12 +633,12 @@ void Dict::default_dawgs(DawgPositionVector *dawg_pos_vec, bool suppress_pattern if (dawg_ty == DAWG_TYPE_PUNCTUATION) { dawg_pos_vec->push_back(DawgPosition(-1, NO_EDGE, i, NO_EDGE, false)); if (dawg_debug_level >= 3) { - tprintf("Adding beginning punc dawg [%d, " REFFORMAT "]\n", i, NO_EDGE); + tprintf("Adding beginning punc dawg [%u, " REFFORMAT "]\n", i, NO_EDGE); } } else if (!punc_dawg_available || !subsumed_by_punc) { dawg_pos_vec->push_back(DawgPosition(i, NO_EDGE, -1, NO_EDGE, false)); if (dawg_debug_level >= 3) { - tprintf("Adding beginning dawg [%d, " REFFORMAT "]\n", i, NO_EDGE); + tprintf("Adding beginning dawg [%u, " REFFORMAT "]\n", i, NO_EDGE); } } } @@ -900,9 +900,9 @@ bool Dict::valid_punctuation(const WERD_CHOICE &word) { new_word.append_unichar_id(Dawg::kPatternUnicharID, 1, 0.0, 0.0); } } - for (unsigned i = 0; i < dawgs_.size(); ++i) { - if (dawgs_[i] != nullptr && dawgs_[i]->type() == DAWG_TYPE_PUNCTUATION && - dawgs_[i]->word_in_dawg(new_word)) { + for (auto dawg : dawgs_) { + if (dawg != nullptr && dawg->type() == DAWG_TYPE_PUNCTUATION && + dawg->word_in_dawg(new_word)) { return true; } } From b09b9767de0d761e87297fb372a5a330c84352b6 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sat, 16 Apr 2022 08:53:46 +0200 Subject: [PATCH 2/2] More signed/unsigned fixes Signed-off-by: Stefan Weil --- unittest/paragraphs_test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unittest/paragraphs_test.cc b/unittest/paragraphs_test.cc index fcc54b00e0..340f734b34 100644 --- a/unittest/paragraphs_test.cc +++ b/unittest/paragraphs_test.cc @@ -66,11 +66,11 @@ void AsciiToRowInfo(const char *text, int row_number, RowInfo *info) { info->lword_text = words[0].c_str(); info->rword_text = words[words.size() - 1].c_str(); - int lspace = 0; + unsigned lspace = 0; while (lspace < info->text.size() && text[lspace] == ' ') { lspace++; } - int rspace = 0; + unsigned rspace = 0; while (rspace < info->text.size() && text[info->text.size() - rspace - 1] == ' ') { rspace++; }