Skip to content

Commit

Permalink
Fewer g++ -Wsign-compare warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rfschtkt committed May 11, 2017
1 parent 1bd561d commit c335508
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions api/pdfrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ bool TessPDFRenderer::BeginDocumentHandler() {
long int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
const std::unique_ptr</*non-const*/ char[]> buffer(new char[size]);
if (fread(buffer.get(), 1, size, fp) != size) {
if (fread(buffer.get(), 1, size, fp) != static_cast<unsigned long>(size)) {
fclose(fp);
return false;
}
Expand Down Expand Up @@ -945,7 +945,7 @@ bool TessPDFRenderer::EndDocumentHandler() {
if (n >= sizeof(buf)) return false;
AppendString(buf);
size_t pages_objsize = strlen(buf);
for (size_t i = 0; i < pages_.size(); i++) {
for (size_t i = 0; i < pages_.unsigned_size(); i++) {
n = snprintf(buf, sizeof(buf),
"%ld 0 R ", pages_[i]);
if (n >= sizeof(buf)) return false;
Expand Down
2 changes: 1 addition & 1 deletion ccmain/docqual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ BOOL8 Tesseract::terrible_word_crunch(WERD_RES *word,

if ((word->best_choice->unichar_string().length () == 0) ||
(strspn (word->best_choice->unichar_string().string(), " ") ==
word->best_choice->unichar_string().length ()))
word->best_choice->unichar_string().unsigned_size ()))
crunch_mode = 1;
else {
adjusted_len = word->reject_map.length ();
Expand Down
18 changes: 12 additions & 6 deletions ccutil/genericvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class GenericVector {
int size() const {
return size_used_;
}
// Workaround to avoid g++ -Wsign-compare warnings.
unsigned int unsigned_size() const {
static_assert(sizeof(size_used_) <= sizeof(unsigned int), "");
assert(0 <= size_used_);
return static_cast<unsigned int>(size_used_);
}
int size_reserved() const {
return size_reserved_;
}
Expand Down Expand Up @@ -880,7 +886,7 @@ bool GenericVector<T>::write(
}
delete cb;
} else {
if (fwrite(data_, sizeof(T), size_used_, f) != size_used_) return false;
if (fwrite(data_, sizeof(T), size_used_, f) != unsigned_size()) return false;
}
return true;
}
Expand Down Expand Up @@ -912,7 +918,7 @@ bool GenericVector<T>::read(
template <typename T>
bool GenericVector<T>::Serialize(FILE* fp) const {
if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
if (fwrite(data_, sizeof(*data_), size_used_, fp) != size_used_) return false;
if (fwrite(data_, sizeof(*data_), size_used_, fp) != unsigned_size()) return false;
return true;
}
template <typename T>
Expand All @@ -933,7 +939,7 @@ bool GenericVector<T>::DeSerialize(bool swap, FILE* fp) {
if (swap) Reverse32(&reserved);
reserve(reserved);
size_used_ = reserved;
if (fread(data_, sizeof(T), size_used_, fp) != size_used_) return false;
if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) return false;
if (swap) {
for (int i = 0; i < size_used_; ++i)
ReverseN(&data_[i], sizeof(data_[i]));
Expand Down Expand Up @@ -982,7 +988,7 @@ bool GenericVector<T>::SerializeClasses(tesseract::TFile* fp) const {
// If swap is true, assumes a big/little-endian swap is needed.
template <typename T>
bool GenericVector<T>::DeSerializeClasses(bool swap, FILE* fp) {
uinT32 reserved;
inT32 reserved;
if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
if (swap) Reverse32(&reserved);
T empty;
Expand All @@ -994,7 +1000,7 @@ bool GenericVector<T>::DeSerializeClasses(bool swap, FILE* fp) {
}
template <typename T>
bool GenericVector<T>::DeSerializeClasses(tesseract::TFile* fp) {
uinT32 reserved;
inT32 reserved;
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
T empty;
init_to_size(reserved, empty);
Expand All @@ -1005,7 +1011,7 @@ bool GenericVector<T>::DeSerializeClasses(tesseract::TFile* fp) {
}
template <typename T>
bool GenericVector<T>::SkipDeSerializeClasses(tesseract::TFile* fp) {
uinT32 reserved;
inT32 reserved;
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
for (int i = 0; i < reserved; ++i) {
if (!T::SkipDeSerialize(fp)) return false;
Expand Down
7 changes: 7 additions & 0 deletions ccutil/strngs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef STRNGS_H
#define STRNGS_H

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "platform.h"
Expand Down Expand Up @@ -66,6 +67,12 @@ class TESS_API STRING
BOOL8 contains(const char c) const;
inT32 length() const;
inT32 size() const { return length(); }
// Workaround to avoid g++ -Wsign-compare warnings.
uinT32 unsigned_size() const {
const inT32 len = length();
assert(0 <= len);
return static_cast<uinT32>(len);
}
const char *string() const;
const char *c_str() const;

Expand Down
2 changes: 1 addition & 1 deletion classify/blobclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void Classify::LearnBlob(const STRING& fontname, TBLOB* blob,
bool Classify::WriteTRFile(const STRING& filename) {
STRING tr_filename = filename + ".tr";
FILE* fp = Efopen(tr_filename.string(), "wb");
int len = tr_file_data_.length();
size_t len = tr_file_data_.length();
bool result =
fwrite(&tr_file_data_[0], sizeof(tr_file_data_[0]), len, fp) == len;
fclose(fp);
Expand Down
15 changes: 5 additions & 10 deletions classify/featdefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) {
* @note History: Wed May 23 13:52:19 1990, DSJ, Created.
*/
void FreeCharDescription(CHAR_DESC CharDesc) {
int i;

if (CharDesc) {
for (i = 0; i < CharDesc->NumFeatureSets; i++)
for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
FreeFeatureSet (CharDesc->FeatureSets[i]);
Efree(CharDesc);
}
Expand All @@ -163,12 +161,10 @@ void FreeCharDescription(CHAR_DESC CharDesc) {
*/
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
CHAR_DESC CharDesc;
int i;

CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;

for (i = 0; i < CharDesc->NumFeatureSets; i++)
for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
CharDesc->FeatureSets[i] = NULL;

return (CharDesc);
Expand Down Expand Up @@ -196,16 +192,15 @@ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
*/
void WriteCharDescription(const FEATURE_DEFS_STRUCT& FeatureDefs,
CHAR_DESC CharDesc, STRING* str) {
int Type;
int NumSetsToWrite = 0;

for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++)
if (CharDesc->FeatureSets[Type])
NumSetsToWrite++;

str->add_str_int(" ", NumSetsToWrite);
*str += "\n";
for (Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
if (CharDesc->FeatureSets[Type]) {
*str += FeatureDefs.FeatureDesc[Type]->ShortName;
*str += " ";
Expand All @@ -220,7 +215,7 @@ bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
CHAR_DESC CharDesc) {
bool anything_written = false;
bool well_formed = true;
for (int Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
if (CharDesc->FeatureSets[Type]) {
for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
Expand Down
2 changes: 1 addition & 1 deletion lstm/networkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void NetworkIO::FromPixes(const StaticShape& shape,
stride_map_.SetStride(h_w_pairs);
ResizeToMap(int_mode(), stride_map_, shape.depth());
// Iterate over the images again to copy the data.
for (int b = 0; b < pixes.size(); ++b) {
for (size_t b = 0; b < pixes.size(); ++b) {
Pix* pix = const_cast<Pix*>(pixes[b]);
float black = 0.0f, white = 255.0f;
if (shape.depth() != 3) ComputeBlackWhite(pix, &black, &white);
Expand Down
3 changes: 2 additions & 1 deletion lstm/stridemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ bool StrideMap::Index::IsLast(FlexDimensions dimension) const {
int StrideMap::Index::MaxIndexOfDim(FlexDimensions dim) const {
int max_index = stride_map_->shape_[dim] - 1;
if (dim == FD_BATCH) return max_index;
int batch = indices_[FD_BATCH];
assert(0 <= indices_[FD_BATCH]);
const size_t batch = indices_[FD_BATCH];
if (dim == FD_HEIGHT) {
if (batch >= stride_map_->heights_.size() ||
stride_map_->heights_[batch] > max_index)
Expand Down
3 changes: 1 addition & 2 deletions training/commontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs,
LABELEDLIST char_sample;
FEATURE_SET feature_samples;
CHAR_DESC char_desc;
int i;
int feature_type = ShortNameToFeatureType(feature_defs, feature_name);
// Zero out the font_sample_count for all the classes.
LIST it = *training_samples;
Expand Down Expand Up @@ -404,7 +403,7 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs,
} else {
FreeFeatureSet(feature_samples);
}
for (i = 0; i < char_desc->NumFeatureSets; i++) {
for (size_t i = 0; i < char_desc->NumFeatureSets; i++) {
if (feature_type != i)
FreeFeatureSet(char_desc->FeatureSets[i]);
}
Expand Down

0 comments on commit c335508

Please sign in to comment.