Skip to content

Modernize some for loops and fix some signed/unsigned issues #4438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/ccmain/osdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be if (std::ranges::find(*allowed_scripts_, choice_script) != allowed_scripts_->end()) ...

if (script == choice_script) {
choice = choice_it.data();
break;
}
Expand Down
39 changes: 21 additions & 18 deletions src/classify/shapetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ namespace tesseract {
// Returns -1 if the unichar_id is not found
int ShapeRating::FirstResultWithUnichar(const std::vector<ShapeRating> &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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (shape_table.GetShape(result.shape_id).ContainsUnichar(unichar_id)) {

also can be put inside std::ranges::find_if()

return r;
}
++r;
}
return -1;
}
Expand All @@ -53,10 +55,12 @@ int ShapeRating::FirstResultWithUnichar(const std::vector<ShapeRating> &results,
// Returns -1 if the unichar_id is not found
int UnicharRating::FirstResultWithUnichar(const std::vector<UnicharRating> &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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::ranges::find

if (result.unichar_id == unichar_id) {
return r;
}
++r;
}
return -1;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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.
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
}
Expand Down
14 changes: 7 additions & 7 deletions src/dict/dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions unittest/paragraphs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
Loading