Skip to content

Commit 36c985b

Browse files
committed
Fix CID 1164746 (Big parameter passed by value)
Use std::vector instead of GenericVector. Fix also several signed / unsigned compiler warnings. Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent b24b2c2 commit 36c985b

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/textord/cjkpitch.cpp

+35-32
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// File: cjkpitch.cpp
33
// Description: Code to determine fixed pitchness and the pitch if fixed,
44
// for CJK text.
5-
// Copyright 2011 Google Inc. All Rights Reserved.
6-
// Author: takenaka@google.com (Hiroshi Takenaka)
5+
// Author: takenaka@google.com (Hiroshi Takenaka)
76
// Created: Mon Jun 27 12:48:35 JST 2011
87
//
8+
// Copyright 2011 Google Inc. All Rights Reserved.
99
// Licensed under the Apache License, Version 2.0 (the "License");
1010
// you may not use this file except in compliance with the License.
1111
// You may obtain a copy of the License at
@@ -17,12 +17,14 @@
1717
// limitations under the License.
1818
//
1919
///////////////////////////////////////////////////////////////////////
20+
2021
#include "cjkpitch.h"
2122
#include "genericvector.h"
2223
#include "topitch.h"
2324
#include "tovars.h"
2425

2526
#include <algorithm>
27+
#include <vector> // for std::vector
2628

2729
BOOL_VAR(textord_space_size_is_variable, FALSE,
2830
"If true, word delimiter spaces are assumed to have "
@@ -343,7 +345,7 @@ class FPRow {
343345
return gap_;
344346
}
345347

346-
int num_chars() {
348+
size_t num_chars() {
347349
return characters_.size();
348350
}
349351
FPChar *character(int i) {
@@ -556,7 +558,7 @@ void FPRow::OutputEstimations() {
556558

557559
// Make max_nonspace larger than any intra-character gap so that
558560
// make_prop_words() won't break a row at the middle of a character.
559-
for (int i = 0; i < num_chars(); ++i) {
561+
for (size_t i = 0; i < num_chars(); ++i) {
560562
if (characters_[i].max_gap() > real_row_->max_nonspace) {
561563
real_row_->max_nonspace = characters_[i].max_gap();
562564
}
@@ -572,7 +574,7 @@ void FPRow::OutputEstimations() {
572574
cell_it.add_after_then_move(cell);
573575

574576
int right = real_body(0).right();
575-
for (int i = 1; i < num_chars(); ++i) {
577+
for (size_t i = 1; i < num_chars(); ++i) {
576578
// Put a word break if gap between two characters is bigger than
577579
// space_threshold. Don't break if none of two characters
578580
// couldn't be "finalized", because maybe they need to be merged
@@ -614,7 +616,7 @@ void FPRow::EstimatePitch(bool pass1) {
614616
cx0 = center_x(0);
615617

616618
heights_.Add(box(0).height());
617-
for (int i = 1; i < num_chars(); i++) {
619+
for (size_t i = 1; i < num_chars(); i++) {
618620
cx1 = center_x(i);
619621
int32_t pitch = cx1 - cx0;
620622
int32_t gap = std::max(0, real_body(i - 1).x_gap(real_body(i)));
@@ -680,7 +682,7 @@ void FPRow::DebugOutputResult(int row_index) {
680682
real_row_->space_size, real_row_->space_threshold,
681683
real_row_->xheight);
682684

683-
for (int i = 0; i < num_chars(); i++) {
685+
for (size_t i = 0; i < num_chars(); i++) {
684686
tprintf("Char %d: is_final=%d is_good=%d num_blobs=%d: ",
685687
i, is_final(i), is_good(i), character(i)->num_blobs());
686688
box(i).print();
@@ -692,14 +694,14 @@ void FPRow::Pass1Analyze() {
692694
if (num_chars() < 2) return;
693695

694696
if (estimated_pitch_ > 0.0f) {
695-
for (int i = 2; i < num_chars(); i++) {
697+
for (size_t i = 2; i < num_chars(); i++) {
696698
if (is_good_pitch(estimated_pitch_, box(i - 2), box(i-1)) &&
697699
is_good_pitch(estimated_pitch_, box(i - 1), box(i))) {
698700
mark_good(i - 1);
699701
}
700702
}
701703
} else {
702-
for (int i = 2; i < num_chars(); i++) {
704+
for (size_t i = 2; i < num_chars(); i++) {
703705
if (is_good_pitch(box_pitch(box(i-2), box(i-1)), box(i - 1), box(i))) {
704706
mark_good(i - 1);
705707
}
@@ -715,7 +717,7 @@ bool FPRow::Pass2Analyze() {
715717
if (num_chars() <= 1 || estimated_pitch_ == 0.0f) {
716718
return false;
717719
}
718-
for (int i = 0; i < num_chars(); i++) {
720+
for (size_t i = 0; i < num_chars(); i++) {
719721
if (is_final(i)) continue;
720722

721723
FPChar::Alignment alignment = character(i)->alignment();
@@ -786,7 +788,7 @@ bool FPRow::Pass2Analyze() {
786788
}
787789
TBOX ibody(c1 - estimated_pitch_, box(i).bottom(), c1, box(i).top());
788790

789-
int j = i;
791+
size_t j = i;
790792
TBOX merged;
791793
while (j < num_chars() && !is_final(j) && mostly_overlap(ibody, box(j)) &&
792794
merged.bounding_union(box(j)).height() <
@@ -809,7 +811,7 @@ bool FPRow::Pass2Analyze() {
809811
character(i)->set_merge_to_prev(false);
810812
finalize(i);
811813
} else {
812-
for (int k = i + 1; k < j; k++) {
814+
for (size_t k = i + 1; k < j; k++) {
813815
character(k)->set_merge_to_prev(true);
814816
}
815817
}
@@ -832,7 +834,7 @@ bool FPRow::Pass2Analyze() {
832834
void FPRow::MergeFragments() {
833835
int last_char = 0;
834836

835-
for (int j = 0; j < num_chars(); ++j) {
837+
for (size_t j = 0; j < num_chars(); ++j) {
836838
if (character(j)->merge_to_prev()) {
837839
character(last_char)->Merge(*character(j));
838840
character(j)->set_delete_flag(true);
@@ -847,7 +849,7 @@ void FPRow::MergeFragments() {
847849

848850
void FPRow::FinalizeLargeChars() {
849851
float row_pitch = estimated_pitch();
850-
for (int i = 0; i < num_chars(); i++) {
852+
for (size_t i = 0; i < num_chars(); i++) {
851853
if (is_final(i)) continue;
852854

853855
// Finalize if both neighbors are finalized. We have no other choice.
@@ -888,7 +890,7 @@ void FPRow::FinalizeLargeChars() {
888890
// character L on its left and a not-finalized character R on its
889891
// right, we mark C as good if the pitch between C and L is good,
890892
// regardless of the pitch between C and R.
891-
for (int i = 0; i < num_chars(); i++) {
893+
for (size_t i = 0; i < num_chars(); i++) {
892894
if (!is_final(i)) continue;
893895
bool good_pitch = false;
894896
bool bad_pitch = false;
@@ -919,7 +921,7 @@ class FPAnalyzer {
919921
void Init(ICOORD page_tr, TO_BLOCK_LIST *port_blocks);
920922

921923
void Pass1Analyze() {
922-
for (int i = 0; i < rows_.size(); i++) rows_[i].Pass1Analyze();
924+
for (size_t i = 0; i < rows_.size(); i++) rows_[i].Pass1Analyze();
923925
}
924926

925927
// Estimate character pitch for each row. The argument pass1 can be
@@ -934,16 +936,16 @@ class FPAnalyzer {
934936
}
935937

936938
void MergeFragments() {
937-
for (int i = 0; i < rows_.size(); i++) rows_[i].MergeFragments();
939+
for (size_t i = 0; i < rows_.size(); i++) rows_[i].MergeFragments();
938940
}
939941

940942
void FinalizeLargeChars() {
941-
for (int i = 0; i < rows_.size(); i++) rows_[i].FinalizeLargeChars();
943+
for (size_t i = 0; i < rows_.size(); i++) rows_[i].FinalizeLargeChars();
942944
}
943945

944946
bool Pass2Analyze() {
945947
bool changed = false;
946-
for (int i = 0; i < rows_.size(); i++) {
948+
for (size_t i = 0; i < rows_.size(); i++) {
947949
if (rows_[i].Pass2Analyze()) {
948950
changed = true;
949951
}
@@ -952,33 +954,34 @@ class FPAnalyzer {
952954
}
953955

954956
void OutputEstimations() {
955-
for (int i = 0; i < rows_.size(); i++) rows_[i].OutputEstimations();
957+
for (size_t i = 0; i < rows_.size(); i++) rows_[i].OutputEstimations();
956958
// Don't we need page-level estimation of gaps/spaces?
957959
}
958960

959961
void DebugOutputResult() {
960962
tprintf("FPAnalyzer: final result\n");
961-
for (int i = 0; i < rows_.size(); i++) rows_[i].DebugOutputResult(i);
963+
for (size_t i = 0; i < rows_.size(); i++) rows_[i].DebugOutputResult(i);
962964
}
963965

964-
int num_rows() {
966+
size_t num_rows() {
965967
return rows_.size();
966968
}
967969

968970
// Returns the upper limit for pass2 loop iteration.
969-
int max_iteration() {
971+
unsigned max_iteration() {
970972
// We're fixing at least one character per iteration. So basically
971973
// we shouldn't require more than max_chars_per_row_ iterations.
972974
return max_chars_per_row_ + 100;
973975
}
974976

975977
private:
976978
ICOORD page_tr_;
977-
GenericVector<FPRow> rows_;
978-
int num_tall_rows_;
979-
int num_bad_rows_;
980-
int num_empty_rows_;
981-
int max_chars_per_row_;
979+
std::vector<FPRow> rows_;
980+
unsigned num_tall_rows_;
981+
unsigned num_bad_rows_;
982+
// TODO: num_empty_rows_ is incremented, but never used overwise.
983+
unsigned num_empty_rows_;
984+
unsigned max_chars_per_row_;
982985
};
983986

984987
void FPAnalyzer::Init(ICOORD page_tr, TO_BLOCK_LIST *port_blocks) {
@@ -1005,7 +1008,7 @@ void FPAnalyzer::Init(ICOORD page_tr, TO_BLOCK_LIST *port_blocks) {
10051008
FPRow row;
10061009
row.Init(row_it.data());
10071010
rows_.push_back(row);
1008-
int num_chars = rows_.back().num_chars();
1011+
size_t num_chars = rows_.back().num_chars();
10091012
if (num_chars <= 1) num_empty_rows_++;
10101013
if (num_chars > max_chars_per_row_) max_chars_per_row_ = num_chars;
10111014
}
@@ -1018,7 +1021,7 @@ void FPAnalyzer::EstimatePitch(bool pass1) {
10181021
num_tall_rows_ = 0;
10191022
num_bad_rows_ = 0;
10201023
pitch_height_stats.Clear();
1021-
for (int i = 0; i < rows_.size(); i++) {
1024+
for (size_t i = 0; i < rows_.size(); i++) {
10221025
rows_[i].EstimatePitch(pass1);
10231026
if (rows_[i].good_pitches()) {
10241027
pitch_height_stats.Add(rows_[i].height() + rows_[i].gap(),
@@ -1030,7 +1033,7 @@ void FPAnalyzer::EstimatePitch(bool pass1) {
10301033
}
10311034

10321035
pitch_height_stats.Finish();
1033-
for (int i = 0; i < rows_.size(); i++) {
1036+
for (size_t i = 0; i < rows_.size(); i++) {
10341037
if (rows_[i].good_pitches() >= 5) {
10351038
// We have enough evidences. Just use the pitch estimation
10361039
// from this row.
@@ -1077,7 +1080,7 @@ void compute_fixed_pitch_cjk(ICOORD page_tr,
10771080
return;
10781081
}
10791082

1080-
int iteration = 0;
1083+
size_t iteration = 0;
10811084
do {
10821085
analyzer.MergeFragments();
10831086
analyzer.FinalizeLargeChars();

0 commit comments

Comments
 (0)